編譯安裝Lnmp并使用服務(wù)管理

Ubuntu 更換國(guó)內(nèi)鏡像源

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
deb http://archive.ubuntu.com/ubuntu/ trusty main universe restricted multiverse

安裝依賴(lài)

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

準(zhǔn)備用戶(hù)和用戶(hù)組

查看 ×××w 用戶(hù)是否存在 : groups ×××w

成都創(chuàng)新互聯(lián)公司主營(yíng)鹽湖網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,app軟件開(kāi)發(fā),鹽湖h5小程序開(kāi)發(fā)搭建,鹽湖網(wǎng)站營(yíng)銷(xiāo)推廣歡迎鹽湖等地區(qū)企業(yè)咨詢(xún)

groupadd ×××w
useradd -g ×××w -M ×××w  # -M 不為 ×××w 用戶(hù)創(chuàng)建 home 目錄

禁止 ×××w 用戶(hù)通過(guò) 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用戶(hù)
--group:Nginx用戶(hù)所屬組
--with-http_ssl_module:提供https支持
--with-http_flv_module:搭建flv視頻服務(wù)器使用的
--with-http_stub_status_module:開(kāi)啟Stub Status模塊,該模塊會(huì)產(chǎn)生一個(gè)服務(wù)器狀態(tài)和信息頁(yè)
--with-http_gzip_static_module:開(kāi)啟Gzip靜態(tài)模塊,該模塊用于發(fā)送預(yù)壓縮文件
--with-pcre:perl執(zhí)行文件路徑

    完成后最后提示:
  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 運(yùn)行 pid
nginx error log file: "/usr/local/nginx/logs/error.log" # 錯(cuò)誤日志
nginx http access log file: "/usr/local/nginx/logs/access.log" # 請(qǐng)求日志
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
啟動(dòng)后訪問(wèn): curl localhost

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

/usr/local/nginx/sbin/nginx -t              # 檢測(cè)配置是否正確
/usr/local/nginx/sbin/nginx                 # 啟動(dòng) nginx ,啟動(dòng)后查看進(jìn)程 ps -ef| grep nginx
/user/local/nginx/sbin/nginx  -s  reload |stop|quit     # 平滑重啟|停止|退出 [ -s 向 nginx 命令腳本發(fā)送 信號(hào)]
kill -TERM 主進(jìn)程號(hào)                 #  快速停止 nginx
pkill -9 nginx                      # 強(qiáng)制停止 nginx

方法2. 創(chuàng)建軟鏈
sudo ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx

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

方法3. systemd 管理 并設(shè)置開(kāi)機(jī)自啟動(dòng)
創(chuàng)建 nginx 服務(wù)

vim /lib/systemd/system/nginx.service

寫(xiě)入 內(nèi)容

[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

文件說(shuō)明
[Unit]部分
Description:描述服務(wù)
After:依賴(lài),當(dāng)依賴(lài)的服務(wù)啟動(dòng)之后再啟動(dòng)自定義的服務(wù)
[Service]部分
Type=forking是后臺(tái)運(yùn)行的形式
ExecStart為服務(wù)的具體運(yùn)行命令(需要根據(jù)路徑適配)
ExecReload為重啟命令(需要根據(jù)路徑適配)
ExecStop為停止命令(需要根據(jù)路徑適配)
PrivateTmp=True表示給服務(wù)分配獨(dú)立的臨時(shí)空間
注意:?jiǎn)?dòng)、重啟、停止命令全部要求使用絕對(duì)路徑
[Install]部分
服務(wù)安裝的相關(guān)設(shè)置,可設(shè)置為多用戶(hù)
  • 開(kāi)機(jī)啟動(dòng) 任意目錄都可執(zhí)行
    systemctl enable nginx.service # 開(kāi)機(jī)啟動(dòng)
    systemctl disable nginx # 禁止開(kāi)啟啟動(dòng)
  • 管理命令
    systemctl start nginx
    systemctl stop nginx
    systemctl reload nginx
    systemctl status nginx
    systemctl list-units --type=service # 查看所有服務(wù)

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

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

方法4. 管理 ngixn 服務(wù)
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
執(zhí)行提示如下錯(cuò)誤
Failed to start nginx.service: Unit nginx.service is masked.

解決方案:
systemctl unmask nginx.service

服務(wù)隨系統(tǒng)啟動(dòng)

sudo apt install sysv-rc-conf
sudo sysv-rc-conf nginx on

MySQL

