部署LNMP架构
文章目录
- 部署LNMP架构
-
- 1、nginx的安装与配置
-
- 1、1 nginx的安装
- 1、2 nginx安装后配置
- 2、部署mysql
-
- 2、1 关闭防火墙和selinux
- 2、2 解压mysql压缩包
- 2、3 安装后配置
- 3、部署php
-
- 3、1 下载并解压PHP压缩包
- 3、2 安装后配置
- 3、3 配置php-fpm
- 3、4 启动php-fpm
- 3、5 生成测试页面
- 4、整合nginx和php
-
- 4、1 在nginx节点进行配置
- 4、2 测试nginx配置文件
IP |
系统 |
服务 |
192.168.89.10 |
centos 8 |
nginx |
192.168.89.150 |
centos 8 |
mysql |
192.168.89.151 |
centos 8 |
php |
1、nginx的安装与配置
1、1 nginx的安装
//创建系统用户nginx
[root@10 ~]# useradd -r -M -s /sbin/nologin nginx
//安装依赖环境
[root@10 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@10 ~]# yum -y groups mark install 'Development Tools'
//创建日志存放目录
[root@10 ~]# mkdir -p /var/log/nginx
[root@10 ~]# chown -R nginx.nginx /var/log/nginx
[root@10 ~]#
//下载nginx
[root@10 ~]# cd /usr/src/
[root@10 src]# wget http://nginx.org/download/nginx-1.22.0.tar.gz
--2022-10-10 14:15:30-- http://nginx.org/download/nginx-1.22.0.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1073322 (1.0M) [application/octet-stream]
Saving to: ‘nginx-1.22.0.tar.gz’
nginx-1.22.0.tar.gz 100%[====================================================================================>] 1.02M 301KB/s in 3.5s
2022-10-10 14:15:34 (301 KB/s) - ‘nginx-1.22.0.tar.gz’ saved [1073322/1073322]
[root@10 src]#
//编译安装
[root@10 src]# ls
debug kernels nginx-1.22.0.tar.gz
[root@10 src]# tar xf nginx-1.22.0.tar.gz
[root@10 src]# cd nginx-1.22.0
[root@10 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@10 nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
1、2 nginx安装后配置
//配置环境变量
[root@10 ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@10 ~]# . /etc/profile.d/nginx.sh
[root@10 ~]#
//服务控制方式,使用nginx命令
-t //检查配置文件语法
-v //输出nginx的版本
-c //指定配置文件的路径
-s //发送服务控制信号,可选值有{stop|quit|reopen|reload}
//启动nginx
[root@10 ~]# nginx
[root@10 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
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 [::]:*
[root@10 ~]#
2、部署mysql
2、1 关闭防火墙和selinux
[root@150 ~]# systemctl stop firewalld
[root@150 ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@150 ~]# vim /etc/selinux/config
SELINUX=disabled
[root@150 ~]# setenforce 0
2、2 解压mysql压缩包
[root@150 ~]# useradd -r -M -s /sbin/nologin mysql
[root@150 ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
[root@150 ~]# tar -zxf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@150 ~]# cd /usr/local/
[root@150 local]# ln -s mysql-5.7.37-linux-glibc2.12-x86_64/ mysql
[root@150 local]# chown -R mysql.mysql mysql*
//创建存放数据库目录,配置环境变量
[root@150 local]# mkdir /opt/data
[root@150 local]# chown -R mysql.mysql /opt/data/
[root@150 local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@150 local]# source /etc/profile.d/mysql.sh
//初始化安装
[root@150 ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2022-10-11T03:53:42.324329Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-10-11T03:53:42.504681Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-10-11T03:53:42.527280Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-10-11T03:53:42.583079Z 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: 4c429f10-4918-11ed-b506-000c296b6021.
2022-10-11T03:53:42.584347Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-10-11T03:53:42.966480Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-10-11T03:53:42.966496Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-10-11T03:53:42.966809Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-11T03:53:43.110134Z 1 [Note] A temporary password is generated for root@localhost: t%#J1O?DpQSC
[root@150 ~]# echo t%#J1O?DpQSC > pass
//配置mysql
[root@150 ~]# 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@150 ~]# vim /usr/local/mysql/support-files/mysql.server
basedir=/usr/local/mysql
datadir=/opt/data
[root@150 ~]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysql server daemon
After=network.targe
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.serve stop
ExecReload=/bin/kill -HUP \$MAINPID
[Install]
WantedBy=multi-user.target
[root@150 ~]# systemctl daemon-reload
[root@150 ~]# systemctl restart mysqld
[root@150 ~]# systemctl enable mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@150 ~]#
//安装依赖
[root@150 ~]# mysql -uroot -pt%#J1O?DpQSC
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
[root@150 ~]# dnf whatprovides libncurses.so.5
Last metadata expiration check: 0:10:18 ago on Tue 11 Oct 2022 11:56:55 AM CST.
ncurses-compat-libs-6.1-7.20180224.el8.i686 : Ncurses compatibility libraries
Repo : baseos
Matched from:
Provide : libncurses.so.5
ncurses-compat-libs-6.1-9.20180224.el8.i686 : Ncurses compatibility libraries
Repo : baseos
Matched from:
Provide : libncurses.so.5
[root@150 ~]# yum -y install ncurses-compat-libs
//登录数据库,重新设置密码
[root@150 ~]# mysql -uroot -pt%#J1O?DpQSC
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.37
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> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit
Bye
[root@150 ~]# mysql -uroot -p123456
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 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>
2、3 安装后配置
[root@150 ~]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/mysql/man //添加这一行
[root@150 ~]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@150 ~]#
3、部署php
3、1 下载并解压PHP压缩包
//下载相关依赖
[root@151 ~]# yum -y install 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 libxml2-devel
[root@151 ~]# 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
//下载PHP
[root@151 ~]# cd /usr/src
[root@151 src]# wget https://www.php.net/distributions/php-7.4.30.tar.xz
--2022-10-11 12:57:54-- https://www.php.net/distributions/php-7.4.30.tar.xz
Resolving www.php.net (www.php.net)... 185.85.0.29, 2a02:cb40:200::1ad
Connecting to www.php.net (www.php.net)|185.85.0.29|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 10419136 (9.9M) [application/octet-stream]
Saving to: ‘php-7.4.30.tar.xz’
php-7.4.30.tar.xz 100%[=====================================================================================>] 9.94M 2.30MB/s in 4.5s
2022-10-11 12:58:00 (2.21 MB/s) - ‘php-7.4.30.tar.xz’ saved [10419136/10419136]
[root@151 src]#
//编译安装PHP
[root@151 src]# tar xf php-7.4.30.tar.xz
[root@151 src]# cd php-7.4.30/
[root@151 php-7.4.30]# ./configure --prefix=/usr/local/php74 --with-config-file-path=/usr/local/php74/etc --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-gd --with-jpeg=/usr/local/jpeg --with-freetype=/usr/local/freetype --with-xpm=/usr/lib64 --with-zlib-dir=/usr/local/zlib --with-iconv --with-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --with-zip --enable-soap --without-pear --with-gettext --enable-session --with-curl --enable-ctype --enable-mysqlnd --enable-pdo
Generating files
configure: patching main/php_config.h.in
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/www.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/phpdbg/phpdbg.1
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands
+--------------------------------------------------------------------+
| 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@151 php-7.4.30]# make && make install
安装过程省略......
3、2 安装后配置
[root@151 ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@151 ~]# source /etc/profile.d/php7.sh
[root@151 ~]# cd /usr/src/
[root@151 src]# cd php-7.4.30
[root@151 php-7.4.30]# php -v
PHP 7.2.24 (cli) (built: Oct 22 2019 08:28:36) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
[root@151 php-7.4.30]#
3、3 配置php-fpm
[root@151 php-7.4.30]# cp php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@151 php-7.4.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@151 php-7.4.30]# chmod +x /etc/rc.d/init.d/php-fpm
[root@151 php-7.4.30]# cp /usr/local/php74/etc/php-fpm.conf.default /usr/local/php74/etc/php-fpm.conf
[root@151 php-7.4.30]# cp /usr/local/php74/etc/php-fpm.d/www.conf.default /usr/local/php74/etc/php-fpm.d/www.conf
[root@151 php-7.4.30]# vim /usr/local/php74/etc/php-fpm.d/www.conf
;listen = /run/php-fpm/www.sock
listen = 0.0.0.0:9000
listen.allowed_clients = 192.168.89.10
3、4 启动php-fpm
[root@151 ~]# cd /usr/lib/systemd/system
[root@151 system]# vim php-fpm.service
[root@151 php-7.4.30]# systemctl start php-fpm
[root@151 php-7.4.30]# systemctl enable php-fpm
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@151 php-7.4.30]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:9000 0.0.0.0:*
3、5 生成测试页面
[root@151 php-7.4.30]# vim /var/www/html/index.php
[root@151 php-7.4.30]# chown -R apache.apache /var/www/html
[root@151 php-7.4.30]# systemctl restart php-fpm.service
4、整合nginx和php
4、1 在nginx节点进行配置
[root@10 ~]# vi /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
}
location ~ \.php$ {
root /var/www/html; #更改目录
fastcgi_pass 192.168.89.151:9000; #这里为PHP服务器的地址
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
4、2 测试nginx配置文件
[root@10 ~]# 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
[root@10 ~]# nginx
[root@10 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
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 [::]:*
[root@10 ~]#
浏览器访问