1. 程式人生 > >編譯安裝 Lnmp 並使用服務管理

編譯安裝 Lnmp 並使用服務管理

optimize lib 服務 ups char main 客戶 cmake file

Ubuntu 更換國內鏡像源
sudo vim /etc/apt/source.list
    sudo apt update
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
    deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
    deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
    deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse

安裝依賴

sudo apt install gcc openssl  libssl-dev  libpcre3 libpcre3-dev zlib1g zlib1g-dev make autoconf automake cmake

準備用戶和用戶組

查看 ×××w 用戶是否存在 : groups ×××w

groupadd ×××w
useradd -g ×××w -M ×××w  # -M 不為 ×××w 用戶創建 home 目錄

禁止 ×××w 用戶通過 bash 登錄

vim /etc/passwd
修改  /bin/bash -> /sbin/nologin

Nginx

  1. 下載 && 解壓
    wget http://nginx.org/download/nginx-1.14.1.tar.gz
    tar -zxvf nginx-1.14.1.tar.gz
  2. 配置
    sudo ./configure --prefix=/usr/local/nginx --pid-path=/user/local/nginx/run/nginx.pid --user=×××w --group=×××w --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module  --with-pcre --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module

註:
--prefix:Nginx安裝目錄
--user:Nginx用戶
--group:Nginx用戶所屬組
--with-http_ssl_module:提供https支持
--with-http_flv_module:搭建flv視頻服務器使用的
--with-http_stub_status_module:開啟Stub Status模塊,該模塊會產生一個服務器狀態和信息頁
--with-http_gzip_static_module:開啟Gzip靜態模塊,該模塊用於發送預壓縮文件
--with-pcre:perl執行文件路徑

    完成後最後提示:
  nginx path prefix: "/usr/local/nginx"  # 安裝目錄

nginx binary file: "/usr/local/nginx/sbin/nginx" # 命令目錄
nginx modules path: "/usr/local/nginx/modules" # nginx 模塊目錄
nginx configuration prefix: "/usr/local/nginx/conf" # nginx 配置路徑
nginx configuration file: "/usr/local/nginx/conf/nginx.conf" # nginx配置
nginx pid file: "/usr/local/nginx/run/nginx.pid" # nginx 運行 pid
nginx error log file: "/usr/local/nginx/logs/error.log" # 錯誤日誌
nginx http access log file: "/usr/local/nginx/logs/access.log" # 請求日誌
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

3 . 編譯

sudo make 

4 . 安裝

make install

完成後查看 安裝是否成功 :/usr/local/nginx/sbin/nginx -v

nginx version: nginx/1.14.1
啟動後訪問: curl localhost

5 . 操作 nginx 方法
方法1. 直接 使用 /usr/local/nginx/sbin/nginx 命令:

/usr/local/nginx/sbin/nginx -t              # 檢測配置是否正確
/usr/local/nginx/sbin/nginx                 # 啟動 nginx ,啟動後查看進程 ps -ef| grep nginx
/user/local/nginx/sbin/nginx  -s  reload |stop|quit     # 平滑重啟|停止|退出 [ -s 向 nginx 命令腳本發送 信號]
kill -TERM 主進程號                 #  快速停止 nginx
pkill -9 nginx                      # 強制停止 nginx

方法2. 創建軟鏈
sudo ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx

nginx           # 啟動nginx
nginx -s stop       # 停止 nginx 
nginx -s reload # 平滑重啟 nginx
nginx -s quit       # 退出 nginx
nginx -c /usr/local/nginx/conf/nginx.conf       # 以指定配置啟動 nginx

方法3. systemd 管理 並設置開機自啟動
創建 nginx 服務

vim /lib/systemd/system/nginx.service

寫入 內容

