lnmp分离部署

lnmp分离部署


本次环境为:

系统信息 主机名 IP
RHEL 8 nginx 192.168.100.1
RHEL 8 mysql 192.168.100.2
RHEL 8 php 192.168.100.3

nginx

# 关闭防火墙
[root@nginx ~]# systemctl disable --now firewalld
[root@nginx ~]# sed -ri 's/^(SELINUX=).*/1disabled/g' /etc/selinux/config
[root@nginx ~]# setenforce 0

# 创建系统用户nginx
[root@nginx ~]# useradd  -r -M -s /sbin/nologin nginx

# 安装依赖包
[root@nginx ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
[root@nginx ~]# yum -y groups mark install 'Development Tools'

# 创建日志存放目录
[root@nginx ~]# mkdir -p /var/log/nginx
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx

# 下载nginx包
[root@nginx ~]# cd /usr/src/
[root@nginx src]# wget http://nginx.org/download/nginx-1.20.0.tar.gz

# 编译安装
[root@nginx src]# tar xf nginx-1.20.0.tar.gz
[root@nginx src]# cd nginx-1.20.0
[root@nginx nginx-1.20.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.20.0]# make && 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

# 修改配置文件
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
······
        location / {
            root   html;
            index  index.php index.html index.htm;
        }
        location ~ .php$ {
            fastcgi_pass   192.168.100.3:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

# 检查配置文件语法是否有误
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

# 创建测试页面index.php
[root@nginx ~]# cat > /usr/local/nginx/html/index.php <<EOF
<?php
    phpinfo();
?>
EOF

# 启动nginx
[root@nginx ~]# nginx
[root@nginx ~]# ss -antl
State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     
LISTEN     0          128                   0.0.0.0:80                  0.0.0.0:*        
LISTEN     0          128                   0.0.0.0:22                  0.0.0.0:*        
LISTEN     0          128                      [::]:22                     [::]:*    

mysql

# 关闭防火墙
[root@mysql ~]# systemctl disable --now firewalld
[root@mysql ~]# sed -ri 's/^(SELINUX=).*/1disabled/g' /etc/selinux/config
[root@mysql ~]# setenforce 0

# 创建系统用户mysql
[root@mysql ~]# useradd -r -M -s /sbin/nologin mysql

# 安装依赖包
[root@mysql ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs

# 下载mysql包
[root@mysql ~]# cd /usr/src/
[root@mysql src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

# 安装
[root@mysql src]# tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@mysql ~]# ln -s  /usr/local/mysql-5.7.31-linux-glibc2.12-x86_64 /usr/local/mysql
[root@mysql ~]# chown -R mysql.mysql /usr/local/mysql*

# 配置环境变量
[root@mysql ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@mysql ~]# source /etc/profile.d/mysql.sh
[root@mysql ~]# ln -s /usr/local/mysql/include/  /usr/include/mysql
[root@mysql ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@mysql ~]# ldconfig 

# 建立数据存放目录
[root@mysql ~]# mkdir /opt/data
[root@mysql ~]# chown -R mysql.mysql /opt/data

# 初始化数据库
[root@mysql ~]# mysqld --initialize --user=mysql --datadir=/opt/data
······
root@localhost: ABlonsByG6_e

# 记住密码
[root@mysql ~]# echo 'ABlonsByG6_e' > pass

# 配置mysql
[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 ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@mysql ~]# vim /etc/init.d/mysqld
······
basedir=/usr/local/mysql
datadir=/opt/data

# 启动mysql
[root@mysql ~]# service mysqld start
[root@mysql ~]# ss -antl
State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     
LISTEN     0          128                   0.0.0.0:22                  0.0.0.0:*        
LISTEN     0          128                      [::]:22                     [::]:*        
LISTEN     0          80                          *:3306                      *:* 

# 查看密码登录mysql
[root@mysql ~]# cat pass
ABlonsByG6_e
[root@mysql ~]# mysql -uroot -p'ABlonsByG6_e'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.7.31

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> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> quit
Bye

php

# 关闭防火墙
[root@php ~]# systemctl disable --now firewalld
[root@php ~]# sed -ri 's/^(SELINUX=).*/1disabled/g' /etc/selinux/config
[root@php ~]# setenforce 0

# 安装php
[root@php ~]# yum -y install php*

# 修改配置文件
[root@php ~]# vim /etc/php-fpm.d/www.conf
······
//注释此行
;listen = /run/php-fpm/www.sock
listen = 0.0.0.0:9000
······
//修改成nginx主机的IP
listen.allowed_clients = 192.168.100.1

# 创建测试页面index.php
[root@php ~]# cat > /var/www/html/index.php <<EOF
<?php
    phpinfo();
?>
EOF

[root@php ~]# chown -R nginx.nginx /var/www/html/

# 启动php
[root@php ~]# systemctl enable --now php-fpm
[root@php ~]# ss -antl
State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     
LISTEN     0          128                   0.0.0.0:9000                0.0.0.0:*        
LISTEN     0          128                   0.0.0.0:22                  0.0.0.0:*        
LISTEN     0          128                      [::]:22                     [::]:*   

网站访问测试

1

原文地址:https://www.cnblogs.com/yuqinghao/p/14825644.html