编译安装lnmp并使用服务管理(代码片段)

author author     2023-01-23     696

关键词:

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编译安装(2020)(代码片段)

...解压并进入安装目录tarzxvfnginx-1.12.0.tar.gzcdnginx-1.12.0  4)编译安装./configure--prefix=/usr/local/nginx--with-http_ssl_modulemake&&makeinstall-j2  5)启动并查看服务/usr/local/nginx/sbin/nginxps-ef|grepnginx   6)加入开机自启动vim/etc/rc.local添... 查看详情

lnmp架构安装及搭建discuz论坛(代码片段)

.../opt/目录下并解压3.安装环境依赖包4.创建运行用户、组5.编译安装nginx6.优化路径7.添加nginx系统服务8.检查systemctl命令是否能启动9.在浏览器中访问验证服务三、安装MySQL1.将软件包拖入/opt/并解压2.安装MySQL环境依赖包3.创建运行用... 查看详情

编辑安装lnmp(代码片段)

....安装Nginx服务关闭防火墙1.安装依赖包2.创建运行用户3、编译安装4、优化路径5、添加Nginx系统服务三.安装MySQL服务1、安装Mysql环境依赖包2.创建运行用户3.编译安装4、修改mysql配置文件5、更改mysql安装目录和配置文件的属主属组6.... 查看详情

20使用lnmp架构部署动态网站环境(代码片段)

...”安装服务程序)。使用源码包安装服务程序时会有一个编译过程,因此可以更好地适应安装主机的系统环境,运行效率和优化程度都会强于使用RPM软件包安装的服务程序。也就是说,可以将采用源码包安装服务程序的方式看作... 查看详情

lnmp源码安装全过程(代码片段)

...在使用源码包安装服务程序之前,首先要让安装主机具备编译程序源码的环境,他需要具备C语言、C++语言、Perl语言的编译器,以及各种常见的编译支持函数库程序。因此请先配置妥当Yum软件仓库,然后吧下面列出的软件包都统... 查看详情

lnmp架构搭建(一键部署)(代码片段)

...录下 nginx服务的安装安装依赖包  创建运行用户、组 编译安装Nginx​编辑 添加Nginx系统服务  nginx结果验证 mysql搭建安装Mysql环境依赖包 创建程序用户,便于准确控制访问用户 ​编辑 配置软件模块并编译安装 ​编辑... 查看详情

php介绍及安装(代码片段)

...一步:上传源码包解压,并安装依赖第二步:编译参数配置+编译安装第三步:让PHP配置文件生效第四步:配置php-fpm的服务脚本并启动CentOS6下:CentOS 查看详情

zabbix源码安装(代码片段)

...数据LAMP或LNMP只是提供一个网页监控平台的使用环境。(编译安装,一些老服务期建议yum安装;要求系统纯净) 1.1.1  查看详情

lnmp服务搭建(代码片段)

搭建环境:一台主机192.168.56.12安装nginx,mysql,php关闭防火墙,selinux1.编译安装nginx//创建系统用户[[email protected]~]#useradd-r-M-s/sbin/nologinnginx//安装依赖环境[[email protected]~]#yum-yinstallpcre-developensslopenssl-d 查看详情

lnmp环境编译安装(代码片段)

先安装nginx我的lnmp时编译安装的,下载Nginx安装包wgethttp://nginx.org/download/nginx-1.13.4.tar.gz关闭selinux和防火墙setenforce0serviceiptablesstop检查安装依赖项 (执行下面的命令安装nginx的依赖库)yum-yinstallgccpcrepcre-develzlibzlib-develo 查看详情

lnmp架构(代码片段)

...工作1、将安装包传到/opt/目录下2、搭建本地yum仓库三、编译安装nginx服务四、编译安装mysql服务五、编译安装PHP解析环境六、部署Discuz社区论坛七、装完后使用浏览器访问一、LNMP架构介绍    LNMP代表的就是:Linux系统下Nginx... 查看详情

lnmp分离式部署(代码片段)

...名称,然后进行安装。如:!(2)不需要作软连接,直接编译安装,并进行检查 make&&makei 查看详情

搭建lnmp环境(代码片段)

...装环境。一.gcc安装安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,则需要安装:yuminstallgcc-c++二.PCREpcre-devel安装PCRE(PerlCompatibleRegularExpr 查看详情

lnmp架构介绍

...加到服务并重新启动PHP安装步骤2进入PHP目录先清除之前编译的文件makeclean编译文件nable不加无法启动make&&makeinstall查看文件-t测试配置文件的语法拷贝配置文件如果是开发环境使用development如果是生产环境使用production编辑文... 查看详情

源码编译lnmp架构(代码片段)

众所周知,LAMP平台是目前应用最为广泛的网站服务器架构,其中的“A”对应着Web服务软件ApacheHTTPServer。随着Nginx在企业中的使用越来越多,LNMP(或LEMP)架构也受到越来越多Linux系统工程师的青睐,其中的“E”就来自于Nginx的发音[... 查看详情

lnmp架构概述及相关服务的搭建(代码片段)

目录一,LNMP架构的部署二,手工编译安装Nginx服务1,关闭防火墙和安全机制 2,安装环境依赖包,并且创建程序用户 3,编译安装nginx 4,路径优化 5,启动,停止nginx服务 6,添加nginx系统... 查看详情

配置lnmp发布论坛网站

...)挂载程序光盘​2)挂载程序盘到/mnt​3)配置MySQL​4)编译安装MySQL​3.生成MySQL主配置文件和服务控制文件优化MySQL命令​1)生成mysql主配置文件​2)生成MySQL服务控制文件添加执行权限​3)添加系统服务设置开机自动启动​... 查看详情

企业架构之lnmp(代码片段)

目录:一、编译安装MySQL1.脚本实现安装及其初始化:二、nginx的相关介绍1.nginx源码编译安装:2.nginx二进制执行文件的操作参数:3.nginx服务脚本配置:1).服务脚本测试:三、PHP1.介绍:2.脚本安装php࿱... 查看详情