手动编译部署LNMP环境(CentOS7.5+Nginx-1.18.0+MySQL-5.7.30+PHP-7.4.14)

时间:2023-03-09 13:03:17
手动编译部署LNMP环境(CentOS7.5+Nginx-1.18.0+MySQL-5.7.30+PHP-7.4.14)

在平时运维工作中,经常需要用到LNMP应用框架。LNMP环境是指在Linux系统下,由Nginx + MySQL + PHP组成的网站服务器架构。

可参考前面的文章:

 (1) CentOS7.5 (系统最小化安装)准备环境。

# 更改主机名
[root@localhost ~]# hostnamectl set-hostname --static lnmp-01 && exec bash # 关闭SELinux
[root@lnmp-01 ~]# sed -i '7s/enforcing/disabled/' /etc/selinux/config
[root@lnmp-01 ~]# setenforce 0
[root@lnmp-01 ~]# getenforce
Permissive # 关闭firewalld
[root@lnmp-01 ~]# systemctl stop firewalld && systemctl disable firewalld # 查看内核版本
[root@lnmp-01 ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@lnmp-01 ~]# uname -r
3.10.0-862.el7.x86_64 # 安装基础软件
[root@lnmp-01 ~]# yum install -y vim wget lsof net-tools tree curl lrzsz

(2) 安装Nginx-1.18.0   【官方下载站点:源码包rpm包,版本号A.B.C,B是偶数为稳定版;奇数则为开发版】【官方站点:configure命令参数

# 查看是否已安装,有则卸载
[root@lnmp-01 ~]# yum list installed | egrep nginx
[root@lnmp-01 ~]# yum remove -y nginx.x86_64 nginx-xxx # 创建nginx用户和目录
[root@lnmp-01 ~]# useradd -M -s /sbin/nologin nginx
[root@lnmp-01 ~]# mkdir -p /data/apps/nginx # 安装nginx 编译所需依赖
[root@lnmp-01 ~]# yum -y install gcc gcc-c++ make zlib-devel pcre-devel openssl-devel
[root@lnmp-01 ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@lnmp-01 ~]# tar -xf nginx-1.18.0.tar.gz
[root@lnmp-01 ~]# ls
nginx-1.18.0 nginx-1.18.0.tar.gz
[root@lnmp-01 ~]# cd nginx-1.18.0
# 选择相应的编译参数【官方站点:configure命令参数】
[root@lnmp-01 nginx-1.18.0]# ./configure --help
[root@lnmp-01 nginx-1.18.0]# ./configure --prefix=/data/apps/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module 编译参数解析
--prefix=      安装目录路径
--user=       用户
--group=      组
--with-http_ssl_module    支持HTTPS协议
--with-http_flv_module    支持Flash视频  
--with-http_stub_status_module  提供访问的状态信息
--with-http_gzip_static_module  支持gzip压缩文件 [root@lnmp-01 nginx-1.18.0]# make -j 4 && make install    #make -j 4 表示开4核同时进行编译: cat /proc/cpuinfo| grep "processor"| wc -l # 将nginx命令添加环境变量
[root@lnmp-01 ~]# echo "export PATH=/data/apps/nginx/sbin:$PATH" >> /etc/profile.d/nginx.sh
[root@lnmp-01 ~]# source /etc/profile.d/nginx.sh # nginx命令语法及参数
[root@lnmp-01 ~]# nginx -h
nginx version: nginx/1.18.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives] Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /data/apps/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file # 测试启动nginx
[root@lnmp-01 ~]# nginx
[root@lnmp-01 ~]# netstat -ntupl | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1098/nginx: master # 访问nginx测试页面
[root@lnmp-01 ~]# curl 127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>    
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>    # 安装正常会显示如下内容
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html> [root@lnmp-01 ~]# nginx -s stop # 添加systemd管理
[root@lnmp-01 ~]# vim /usr/lib/systemd/system/nginx.service
# 添加以下内容
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target [Service]
Type=forking
PIDFile=/data/apps/nginx/logs/nginx.pid    # 自定义项
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /data/apps/nginx/logs/nginx.pid    # 自定义项
ExecStartPre=/data/apps/nginx/sbin/nginx -t -c /data/apps/nginx/conf/nginx.conf    # 自定义项
ExecStart=/data/apps/nginx/sbin/nginx    # 自定义项
ExecReload=/data/apps/nginx/sbin/nginx -s reload    # 自定义项
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true [Install]
WantedBy=multi-user.target # 启动nginx
[root@lnmp-01 ~]# systemctl daemon-reload
[root@lnmp-01 ~]# systemctl start|stop|restart|reload nginx
[root@lnmp-01 ~]# netstat -nutpl | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9714/nginx: master
[root@lnmp-01 ~]# ps -ef |grep nginx
root 9714 1 0 11:46 ? 00:00:00 nginx: master process /data/apps/nginx/sbin/nginx
nginx 9715 9714 0 11:46 ? 00:00:00 nginx: worker process # 设置开机自启动
[root@lnmp-01 ~]# systemctl enable nginx
[root@lnmp-01 ~]# systemctl list-unit-files | grep nginx
nginx.service enabled

 (3) MySQL-5.7.30 【官方下载站点:源码/rpm包】【下载站点:boost_1_59_0】【官方站点:cmake选项参数

