1. lnmp简介
LNMP:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。Mysql是一个小型关系型数据库管理系统。PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。
原理:浏览器发送http request请求到服务器(Nginx),服务器响应并处理web请求,将一些静态资源(CSS,图片,视频等)保存服务器上,然后将php脚本通过接口传输协议(网关协议)PHP-FCGI(fast-cgi)传输给PHP-FPM(进程管理程序),PHP-FPM不做处理,然后PHP-FPM调用PHP解析器进程,PHP解析器解析php脚本信息。PHP解析器进程可以启动多个,进行并发执行。然后将解析后的脚本返回到PHP-FPM,PHP-FPM再通过fast-cgi的形式将脚本信息传送给Nginx.服务器再通过Http response的形式传送给浏览器。浏览器再进行解析与渲染然后进行呈现。
2. lnmp架构部署
2.1 安装nginx
//创建系统用户nginx
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
//安装依赖环境
[root@localhost ~]# yum -y install pcre-devel pcre gcc gcc-c++ openssl-devel zlib zlib-devel make vim wget openssl openssl-devel gd-devel
//创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
//下载nginx
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://nginx.org/download/nginx-1.20.1.tar.gz
--2021-10-26 14:12:57-- https://nginx.org/download/nginx-1.20.1.tar.gz
正在解析主机 nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ...
正在连接 nginx.org (nginx.org)|3.125.197.172|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1061461 (1.0M) [application/octet-stream]
正在保存至: “nginx-1.20.1.tar.gz”
nginx-1.20.1.tar.gz 100%[========================================================>] 1.01M 454KB/s 用时 2.3s
2021-10-26 14:13:01 (454 KB/s) - 已保存 “nginx-1.20.1.tar.gz” [1061461/1061461])
[root@localhost src]#
//编译安装
[root@localhost src]# ls
debug kernels nginx-1.20.1.tar.gz
[root@localhost src]# tar xf nginx-1.20.1.tar.gz
[root@localhost src]# cd nginx-1.20.1/
[root@localhost nginx-1.20.1]# ./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@localhost nginx-1.20.1]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
//配置环境变量
[root@localhost nginx-1.20.1]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost nginx-1.20.1]# source /etc/profile.d/nginx.sh
[root@localhost nginx-1.20.1]# nginx
//启动nginx
[root@localhost nginx-1.20.1]# 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 [::]:*
[root@localhost nginx-1.20.1]#
//使用service控制nginx
[root@localhost nginx-1.20.1]# nginx -s stop
[root@localhost nginx-1.20.1]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service nginx.service
[root@localhost system]# cat nginx.service
[Unit]
Description=Nginx server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP \$MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost system]#
[root@localhost system]# systemctl daemon-reload
[root@localhost system]# systemctl enable --now nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
在浏览器输入IP访问
2.2 安装mysql
//安装依赖包
[root@localhost ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
//创建用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
mysql软件包下载地址:https://downloads.mysql.com/archives/community/
//下载二进制格式的mysql软件包
[root@localhost src]# ls
debug kernels mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz nginx-1.20.1 nginx-1.20.1.tar.gz
[root@localhost src]#
//解压软件至/usr/local/
[root@localhost src]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# ln -s /usr/local/mysql-5.7.34-linux-glibc2.12-x86_64/ /usr/local/mysql
//添加环境变量
[root@localhost src]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost src]# source /etc/profile.d/mysql.sh
[root@localhost src]# echo $PATH
/usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost src]#
[root@localhost src]# vim /etc/man_db.conf
23 MANDATORY_MANPATH /usr/local/mysql/man //添加这一行
[root@localhost src]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@localhost src]# ldconfig
//创建数据存放目录
[root@localhost src]# mkdir -p /opt/data
[root@localhost src]# chown -R mysql.mysql /opt/data/
//初始化数据库
[root@localhost src]# mysqld --initialize-insecure --user mysql --datadir /opt/data/
2021-10-26T06:38:59.862563Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-10-26T06:38:59.997140Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-10-26T06:39:00.027100Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-10-26T06:39:00.031350Z 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: 66f0a25d-3627-11ec-a2c9-000c29297f87.
2021-10-26T06:39:00.031896Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-10-26T06:39:00.348144Z 0 [Warning] CA certificate ca.pem is self signed.
2021-10-26T06:39:00.401931Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[root@localhost src]#
//生成配置文件
[root@localhost src]# cat /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@localhost src]#
//使用service控制mysql
[root@localhost src]# cat /usr/lib/systemd/system/mysqld.service
[Unit]
Description=Mysql server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost src]#
[root@localhost src]# vim /usr/local/mysql/support-files/mysql.server
46 basedir=/usr/local //修改这两行
47 datadir=/opt/data
[root@localhost src]# systemctl daemon-reload
[root@localhost src]# systemctl enable --now mysqld.service
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@localhost src]#
[root@localhost src]# 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 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost src]#
//设置密码
[root@localhost src]# yum -y install ncurses-compat-libs
[root@localhost src]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, 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("1");
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
2.3 安装php
//安装epel源
[root@localhost src]# yum -y install epel-release
//安装依赖包
[root@localhost 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 libsqlite3x-devel php-mysqlnd libzip-devel
[root@localhost 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
//下载php
[root@localhost src]# wget https://www.php.net/distributions/php-8.0.10.tar.gz
[root@localhost src]# tar xf php-8.0.10.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/
[root@localhost local]# ls
bin etc games include lib lib64 libexec mysql mysql-5.7.34-linux-glibc2.12-x86_64 nginx php-8.0.10 sbin share src
[root@localhost local]# cd php-8.0.10/
//编译安装
[root@localhost php-8.0.10]# ./configure --prefix=/usr/local/php8 --with-config-file-path=/etc --enable-fpm --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-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 --enable-posix
[root@localhost php-8.0.10]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
//安装后配置
[root@localhost php-8.0.10]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@localhost php-8.0.10]# source /etc/profile.d/php.sh
[root@localhost php-8.0.10]# which php
/usr/local/php8/bin/php
[root@localhost php-8.0.10]#
//配置php-fpm
[root@localhost php-8.0.10]# cp php.ini-production /etc/php.ini
cp:是否覆盖'/etc/php.ini'? y
[root@localhost php-8.0.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cp:是否覆盖'/etc/init.d/php-fpm'? y
[root@localhost php-8.0.10]# chmod +x /etc/rc.d/init.d/php-fpm
[root@localhost php-8.0.10]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@localhost php-8.0.10]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
[root@localhost php-8.0.10]# service php-fpm start
Starting php-fpm done
[root@localhost php-8.0.10]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
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 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost php-8.0.10]#
//使用service控制nginx
[root@localhost php-8.0.10]# service php-fpm stop
Gracefully shutting down php-fpm . done
[root@localhost php-8.0.10]# cat /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php server daemon
After=network.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
[root@localhost php-8.0.10]# systemctl daemon-reload
[root@localhost php-8.0.10]# 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@localhost php-8.0.10]#
[root@localhost php-8.0.10]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
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 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost php-8.0.10]#
//创建php访问界面
[root@localhost ~]# cat /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
[root@localhost ~]#
//修改nginx配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
43 location / {
44 root html;
45 index index.php index.html index.htm; //修改这一行
46 }
//取消下面注释
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; //修改这一行
70 include fastcgi_params;
71 }
[root@localhost ~]# systemctl restart nginx.service