[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target

文件說明
[Unit]部分
Description:描述服務
After:依賴,當依賴的服務啟動之後再啟動自定義的服務
[Service]部分
Type=forking是後臺運行的形式
ExecStart為服務的具體運行命令(需要根據路徑適配)
ExecReload為重啟命令(需要根據路徑適配)
ExecStop為停止命令(需要根據路徑適配)
PrivateTmp=True表示給服務分配獨立的臨時空間
註意:啟動、重啟、停止命令全部要求使用絕對路徑
[Install]部分
服務安裝的相關設置,可設置為多用戶
  • 開機啟動 任意目錄都可執行
    systemctl enable nginx.service # 開機啟動
    systemctl disable nginx # 禁止開啟啟動
  • 管理命令
    systemctl start nginx
    systemctl stop nginx
    systemctl reload nginx
    systemctl status nginx
    systemctl list-units --type=service # 查看所有服務

如果遇到錯誤: System has not been booted with systemd as init system (PID 1). Can‘t operat
意味著系統使用 sysvinit 運行,而不是 systemd 運行,請使用下面的方法

    先刪除之前的配置
    sudo rm -rf nginx.service
    sudo rm -rf nginx.service

方法4. 管理 ngixn 服務
sudo vim /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run ‘update-rc.d -f nginx defaults‘, or use the appropriate command on your
# distro. For CentOS/Redhat run: ‘chkconfig --add nginx‘

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

# Author:   licess
# website:  https://lnmp.org

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/run/$NAME.pid
if [ -s /bin/ss ]; then
    StatBin=/bin/ss
else
    StatBin=/bin/netstat
fi

case "$1" in
    start)
        echo -n "Starting $NAME... "

        if $StatBin -tnpl | grep -q nginx;then
            echo "$NAME (pid `pidof $NAME`) already running."
            exit 1
        fi

        $NGINX_BIN -c $CONFIGFILE

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    stop)
        echo -n "Stoping $NAME... "

        if ! $StatBin -tnpl | grep -q nginx; then
            echo "$NAME is not running."
            exit 1
        fi

        $NGINX_BIN -s stop

        if [ "$?" != 0 ] ; then
            echo " failed. Use force-quit"
            $0 force-quit
        else
            echo " done"
        fi
        ;;

    status)
        if $StatBin -tnpl | grep -q nginx; then
            PID=`pidof nginx`
            echo "$NAME (pid $PID) is running..."
        else
            echo "$NAME is stopped."
            exit 0
        fi
        ;;

    force-quit|kill)
        echo -n "Terminating $NAME... "

        if ! $StatBin -tnpl | grep -q nginx; then
            echo "$NAME is is stopped."
            exit 1
        fi

        kill `pidof $NAME`

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    restart)
        $0 stop
        sleep 1
        $0 start
        ;;

    reload)
        echo -n "Reload service $NAME... "

        if $StatBin -tnpl | grep -q nginx; then
            $NGINX_BIN -s reload
            echo " done"
        else
            echo "$NAME is not running, can‘t reload."
            exit 1
        fi
        ;;

    configtest)
        echo -n "Test $NAME configure files... "

        $NGINX_BIN -t
        ;;

    *)
        echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"
        exit 1
        ;;
esac

管理命令

service nginx start 
service nginx stop
service nginx status
service nginx reload
service nginx restart
service nginx configuretest

Mysql

安裝依賴

sudo apt install cmake g++ bison libncurses5-dev build-essential

  1. 下載解壓
    sudo wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.24.tar.gz
    sudo tar -zxvf mysql-boost-5.7.24.tar.gz
    cd /usr/local/src/mysql-boost-5.7.24
  2. 生成配置文件
    sudo cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
    -DMYSQL_DATADIR=/usr/local/mysql/data \
    -DSYSCONFDIR=/usr/local/mysql/etc \
    -DWITH_BOOST=/usr/local/src/mysql-boost-5.7.24/boost/boost_1_59_0 \
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
    -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
    -DWITH_PARTITION_STORAGE_ENGINE=1 \
    -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
    -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
    -DWITHOUT_FEDERATED_STORAGE_ENGINE=1 \
    -DDEFAULT_CHARSET=utf8 \
    -DDEFAULT_COLLATION=utf8_general_ci \
    -DWITH_EXTRA_CHARSETS=all \
    -DENABLED_LOCAL_INFILE=1 \
    -DWITH_READLINE=1 \
    -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
    -DMYSQL_TCP_PORT=3306 \
    -DMYSQL_USER=×××w \
    -DCOMPILATION_COMMENT="lq-edition" \
    -DENABLE_DTRACE=0 \
    -DOPTIMIZER_TRACE=1 \
    -DWITH_DEBUG=1
  3. 編譯、安裝
    sudo make && sudo make install
  4. 新建 用戶和用戶組 mysql:mysql
    sudo groupadd mysql
    sudo useradd -g mysql -M mysql
  5. 創建數據目錄 並添加 寫權限
    sudo mkdir -p /usr/local/mysql/data
    sudo chmod +w /usr/local/mysql
  6. 修改 mysql 目錄所有者
    sudo chown -R mysql:mysql /usr/local/mysql/
  7. 初始化 mysql ,並開啟 ssl 功能
    sudo /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
    sudo /usr/local/mysql/bin/mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
  8. 測試啟動 mysql
    sudo /usr/local/mysql/bin/mysqld_safe --user=mysql
  9. 啟動 mysql 服務 並修改密碼
    sudo /usr/local/mysql/support-files/mysql.server start
    sudo /usr/local/mysql/bin/mysql -u root -p‘password‘
    set password for ‘root‘@‘localhost‘ = password(‘123456‘);
    # 退出客戶端,重新連接進行測試
    mysql>quit;
    sudo /usr/local/mysql/bin/mysql -u root -p
    Enter password: # 輸入密碼
  10. 管理 mysql 服務
    sudo /usr/local/mysql/support-files/mysql.server stop
    sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
    此時可以用 service 進行管理,常用命令:
    service mysql start
    service mysql stop
    service mysql status
    service mysql reload
    service mysql restart
    service mysql help
  11. 添加客戶端命令?環境變量
    sudo vim ~/.bashrc
    # 在開頭添加
    export MYSQL_HOME=/usr/local/mysql
    export PATH=$PATH:$MYSQL_HOME/bin
    # 刷新環境變量
    source ~/.bashrc
    # 測試
    mysql -u root -p
  12. 允許遠程登錄
    # 客戶端登錄
    mysql -u root -p
    # 創建用戶並授權
    mysql>use mysql;
    mysql>grant all on . to ‘leesin‘@‘%‘ identified by ‘123456‘ WITH GRANT OPTION;
    # 刷新權限
    mysql>flush privileges;
    # 測試
    mysql -u leesin -p
  13. 配置文件不存在問題請參考:
    https://blog.csdn.net/qq_38545713/article/details/81868846

PHP

安裝依賴

sudo apt install libxml2 libxml2-dev openssl opensll-dev curl libcurl4-gnutls-dev libcurl4-openssl-dev libjpeg-dev libpng-dev libmcrypt-dev

  1. 下載解壓
    sudo wget http://cn2.php.net/get/php-7.2.12.tar.gz/from/this/mirror
    sudo tar -zxvf mirror && cd php-7.2.12
  2. 配置
    ./configure --help 查看所有選項
    切記不要有多余空格:
    sudo ./configure --prefix=/usr/local/php \
    --with-config-file-path=/usr/local/php/etc \
    --with-iconv-dir=/usr/local/lib \
    --enable-fpm \
    --enable-mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --with-openssl \
    --with-zlib \
    --enable-zip \
    --with-gd \
    --with-curl=/usr/bin/curl \
    --with-mysqli \
    --with-pdo-mysql

    tip: 如果報錯,curl : sudo ln -s /usr/include/x86_64-linux-gnu/curl /usr/include/curl
  3. 編譯 && 測試
    sudo make && sudo make test
  4. 安裝
    sudo make install
  5. 配置 PHP
    sudo cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
    sudo cp /usr/local/php/etc/php-fpm.d/×××w.conf.default /usr/local/php/etc/php-fpm.d/×××w.conf
    sudo cp /usr/local/src/php-7.2.12/php.ini-development /usr/local/php/etc/php.ini
    sudo mkdir /tmp/php && sudo touch /tmp/php/php-cgi.sock

    \# 解決訪問時無權限問題 + *6 connect() to unix:/tmp/php/php-cgi.sock failed (111: Connection refused) 問題
    \# 修改 /usr/local/php/etc/php-fpm.d/×××w.conf
    listen.owner = ×××w
    listen.group = ×××w
    listen.mode = 0660

    修改配置:
    /usr/local/php/etc/php-fpm.d/×××w.conf
    user = ×××w
    group = ×××w
    listen = /tmp/php/php-cgi.sock # sock 監聽,性能更高
    /usr/local/php/etc/php.ini
    cgi.fix_pathinfo=0

  6. 配置 php-fpm 服務管理
    sudo cp /usr/local/src/php-7.2.12/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    sudo chmod +x /etc/init.d/php-fpm
    chkconfig --add php-fpm

    此時就可以使用 service 進行管理了
    service php-fpm start
    serivce php-fpm stop
    service php-fpm status  
    service php-fpm reload
    service php-fpm restart
    service php-fpm help
    service php-fpm configtest
  7. 配置環境變量
    # 方法 1 - 創建軟鏈
    sudo ln -s /usr/local/php/bin/php /usr/bin/php
    php -v|m

    \# 方法 2 - 設置全局環境變量
    sudo vim /etc/profile
    export PATH=/usr/local/php/bin:$PATH
    source /etc/profile
    
    \# 方法 3 - 設置當前用戶環境變量
    sudo vim ~/.bashrc
    export PATH=/usr/local/php/bin:$PATH
    source ~/.bashrc
    php -v|m

    Nginx + php-fpm 配置

    sudo mkdir vhosts # 存放 細分 的配置
    sudo vim /usr/local/nginx/conf/nginx.conf
    http{
    ...
    include vhosts/*.conf
    }

具體 項目配置:
touch /usr/local/nginx/conf/vhosts/test.conf
server {
listen 80;
server_name lee.test.me;
#server_name 10.1.16.162;
charset utf-8;
index index.php index.html index.htm;
#access_log logs/host.access.log main;
root /Data/×××wroot/test/public;

            if (!-e $request_filename) {
                 rewrite  ^(.*)$  /index.php?s=/$1  last;
                 break;
            }  

            #error_page  404              /404.html;

            error_page   500 502 503 504  /50x.html;
                            location = /50x.html {
                            root   html;
            }  
            location ~ \.php$ {
                    #fastcgi_pass   127.0.0.1:9000;
                                    fastcgi_pass unix:/tmp/php/php-cgi.sock;
                    fastcgi_index  index.php;
                    include        fastcgi_params;
                    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
            }  

編譯安裝 Lnmp 並使用服務管理