手动编译部署LNMP环境(CentOS7.5+Nginx-1.18.0+MySQL-5.7.30+PHP-7.4.14)

# 查看是否已安装mariadb或mysql,有则卸载
[root@lnmp-01 ~]# yum list installed | egrep 'mariadb|mysql'
mariadb-libs.x86_64 1:5.5.56-2.el7 @anaconda
[root@lnmp-01 ~]# yum remove -y mariadb-libs.x86_64 # 安装mysql编译所需依赖
[root@lnmp-01 ~]# yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel # 若需下载其他版本,修改最后段版本号(30改为34)即可;推荐到官方站点下载
[root@lnmp-01 ~]# wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.30.tar.gz    # 地址1
[root@lnmp-01 ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.30.tar.gz  # 地址2
[root@lnmp-01 ~]# wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
---------------------------------# 或下载带boost的包 ----------------------------------------------------
[root@lnmp-01 ~]# wget https://cdn.mysql.com/archives/mysql-5.7/mysql-boost-5.7.30.tar.gz  # 地址1
[root@lnmp-01 ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.30.tar.gz  # 地址2
--------------------------------------------------------------------------------------------------------
[root@lnmp-01 ~]# tar -xf mysql-5.7.30.tar.gz
[root@lnmp-01 ~]# tar -xf boost_1_59_0.tar.gz
[root@lnmp-01 ~]# ls
boost_1_59_0 boost_1_59_0.tar.gz mysql-5.7.30 mysql-5.7.30.tar.gz
[root@lnmp-01 ~]# cd mysql-5.7.30
# 选择相应的编译参数【官方站点:cmake选项参数】
[root@lnmp-01 mysql-5.7.30]# cmake --help
[root@lnmp-01 mysql-5.7.30]# cmake . \
-DCMAKE_INSTALL_PREFIX=/data/apps/mysql \
-DMYSQL_DATADIR=/data/apps/mysql/data_db \
-DSYSCONFDIR==/etc \
-DDEFAULT_CHARSET=utf8mb4 \
-DWITH_SYSTEMD=bool \
-DENABLED_LOCAL_INFILE=bool \
-DWITH_BOOST=/root/boost_1_59_0 \
-DWITH_EXTRA_CHARSETS=all ======= 等待几分钟完成编译,末尾正常输出以下内容=========
......
-- INSTALL mysqlclient.pc lib/pkgconfig
-- Skipping deb packaging on unsupported platform .
-- CMAKE_BUILD_TYPE: RelWithDebInfo
-- COMPILE_DEFINITIONS: _GNU_SOURCE;_FILE_OFFSET_BITS=64;HAVE_CONFIG_H
-- CMAKE_C_FLAGS: -Wall -Wextra -Wformat-security -Wvla -Wwrite-strings -Wdeclaration-after-statement
-- CMAKE_CXX_FLAGS: -Wall -Wextra -Wformat-security -Wvla -Woverloaded-virtual -Wno-unused-parameter
-- CMAKE_C_LINK_FLAGS:
-- CMAKE_CXX_LINK_FLAGS:
-- CMAKE_C_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
-- CMAKE_CXX_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
-- Configuring done
-- Generating done
-- Build files have been written to: /root/mysql-5.7.30 参数说明:
-DCMAKE_INSTALL_PREFIX:mysql安装目录
-DMYSQL_DATADIR:数据存放目录
-DEFAULT_CHARSET:数据库默认字符编码
-DWITH_SYSTEMD:提供systemd脚本
-DENABLED_LOCAL_INFILE:允许从本文件导入数据
-DWITH_BOOST: boost源码路径
-DWITH_EXTRA_CHARSETS:支持额外字符集 ----------------------------------------------------------------------
# 若cmake出错了,需根据下面操作,然后再重新编译安装。
[root@wencheng mysql-5.7.30]# find / -iname CMakeCache.txt
[root@wencheng mysql-5.7.30]# make clean
[root@wencheng mysql-5.7.30]# rm -f CMakeCache.txt
---------------------------------------------------------------------- [root@lnmp-01 mysql-5.7.30]# make -j 4 && make install    # make -j 4 表示开4核同时进行编译: cat /proc/cpuinfo| grep "processor"| wc -l [root@lnmp-01 ~]# /data/apps/mysql/bin/mysql -V
/data/apps/mysql/bin/mysql Ver 14.14 Distrib 5.7.30, for Linux (x86_64) using EditLine wrapper # 添加环境变量
[root@lnmp-01 ~]# echo "PATH=/data/apps/mysql/bin:$PATH" >> /etc/profile.d/mysql.sh
[root@lnmp-01 ~]# source /etc/profile.d/mysql.sh # 创建mysql用户和目录并设置权限
[root@lnmp-01 ~]# useradd -M -s /sbin/nologin mysql
[root@lnmp-01 ~]# mkdir -p /data/apps/mysql/{data_db,logs,tmp}
[root@lnmp-01 ~]# chown -R mysql:mysql /data/apps/mysql/{data_db,logs,tmp} # 创建配置文件my.cnf
[root@lnmp-01 ~]# vim /etc/my.cnf
[client]
port = 3306
default-character-set = utf8mb4
socket = /data/apps/mysql/tmp/mysql.sock [mysql]
port = 3306
default-character-set = utf8mb4
socket = /data/apps/mysql/tmp/mysql.sock [mysqld]
port = 3306
server-id = 1
user = mysql
basedir = /data/apps/mysql
datadir = /data/apps/mysql/data_db
character_set_server = utf8mb4
collation-server = utf8mb4_general_ci
socket = /data/apps/mysql/tmp/mysql.sock log-error = /data/apps/mysql/logs/mysql-error.log
pid-file = /data/apps/mysql/tmp/mysqld.pid # 执行初始化
[root@lnmp-01 ~]# /data/apps/mysql/bin/mysqld --initialize-insecure --basedir=/data/apps/mysql --datadir=/data/apps/mysql/data_db --user=mysql   #(5.7以上版本)
--------- 正常输出以下信息 -----------
2021-06-22T11:38:05.991267Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-06-22T11:38:06.327772Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-06-22T11:38:06.367358Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-06-22T11:38:06.424912Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 4fc67845-d34e-11eb-9ce0-000c29ceb2c0.
2021-06-22T11:38:06.426105Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-06-22T11:38:06.590676Z 0 [Warning] CA certificate ca.pem is self signed.
2021-06-22T11:38:06.643809Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option. # 添加systemd管理
[root@lnmp-01 ~]# cp /data/apps/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
[root@lnmp-01 ~]# grep -Ev '^$|#' /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/data/apps/mysql/tmp/mysqld.pid    # 自定义项
TimeoutSec=0
PermissionsStartOnly=true
ExecStartPre=/data/apps/mysql/bin/mysqld_pre_systemd
ExecStart=/data/apps/mysql/bin/mysqld --daemonize --pid-file=/data/apps/mysql/tmp/mysqld.pid $MYSQLD_OPTS # 自定义项
EnvironmentFile=-/etc/sysconfig/mysql
LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false # 启动mysql
[root@lnmp-01 ~]# systemctl daemon-reload
[root@lnmp-01 ~]# systemctl start|stop|restart|reload mysqld
[root@lnmp-01 ~]# netstat -nutpl | grep mysql
tcp6 0 0 :::3306 :::* LISTEN 30472/mysqld
[root@lnmp-01 ~]# ps -ef |grep mysql
mysql 30472 1 0 12:33 ? 00:00:00 /data/apps/mysql/bin/mysqld --daemonize --pid-file=/data/apps/mysql/tmp/mysqld.pid # 登录mysql
[root@lnmp-01 ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.30 Source distribution Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec) # 设置开机自启动
[root@lnmp-01 ~]# systemctl enable mysqld
[root@lnmp-01 ~]# systemctl list-unit-files | grep mysql
mysqld.service enabled

 (4) PHP-7.4.14 【官方下载站点:源码包】【官方站点:comfigure选项参数

# 查看是否已安装php,有则卸载
[root@lnmp-01 ~]# yum list installed | grep php # 安装mysql编译所需依赖
[root@lnmp-01 ~]# yum install -y epel-release
[root@lnmp-01 ~]# yum install -y bzip2 bzip2-devel libmcrypt-devel openssl-devel libxml2-devel sqlite-devel libcurl-devel libpng-devel libjpeg libjpeg-devel oniguruma oniguruma-devel # 下载指定版本
[root@lnmp-01 ~]# wget https://www.php.net/distributions/php-7.4.14.tar.gz
[root@lnmp-01 ~]# tar -xf php-7.4.14.tar.gz
[root@lnmp-01 ~]# cd php-7.4.14 # 选择相应的编译参数【官方站点:cmake选项参数】
[root@lnmp-01 php-7.4.14]# ./configure --help
[root@lnmp-01 php-7.4.14]# ./configure --prefix=/data/apps/php \
--with-config-file-path=/etc \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-fpm \
--with-openssl \
--enable-mbstring \
--enable-sockets \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-libxml-dir=/usr \
--enable-xml \
--with-zlib \
--with-mcrypt \
--with-bz2 \
--with-mhash ======= 等待几分钟完成编译,末尾正常输出以下内容=========
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+
Thank you for using PHP. 参数说明:
--prefix:安装目录
--with-config-file-path:配置文件位置
--with-fpm-user:进程管理器进程守护者
--with-fpm-group:进程管理器进程守护者的用户组
--enable-fpm:进程管理器
 ......
--------------------------------------------------
# 若configure出错了,需清除原编译,然后再重新编译安装。
[root@lnmp-01 php-7.4.14]# make clean
-------------------------------------------------- [root@lnmp-01 php-7.4.14]# make -j 4 && make install    # make -j 4 表示开4核同时进行编译: cat /proc/cpuinfo| grep "processor"| wc -l [root@lnmp-01 ~]# /data/apps/php/bin/php -v
PHP 7.4.14 (cli) (built: Sep 8 2021 09:41:51) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologi # 添加环境变量
[root@lnmp-01 ~]# echo "PATH=/data/apps/php/bin:$PATH" >> /etc/profile.d/php.sh
[root@lnmp-01 ~]# echo "PATH=/data/apps/php/sbin:$PATH" >> /etc/profile.d/php.sh
[root@lnmp-01 ~]# source /etc/profile.d/php.sh
-----------------------------------------------------------------
# 或可创建软连接
[root@lnmp-01 ~]# ln -s /data/apps/php/bin/php /usr/local/bin/php
[root@lnmp-01 ~]# ln -s /data/apps/php/sbin/php-fpm /usr/local/bin/php-fpm # 创建php用户并设置目录权限
[root@lnmp-01 ~]# useradd -M -s /sbin/nologin www
[root@lnmp-01 ~]# chown -R www:www /data/apps/php # 生成php服务脚本
[root@lnmp-01 ~]# cp ~/php-7.4.14/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpmd
[root@lnmp-01 ~]# chmod +x /etc/init.d/php-fpmd # 设置php-fpm配置文件
[root@lnmp-01 ~]# cp /data/apps/php/etc/php-fpm.conf.default /data/apps/php/etc/php-fpm.conf
[root@lnmp-01 ~]# cp /data/apps/php/etc/php-fpm.d/www.conf.default /data/apps/php/etc/php-fpm.d/www.conf # 测试php配置文件语法是否正确
[root@lnmp-01 ~]# php-fpm -t
[08-Sep-2021 10:45:52] NOTICE: configuration file /data/apps/php/etc/php-fpm.conf test is successful # 启动php
[root@lnmp-01 ~]# /etc/init.d/php-fpmd start|stop|force-quit|restart|reload|status|configtest
[root@lnmp-01 ~]# netstat -nuptl | grep php
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 63705/php-fpm: mast
[root@lnmp-01 ~]# ps -ef | grep php
root 63705 1 0 10:49 ? 00:00:00 php-fpm: master process (/data/apps/php/etc/php-fpm.conf)
www 63706 63705 0 10:49 ? 00:00:00 php-fpm: pool www
www 63707 63705 0 10:49 ? 00:00:00 php-fpm: pool www # 添加systemd管理
[root@lnmp-01 ~]# vim /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target [Service]
Type=simple
PIDFile=/data/apps/php/var/run/php-fpm.pid  # 自定义项
ExecStart=/data/apps/php/sbin/php-fpm --nodaemonize --fpm-config /data/apps/php/etc/php-fpm.conf  # 自定义项
ExecReload=/bin/kill -USR2 $MAINPID [Install]
WantedBy=multi-user.target # 设置开机自启动
[root@lnmp-01 ~]# systemctl enable php-fpm
[root@lnmp-01 ~]# systemctl list-unit-files | grep php
php-fpm.service enable

至此,手动编译部署LNMP环境(CentOS7.5+Nginx-1.18.0+MySQL-5.7.30+PHP-7.4.14)已完成。

 :YUM安装php【下载rpm包

[root@lnmp-01 ~]# yum install epel-release
[root@lnmp-01 ~]# rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
# 查看php所有版本
[root@lnmp-01 ~]# ls /etc/yum.repos.d/*php*
/etc/yum.repos.d/remi-php54.repo /etc/yum.repos.d/remi-php72.repo /etc/yum.repos.d/remi-php80.repo
/etc/yum.repos.d/remi-php70.repo /etc/yum.repos.d/remi-php73.repo /etc/yum.repos.d/remi-php81.repo
/etc/yum.repos.d/remi-php71.repo /etc/yum.repos.d/remi-php74.repo
# 选择适合的版本,这里选择php74
[root@lnmp-01 ~]# yum --enablerepo=remi install -y php74-php
[root@lnmp-01 ~]# yum --enablerepo=remi install php74-php php74-php-gd php74-php-xml php74-php-sockets php74-php-session php74-php-snmp php74-php-mysql
[root@lnmp-01 ~]# php74 -v
PHP 7.4.23 (cli) (built: Aug 24 2021 16:33:30) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies