重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

编译安装Lnmp并使用服务管理

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

安装依赖

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

准备用户和用户组

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

成都创新互联公司主营盐湖网站建设的网络公司,主营网站建设方案,app软件开发,盐湖h5小程序开发搭建,盐湖网站营销推广欢迎盐湖等地区企业咨询

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
执行提示如下错误
Failed to start nginx.service: Unit nginx.service is masked.

解决方案:
systemctl unmask nginx.service

服务随系统启动

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

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_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. 新建 用户和用户组 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
    sudo chmod +x /etc/init.d/mysql
    sudo systemctl unmask mysql.service
    sudo sysv-rc-conf mysql on
    此时可以用 service 进行管理,常用命令:
    service mysql start 
    service mysql stop
    service mysql status
    service mysql reload
    service mysql restart
    service mysql help
  1. 添加客户端命令☞环境变量
    sudo vim ~/.bashrc
    # 在开头添加
    export MYSQL_HOME=/usr/local/mysql
    export PATH=$PATH:$MYSQL_HOME/bin
    # 刷新环境变量
    source ~/.bashrc
    # 测试
    mysql -u root -p
  2. 允许远程登录
    # 客户端登录
    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
  3. 配置文件不存在问题请参考:
    https://blog.csdn.net/qq_38545713/article/details/81868846

PHP

安装依赖

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 查看所有选项
    切记不要有多余空格:
    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

    tips: 错误: 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

    \# 解决访问时无权限问题 + *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
    sudo systemctl unmask php-fpm.service # 添加服务
    sudo sysv-rc-conf php-fpm on # 开机启动
    #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并使用服务管理
文章分享:http://cqcxhl.cn/article/jpesje.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP