版权声明:本站所有文章如未特别说明均为原创,未经本人许可禁止转载
本文链接:https://www.cdroom.net/2018/12/01/%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE.html

Linux整理 云梦斋主人 2018年12月01日发布 所属分类:IT   返回主页

本文主要是新建服务器的常用环境配置,针对的是Centos系统,其他版本的Linux也可以参考。

常用工具

登录设置

有的云服务器新建后只有一个root用户,而Linux系统不建议直接在root中执行操作,因此有必要为系统添加一个普通用户,并禁止root的远程登录权限。

Nginx

Nginx开始向导

安装并启动后,可以使用curl 127.0.0.1或者wget 127.0.0.1测试Nginx是否正常运行。

Jekyll

Jekyll是一个静态博客渲染器,支持用Markdown编写我们的静态网页,结合Github Pages我们可以轻松在Github上建立自己的静态博客站点,而无需自己搭建Web环境。Jekyll的安装及使用可以参考Jekyll官方文档,这里介绍一下Jekyll在Linux上的安装步骤:

  1. 安装依赖(应该避免直接使用root用户安装):
    • 如果是Ubuntu,使用:sudo apt-get install ruby-full build-essential
    • 如果是Centos,直接使用sudo yum install ruby ruby-devel安装的ruby版本太旧,导致Jekyll无法安装。因此需要先添加yum源(当然也可以编译安装):
      yum install centos-release-scl-rh
      

      然后执行yum install rh-ruby24 rh-ruby24-ruby-devel -y命令,安装ruby和ruby-devel。注意其中的24是ruby的版本,根据自己的需要选择,可以使用命令yum list rh-ruby*查看所有可用的rh-ruby版本。

      安装完成后,执行source scl_source enable rh-ruby24,然后执行ruby -v查看是否安装成功。source scl_source enable rh-ruby24只在当前会话中生效,如果不想每次登录后执行ruby前都执行此命令,可以使用下面命令在~/.bashrc中添加配置:

      echo '# enable rh-ruby' >>  ~/.bashrc
      echo 'source scl_source enable rh-ruby24' >>  ~/.bashrc
      source ~/.bashrc #立即生效
      
  2. 添加环境变量:
    echo '# Install Ruby Gems to ~/gems' >> ~/.bashrc
    echo 'export GEM_HOME=$HOME/gems' >> ~/.bashrc
    echo 'export PATH=$HOME/gems/bin:$PATH' >> ~/.bashrc
    source ~/.bashrc
    
  3. 安装Jekyll,使用:
    gem install jekyll bundler
    

    这里还有问题,如果在服务器在国内,gem源可能访问不了或者访问速度极慢,导致gem命令无反应。因此需要先删除gem自带的源并添加国内的源:

    gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
    gem sources -l	# 查看当前的源,看看是否添加成功
    

    如果安装过程中提示gcc和g++未发现,那么使用下面命令安装gcc和g++,然后重新执行安装:

    yum install gcc gcc-c++
    
  4. 新建站点并运行:
    • 新建
      jekyll new myblog
      
    • 运行
      bundle exec jekyll serve
      

PHP-FPM

PHP-FPM官网

  1. 查看已安装的php程序
    yum list installed | grep php 
    
  2. 安装PHP7所在的源
    • 安装php源
      rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 
      rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
      
    • 检查源是否安装成功
      yum repolist enabled | grep "webtatic*"
      
    • 安装php-fpm 7.1及其基本扩展
      yum install -y php71w-fpm php71w-common php71w-devel php71w-cli php71w-gd php71w-mbstring php71w-mysql php71w-pdo
      
      其他常用的扩展:
      php71w-mcrypt php71w-xml
      php71w-pecl-memcached php71w-pecl-redis php71w-opcache
      
    • 验证安装是否成功
      • 验证php是否安装成功:php -v
      • 验证对应的扩展是否安装成功:php -m
      • 启动php-fpm服务:systemctl start php-fpm.service
      • 设置开机启动:systemctl enable php-fpm.service
  3. 主要配置文件 /etc/php.ini

