在平时运维工作中,经常需要用到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选项参数】
# 查看是否已安装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 | +--------------------+ | @R_82_4045@ion_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)已完成。
[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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。