安裝依賴(lài)

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_BOOST=boost
    -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. 新建 用戶(hù)和用戶(hù)組 mysql:mysql
    sudo groupadd mysql
    sudo useradd -g mysql -M mysql
  5. 創(chuàng)建數(shù)據(jù)目錄 并添加 寫(xiě)權(quán)限
    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 ,并開(kāi)啟 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. 測(cè)試啟動(dòng) mysql
    sudo /usr/local/mysql/bin/mysqld_safe --user=mysql
  9. 啟動(dòng) mysql 服務(wù) 并修改密碼
    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');
    # 退出客戶(hù)端,重新連接進(jìn)行測(cè)試
    mysql>quit;
    sudo /usr/local/mysql/bin/mysql -u root -p
    Enter password: # 輸入密碼
  10. 管理 mysql 服務(wù)
    sudo /usr/local/mysql/support-files/mysql.server stop
    sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
    sudo chmod +x /etc/init.d/mysql
    sudo systemctl unmask mysql.service
    sudo sysv-rc-conf mysql on
    此時(shí)可以用 service 進(jìn)行管理,常用命令:
    service mysql start 
    service mysql stop
    service mysql status
    service mysql reload
    service mysql restart
    service mysql help
  1. 添加客戶(hù)端命令?環(huán)境變量
    sudo vim ~/.bashrc
    # 在開(kāi)頭添加
    export MYSQL_HOME=/usr/local/mysql
    export PATH=$PATH:$MYSQL_HOME/bin
    # 刷新環(huán)境變量
    source ~/.bashrc
    # 測(cè)試
    mysql -u root -p
  2. 允許遠(yuǎn)程登錄
    # 客戶(hù)端登錄
    mysql -u root -p
    # 創(chuàng)建用戶(hù)并授權(quán)
    mysql>use mysql;
    mysql>grant all on . to 'leesin'@'%' identified by '123456' WITH GRANT OPTION;
    # 刷新權(quán)限
    mysql>flush privileges;
    # 測(cè)試
    mysql -u leesin -p
  3. 配置文件不存在問(wèn)題請(qǐng)參考:
    https://blog.csdn.net/qq_38545713/article/details/81868846

PHP

安裝依賴(lài)

sudo apt install libxml2 libxml2-dev openssl libssl-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 查看所有選項(xiàng)
    切記不要有多余空格:
    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: 如果報(bào)錯(cuò),curl : sudo ln -s /usr/include/x86_64-linux-gnu/curl /usr/include/curl
  3. 編譯 && 測(cè)試
    sudo make && sudo make test

    tips: 錯(cuò)誤: sapi/cli/php not found  
                    make ZEND_EXTRA_LIBS='-liconv'
                    ln -s /usr/local/lib/libiconv.so.2 /usr/lib64/
    
                    或者:
                        sudo vim Makefile
                                    EXTRA_LIBS = 新增  -liconv
  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

    \# 解決訪問(wèn)時(shí)無(wú)權(quán)限問(wèn)題 + *6 connect() to unix:/tmp/php/php-cgi.sock failed (111: Connection refused) 問(wèn)題
    \# 修改 /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 監(jiān)聽(tīng),性能更高
    /usr/local/php/etc/php.ini
    cgi.fix_pathinfo=0

  6. 配置 php-fpm 服務(wù)管理
    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
    sudo systemctl unmask php-fpm.service # 添加服務(wù)
    sudo sysv-rc-conf php-fpm on # 開(kāi)機(jī)啟動(dòng)
    #chkconfig --add php-fpm

    此時(shí)就可以使用 service 進(jìn)行管理了
    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. 配置環(huán)境變量
    # 方法 1 - 創(chuàng)建軟鏈
    sudo ln -s /usr/local/php/bin/php /usr/bin/php
    php -v|m

    \# 方法 2 - 設(shè)置全局環(huán)境變量
    sudo vim /etc/profile
    export PATH=/usr/local/php/bin:$PATH
    source /etc/profile
    
    \# 方法 3 - 設(shè)置當(dāng)前用戶(hù)環(huán)境變量
    sudo vim ~/.bashrc
    export PATH=/usr/local/php/bin:$PATH
    source ~/.bashrc
    php -v|m

    Nginx + php-fpm 配置

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

具體 項(xiàng)目配置:
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并使用服務(wù)管理
分享地址:http://muchs.cn/article24/jpesje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、外貿(mào)建站、定制開(kāi)發(fā)、ChatGPT電子商務(wù)、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)