LNMP分离部署

image-20221012090503173

环境准备:

主机名 系统版本 IP地址
nginx Red Hat Enterprise Linux release 8.2 192.168.100.110/24
php Red Hat Enterprise Linux release 8.2 192.168.100.123/24
mysql Red Hat Enterprise Linux release 8.2 192.168.100.124/24

三台服务器全部关闭防火墙

[root@nginx ]# systemctl stop firewalld.service 
[root@nginx ]# systemctl disable firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@nginx ]# setenforce 0

[root@php ~]# systemctl stop firewalld.service 
[root@php ~]# systemctl disable firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@php ~]# setenforce 0

[root@mysql ~]# systemctl stop firewalld.service 
[root@mysql ~]# systemctl disable firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@mysql ~]# setenforce 0

部署nginx

//创建nginx用户
[root@nginx ~]# id nginx
id: “nginx”:无此用户
[root@nginx ~]# useradd -M -r -s /sbin/nologin nginx
[root@nginx ~]# id nginx
uid=975(nginx) gid=973(nginx) 组=973(nginx)
//安装依赖包
[root@nginx ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
//安装开发工具包
[root@nginx ~]# yum -y groups mark install 'Development Tools'
//创建日志存放目录
[root@nginx ~]# mkdir /var/log/nginx
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx
[root@nginx ~]# cd /usr/src/
//下载nginx包
[root@nginx src]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
[root@nginx src]# tar xf nginx-1.22.0.tar.gz 
[root@nginx src]# ls
debug  kernels  nginx-1.22.0  nginx-1.22.0.tar.gz
[root@nginx src]# cd nginx-1.22.0
[root@nginx nginx-1.22.0]# ./configure  \
--prefix=/usr/local/nginx  \
--user=nginx  \
--group=nginx  \
--with-debug  \
--with-http_ssl_module  \
--with-http_realip_module  \
--with-http_image_filter_module  \
--with-http_gunzip_module  \
--with-http_gzip_static_module  \
--with-http_stub_status_module  \
--http-log-path=/var/log/nginx/access.log  \
--error-log-path=/var/log/nginx/error.log
[root@nginx nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
//配置环境变量
[root@nginx ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx ~]# source /etc/profile.d/nginx.sh
//查看nginx版本
[root@nginx ~]# nginx -v
nginx version: nginx/1.22.0
//创建网页测试文件
[root@nginx ~]# cat > /usr/local/nginx/html/index.php << EOF
<?php
phpinfo();
?>
EOF
//启动nginx
[root@nginx ~]# nginx 
[root@nginx ~]# ss -tanl
State                Recv-Q                Send-Q                               Local Address:Port                               Peer Address:Port               
LISTEN               0                     128                                        0.0.0.0:111                                     0.0.0.0:*                  
LISTEN               0                     128                                        0.0.0.0:80                                      0.0.0.0:*                  
LISTEN               0                     32                                   192.168.122.1:53                                      0.0.0.0:*                  
LISTEN               0                     128                                        0.0.0.0:22                                      0.0.0.0:*                

部署mysql

//安装依赖包
[root@mysql ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs
//创建mysql用户
[root@mysql ~]# useradd -M -r -s /sbin/nologin mysql
[root@mysql ~]# id mysql 
uid=988(mysql) gid=984(mysql) groups=984(mysql)
//下载mysql软件包
[root@mysql ~]# cd /usr/src/
[root@mysql src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@mysql src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@mysql local]# ln -sv mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.38-linux-glibc2.12-x86_64/'
//解决权限问题
[root@mysql local]# chown -R mysql.mysql mysql
[root@mysql local]# ll
lrwxrwxrwx. 1 root root  36 Oct 11 22:41 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 Oct 11 22:41 mysql-5.7.38-linux-glibc2.12-x86_64
//配置环境变量
[root@mysql local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@mysql local]# source /etc/profile.d/mysql.sh
//创建数据库数据存放目录
[root@mysql local]# chown -R mysql.mysql /opt/data/
[root@mysql local]# ll /opt
total 0
drwxr-xr-x. 2 mysql mysql 6 Oct 11 22:52 data
//设置头文件软链接
[root@mysql ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
'/usr/local/include/mysql' -> '/usr/local/mysql/include/'
//配置lib库
[root@mysql ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
//初始化数据库
[root@mysql ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data
//添加配置文件
[root@mysql ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data 
socket = /tmp/mysql.sock 
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql		
skip-name-resolve
//修改启动配置文件
[root@mysql ~]# sed -ri "s#^(basedir=).*#\1/usr/local/mysql#g" /usr/local/mysql/support-files/mysql.server
[root@mysql ~]# sed -ri "s#^(datadir=).*#\1/opt/data#g" /usr/local/mysql/support-files/mysql.server
//配置systemd管理mysqld
[root@mysql ~]# cat > /usr/lib/systemd/system/mysqld.service << EOF
> [Unit]
> Description=mysql server daemon
> After=network.targe
> 
> [Install]
> WantedBy=multi-user.target
> 
> [Service]
> Type=forking
> ExecStart=/usr/local/mysql/support-files/mysql.server start
> ExecStop=/usr/local/mysql/support-files/mysql.server stop
> ExexReload=/bin/kill/ -HUP \$MAINPID
> EOF
[root@mysql ~]# systemctl daemon-reload 
[root@mysql ~]# systemctl enable --now mysqld.service 
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
//设置数据库密码
[root@mysql ~]# /usr/local/mysql/bin/mysql -uroot -e "set password = password('redhat')"
[root@mysql ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> 

部署PHP

[root@php ~]# cd /usr/src
//下载php包
[root@php src]# wget https://www.php.net/distributions/php-7.4.30.tar.xz
//下载依赖包
[root@php src]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd sqlite-devel libzip-devel gcc gcc-c++ make
[root@php src]# yum -y install  http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
[root@php src]# tar xf php-7.4.30.tar.xz php-7.4.30/
[root@php src]# ls
debug  kernels  php-7.4.30  php-7.4.30.tar.xz
[root@php src]# cd php-7.4.30/
[root@php php-7.4.30]# ./configure \
--prefix=/usr/local/php7 \
--with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-json \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl

......过程省略......
+--------------------------------------------------------------------+
| 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.
[root@php php-7.4.30]# make -j $(cat /proc/cpuinfo |grep processor|wc -l) && make install
//配置环境变量
[root@php php-7.4.30]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@php php-7.4.30]# source /etc/profile.d/php7.sh
[root@php php-7.4.30]# which php
/usr/local/php7/bin/php
[root@php php-7.4.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@php php-7.4.30]# chmod +x /etc/init.d/php-fpm
[root@php php-7.4.30]# cp php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? y
//进入php主目录
[root@php php-7.4.30]# cd /usr/local/php7/
[root@php php7]# ls
bin  etc  include  lib  php  sbin  var
//删除相关文件后缀
[root@php php7]# cp etc/php-fpm.conf.default etc/php-fpm.conf
[root@php php7]# cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf
//配置systemd启动
[root@php php7]# cat > /usr/lib/systemd/system/php-fpm.service << EOF
> [Unit]
> Description=OpenSSH server daemon
> After=network.target sshd-keygen.target
> 
> [Service]
> Type=forking
> ExecStart=/etc/init.d/php-fpm start
> ExecStop=/etc/init.d/php-fpm stop
> ExecReload=/bin/kill -HUP $MAINPID
> 
> [Install]
> WantedBy=multi-user.target
> EOF
//重新加载systemd配置文件
[root@php php7]# systemctl daemon-reload 
//设置php-fpm开机自启
[root@php php7]# systemctl enable --now php-fpm.service 
Synchronizing state of php-fpm.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable php-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
[root@php php7]# vim etc/php-fpm.d/www.conf
//搜索相关项取消注释进行修改
listen = 0.0.0.0:9000	//php服务器ip
listen.allowed_clients = 192.168.100.110 //nginx服务器ip
//创建网页测试目录
[root@php ~]# mkdir -p /var/www/html/
//生成测试网页
[root@php ~]# cat >  /var/www/html/index.php << EOF
> <?php
> phpinfo();
> ?>
> EOF
[root@php ~]# systemctl restart php-fpm.service

关联nginx和php

//关联nginx和php
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
http {
 server {
        listen       80;
        server_name  localhost;    
 location / {
            root   html;    //存放index.php目录
            index  index.php index.html index.htm;     //此处添加index.php
        }
location ~ \.php$ {          //取消注释,修改配置文件
            root           /var/www/html;
            fastcgi_pass   192.168.100.123:9000;      //反向代理,php服务器的ip
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME /var/www//html/$fastcgi_script_name;  
            include        fastcgi_params;
        }

访问nginx

image-20221011232027356

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性