JDK

  1. 下载 到Oracle官网找到Java SE Development Kit版本下载链接,点击Accept License Agreement,然后选择Linux x64的后缀是.tar.gz的版本,然后鼠标右击下载链接,选择复制链接地址。此时,在Linux Shell中执行下面的命令下载JDK:
     wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" 这里加上刚才复制的链接地址
    
  2. 配置 本文下载的JDK版本是8u191,因此文件名是jdk-8u191-linux-x64.tar.gz,接下来的步骤请替换为自己的版本。
    • 解压JDK
        tar -xzf jdk-8u191-linux-x64.tar.gz		#解压
        sudo mv jdk1.8.0_191 /usr/local/jdk1.8.0_191	#移动到/usr/local目录
      
    • 添加环境变量 使用sudo vim /etc/profile打开/etc/profile文件,在末尾添加:
        #JDK environment
        export JAVA_HOME=/usr/local/jdk1.8.0_191
        export JRE_HOME=$JAVA_HOME/jre
        export PATH=$PATH:$JAVA_HOME/bin
        export CLASSPATH=./:$JAVA_HOME/lib:$JRE_HOME/lib
      

      然后保存,退出。执行source /etc/profile使新添加的环境变量立即生效。使用java -version进行测试,如果安装成功会打印java的版本号。

MariaDB

MariaDB源于MySQL,除了一些地方的名字与MySQL不同,使用方法与MySQL几乎完全一致。此处安装时的版本是mariadb-5.5,对于其他版本未做测试,一般来说是一致的。

  1. 安装与卸载
    • 安装
        yum install mariadb mariadb-server
      
    • 启动
        systemctl start mariadb.service
        systemctl enable mariadb.service
      

      启动后使用mysql -uroot登录mysql,看一下安装是否成功。

  2. 配置
    • /etc/my.cnf 主配置文件。第2-3行指定了数据文件的存储位置,可以修改为自己的目录。其他地方不需更改。
        [mysqld]
        datadir=/var/lib/mysql
        socket=/var/lib/mysql/mysql.sock
        # Disabling symbolic-links is recommended to prevent assorted security risks
        symbolic-links=0
        # Settings user and group are ignored when systemd is used.
        # If you need to run mysqld under a different user or group,
        # customize your systemd unit file for mariadb according to the
        # instructions in http://fedoraproject.org/wiki/Systemd
      
        [mysqld_safe]
        log-error=/var/log/mariadb/mariadb.log
        pid-file=/var/run/mariadb/mariadb.pid
      
        #
        # include all files from the config directory
        #
        !includedir /etc/my.cnf.d
      
    • /etc/my.cnf.d/client.cnf 不需修改。可在client中指定提示符格式。
        #
        # These two groups are read by the client library
        # Use it for options that affect all clients, but not the server
        #
      
      
        [client]
        #prompt="\\u@\\h \\d>"
        # This group is not read by mysql client library,
        # If you use the same .cnf file for MySQL and MariaDB,
        # use it for MariaDB-only client options
        [client-mariadb]
      
      
    • /etc/my.cnf.d/mysql-clients.cnf[mysql]选项下添加一行,用于指定默认字符编码。
        #
        # These groups are read by MariaDB command-line tools
        # Use it for options that affect only one utility
        #
      
        [mysql]
        default-character-set=utf8
        [mysql_upgrade]
      
        [mysqladmin]
      
        [mysqlbinlog]
      
        [mysqlcheck]
      
        [mysqldump]
      
        [mysqlimport]
      
        [mysqlshow]
      
        [mysqlslap]
      
      
    • /etc/my.cnf.d/server.cnf 默认没有任何选项被设置,可根据下面的样例修改,不再赘述。
        #
        # These groups are read by MariaDB server.
        # Use it for options that only the server (but not clients) should see
        #
        # See the examples of server my.cnf files in /usr/share/mysql/
        #
      
        # this is read by the standalone daemon and embedded servers
        [server]
      
        # this is only for the mysqld standalone daemon
        [mysqld]
        init_connect='SET collation_connection = utf8_general_ci'
        init_connect='SET NAMES utf8'
        character-set-server=utf8
        collation-server=utf8_general_ci
        skip-character-set-client-handshake
      
        #open slow query log
        slow_query_log = ON
        slow_query_log_file = /var/log/mariadb/slow.log
        long_query_time = 1
        # this is only for embedded server
        [embedded]
      
        # This group is only read by MariaDB-5.5 servers.
        # If you use the same .cnf file for MariaDB of different versions,
        # use this group for options that older servers don't understand
        [mysqld-5.5]
      
        # These two groups are only read by MariaDB servers, not by MySQL.
        # If you use the same .cnf file for MySQL and MariaDB,
        # you can put MariaDB-only options here
        [mariadb]
      
        [mariadb-5.5]
      
  3. 更改默认文件夹的方法 默认MariaDB/MySQL的数据文件存放在/var/lib/mysql目录下,有时我们需要更改数据文件存放的位置(下面假设存放在/data/db),更改的步骤如下:
    • 停止MariaDB服务:systemctl stop mariadb.service
    • /etc/my.cnf中的datadirsocket的路径修改为/data/db(自己的目录)
    • /etc/my.cnf.d/client.cnf中的[client]下添加socket=/data/db/mysql.sock
    • 复制原来的数据文件到新的目录:
        cp -a /var/lib/mysql/*  /data/db
      
    • 启动MariaDB服务:systemctl start mariadb.service
    • 如果未出现问题,将原文件夹的文件删除(可选)

    如果出现错误,检查是否是权限问题导致的,/data/db以及其子目录的所有者应该是mysql:mysql,如果不是,可用下面的命令修改:

     chown mysql:mysql /data/db -R
    

    用此方法,也可将旧的数据库迁移到的新数据库来。直接将旧数据库下的文件复制到新的空数据库目录中(同名文件直接覆盖)。

    建议不要移动mysql.sock文件,因为该文件是MySQL Server的监听sock,并不存放我们的数据,而其他程序(例如MySQL Client和各种MySQL驱动)会默认使用/var/lib/mysql/mysql.sock这个sock文件去连接MySQL Server,如果更改该文件的位置的话,需要连接MySQL Server的程序或驱动的设置可能都需要更改。因此如无特殊需要,不要更改此文件的位置。

    更改PHP配置文件/etc/php.ini,把pdo_mysql.default_socket设置为移动后的文件路径:

     [Pdo_mysql]
     ; If mysqlnd is used: Number of cache slots for the internal result set cache
     ; http://php.net/pdo_mysql.cache_size
     pdo_mysql.cache_size = 2000
    
     ; Default socket name for local MySQL connects.  If empty, uses the built-in
     ; MySQL defaults.
     ; http://php.net/pdo_mysql.default-socket
     pdo_mysql.default_socket=/data/db/mysql.sock
    
     [Phar]
    
  4. 卸载
     yum remove mariadb mariadb-server
     rm -rf /var/lib/mysql
     rm -rf /etc/my.cnf /etc/my.cnf.d #	配置文件根据自己需要决定是否删除,删除前最好备份,以免配置文件丢失。
    

Samba(Ubuntu)

MariaDB源于MySQL,除了一些地方的名字与MySQL不同,使用方法与MySQL几乎完全一致。此处安装时的版本是mariadb-5.5,对于其他版本未做测试,一般来说是一致的。

  1. 安装
    • 安装
        sudo apt-get install samba
        sudo apt-get install smbclient  #客户端程序,可选
      
  2. 配置
    • sudo vim /etc/samba/smb.conf 打开配置文件后,在配置文件末尾加上以下内容,此外,配置文件中的printers和print不需要可以将其注释掉。
        [share]
        comment = Shared Folder require password
        path = /home/share
        public = yes
        writable = yes
        valid users = share
        create mask = 0777
        directory mask = 0777
        force user = share
        force group = share  # 登录后的用户身份
        available = yes
        browseable = yes
        read only = no
      

      配置完成后,可用testparm命令检查配置文件是否有错误

    • 向samba服务中添加用户 smbpasswd -a share,将share用户添加进samba服务,并设置该用户访问samba的密码(注意不是share用户在Linux中的密码)。 如果系统中不存在share用户,可用adduser share命令创建,当然也可用使用除share外的其他用户名。
  3. 运行Samba服务
    • 启动
        systemctl start smbd.service
        systemctl enable smbd.service  #使能开机自启
      
    • 访问测试 执行smbclient //127.0.0.1/share -U share命令,根据提示输入密码,如果进入smb shell则说明一切正常。
  4. 端口映射 该步骤是samba服务器139、445端口被封时(如阿里云等禁止了139、445端口的访问)的解决方案,如果139、445端口可以直接访问(如局域网,注意要开放防火墙中的TCP139、445端口)则可以跳过该步骤。 x原理是使用iptables的端口转发将流量从其他未被封的端口转发到445端口(包括139端口),步骤如下:
     sudo iptables -t nat -A PREROUTING -p tcp -i eth0 -d 172.18.60.111 --dport 3139 -j DNAT --to 172.18.60.111:139
     sudo iptables -t nat -A PREROUTING -p tcp -i eth0 -d 172.18.60.111 --dport 3445 -j DNAT --to 172.18.60.111:445
    

    其中,172.18.60.111是samba服务器的外网ip,功能是实现端口3139->139,3445->445的流量转发。

  5. Windows中使用samba服务

自定义服务(含开机启动)

要创建要由systemd管理的自定义服务,请创建一个定义该服务配置的单元文件。 例如,要创建名为myservice的服务,请在/etc/systemd/system/中创建名为myservice.service的文件。

  1. 创建服务
    • 创建普通服务,vim /etc/systemd/system/s-l.service
        [Unit]
        Description=s-l
        After=network.target
      
        [Service]
        Type=simple
        ExecStart=/usr/bin/s-l -c /etc/s-l/config.json > /var/log/s-l.log 2>&1
      
        [Install]
        WantedBy=default.target
      
    • 创建Oneshot服务,vim /etc/systemd/system/sdb-mount.service
        [Unit]
        Description=sdb2 sdb3 mount
        After=network.target
      
        [Service]
        Type=onesgot
        ExecStart=/etc/sdb-mount
        ExecStop=/etc/sdb-umount
        RemainAfterExit=yes
        [Install]
        WantedBy=default.target
      
  2. 服务管理
    • 启动服务
        chmod 664 /etc/systemd/system/myservice.service     #更改权限
        systemctl daemon-reload    #重新加载所有Units
        systemctl start myservice.service    #启动服务
      
    • 列出当前Units
        systemctl list-units
        systemctl list-units -t service
        systemctl list-unit-files -t service
        systemctl list-unit-files --state enabled
      

GitWeb

https://git-scm.com/book/zh/v1/%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E7%9A%84-Git-GitWeb https://my.oschina.net/u/2270256/blog/1922546

  1. 安装配置git
    • 安装
        yum install git git-core
      
    • 添加git用户
        sudo adduser git
        su git
        cd
        mkdir .ssh
        cat xxx/id_rsa.pub >> ~/.ssh/authorized_keys
      

      其中xxx/id_rsa.pub为客户端的ssh公钥

    • 禁止git用户登录系统shell。执行 sudo vim /etc/passwd,在文件末尾找到类似这样的行:
        git:x:1000:1000::/home/git:/bin/sh
      

      把其中的 bin/sh 改为 /usr/bin/git-shell (或者用 which git-shell 查看它的实际安装路径)。该行修改后的样子如下:

        git:x:1000:1000::/home/git:/usr/bin/git-shell
      
  2. 建立git仓库,测试git server
    • 在server端建立git仓库,此处将仓库根目录设置到/data/git
        mkdir /data/git && chown git:git /data/git -R
        cd /data/git
        mkdir project.git
        cd project.git
        git --bare init
      
    • 在客户端执行
        git clone git@server:/data/git/project.git	#验证pull
        cd project
        echo "# Test File" >> README.md
        git add .
        git commit -m "init commit"
        git push origin master	#验证push
      
  3. 安装Gitweb(nginx)
    • 安装相关程序
        yum install nginx perl-CGI fcgiwrap spawn-fcgi gitweb highlight
      
    • 配置Nginx。在配置文件中添加如下的server,然后启动nginx服务。
        server {
            listen       9091 default_server;
            listen       [::]:9091 default_server;
            server_name  _;
      
            # Load configuration files for the default server block.
            include /etc/nginx/default.d/*.conf;
      
            location /gitweb.cgi {
            gzip                off;
            fastcgi_param       SCRIPT_FILENAME     /var/www/git/gitweb.cgi;
            fastcgi_param       GITWEB_CONFIG       /etc/gitweb.conf;
            fastcgi_pass        unix:/var/run/fcgiwrap.socket;
            include             fastcgi_params;
            }
      
            location / {
            root    /var/www/git;
            index   gitweb.cgi;
            }
        }
      
    • 配置gitweb,vim /etc/gitweb.conf,修改(如果存在)或添加:
        $projectroot = "/data/git";
        $git_temp = "/tmp";
        $feature{'highlight'}{'default'} = [1];
      
    • 配置CGI。 vim /etc/init.d/fcgiwrap
        #! /bin/bash
        ### BEGIN INIT INFO
        # Provides:          fcgiwrap
        # Required-Start:    $remote_fs
        # Required-Stop:     $remote_fs
        # Should-Start:
        # Should-Stop:
        # Default-Start:     2 3 4 5
        # Default-Stop:      0 1 6
        # Short-Description: FastCGI wrapper
        # Description:       Simple server for running CGI applications over FastCGI
        ### END INIT INFO
      
        PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
        SPAWN_FCGI="/usr/bin/spawn-fcgi"
        DAEMON="/usr/sbin/fcgiwrap"
        NAME="fcgiwrap"
      
        PIDFILE="/var/run/$NAME.pid"
      
        FCGI_SOCKET="/var/run/$NAME.socket"
        FCGI_USER="git"
        FCGI_GROUP="nginx"
        FORK_NUM=5
        SCRIPTNAME=/etc/init.d/$NAME
      
        case "$1" in
            start)
            echo -n "Starting $NAME... "
      
            PID=`pidof $NAME`
            if [ ! -z "$PID" ]; then
                echo " $NAME already running"
                exit 1
            fi
      
            $SPAWN_FCGI -u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -P $PIDFILE -F $FORK_NUM -f $DAEMON
      
            if [ "$?" != 0 ]; then
                echo " failed"
                exit 1
            else
                chmod g+w $FCGI_SOCKET
                echo " done"
            fi
            ;;
      
            stop)
            echo -n "Stoping $NAME... "
      
            PID=`pidof $NAME`
            if [ ! -z "$PID" ]; then
                kill `pidof $NAME`
                if [ "$?" != 0 ]; then
                echo " failed. re-quit"
                exit 1
                else
                rm -f $pid
                echo " done"
                fi
            else
                echo "$NAME is not running."
                exit 1
            fi
            ;;
      
            status)
            PID=`pidof $NAME`
            if [ ! -z "$PID" ]; then
                echo "$NAME (pid $PID) is running..."
            else
                echo "$NAME is stopped"
                exit 0
            fi
            ;;
      
            restart)
            $SCRIPTNAME stop
            sleep 1
            $SCRIPTNAME start
            ;;
      
            *)
            echo "Usage: $SCRIPTNAME {start|stop|restart|status}"
            exit 1
            ;;
        esac
      

      启动脚本,注意spawn-fcgi跟fcgiwrap脚本路径及FCGI_GROUP跟FCGI_GROUP,脚本启动了5个cgi进程,按需调整:

        chmod a+x /etc/init.d/fcgiwrap
        chkconfig --level 35 fcgiwrap on
        sudo /etc/init.d/fcgiwrap start
      
    • 给gitweb添加basic authorization

      生成认证文件

        sudo printf "test:$(openssl passwd -apr1 123456)\n" >> /var/www/git/.gitpasswd
        sudo chown nginx:nginx /var/www/git/.gitpasswd
      

      在前面nginx的server配置中添加:

        auth_basic "Restricted";
        auth_basic_user_file /var/www/git/.gitpasswd;
      

      重启nginx,使用curl测试gitweb访问

        curl localhost:9091 -i
        curl localhost:9091 -i --basic -u test:123456
      

编辑于2018-12-01

云梦斋主人


上一篇:Ubuntu下安装Gnuradio和UHD

下一篇:Matlab绘图手册