一.LNMP的组成
1.LNMP是由Linux、Nginx、Mysql、Php 四个部分组成
2.LNMP的工作原理
由客户端发送页面请求给Nginx,Nginx会根据location匹配用户访问请求的URL路径判断是静态还是动态,静态的一般是以 .html .htm .css .shtml结尾,动态的一般是以 .php .jsp .py结尾。
如果是静态:由Nginx直接处理和响应。
**如果是动态:**Nginx会通过fastcgi接口转发给php-fpm进程接受,然后再由php解析器处理解释动态页面后响应返回给Nginx,Nginx再返回给客户端。如果动态页面涉及到数据库的读写,php会通过程序代码访问,mysql的api接口,并使用sql语句进行数据库的速写操作。
二.配置LNMP
1.准备三台虚拟机,并进行初始化操作
分别做 Nginx服务器,Mysql服务器和 php服务器。
三台虚拟机做一样的操作
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
vim /etc/sysconfig/selinux
2.Nginx服务器配置
2.1.安装依赖包
yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
2.2.创建运行用户
useradd -M -s /sbin/nologin nginx
2.3.准备软件包并解压
[root@localhost opt]# ls
nginx-1.26.0.tar.gz
[root@localhost opt]# tar xf nginx-1.26.0.tar.gz
[root@localhostopt]# ls
nginx-1.26.0 nginx-1.26.0.tar.gz
[root@localhostopt]# cd nginx-1.26.0/
[root@localhost nginx-1.26.0]#
2.4.编译安装
cd nginx-1.26.0/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make -j 4
make install
2.5.设置软链接,可以直接调用nginx命令来进行管理
[root@localhost nginx-1.26.0]# cd /usr/local/nginx/
[root@localhostnginx]# ls sbin/
nginx
[root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
2.6.添加 Nginx 系统服务
[root@localhost logs]# cd /usr/lib/systemd/system
[root@localhost system]# vim nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost system]# systemctl daemon-reload
[root@localhost system]# systemctl start nginx
[root@localhostsystem]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/s ystemd/system/nginx.service.
[root@localhost system]# netstat -lntp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2279/nginx: master
3.安装 MySQL 服务
3.1.安装Mysql环境依赖包
yum -y install ncurses ncurses-devel bison cmake openssl-devel gcc gcc-c++ make
3.2.创建运行用户
useradd -M -s /sbin/nologin mysql
3.3.准备软件包并解压
[root@localhost opt]# ls
mysql-boost-5.7.44.tar.gz
[root@localhost opt]# tar xf mysql-boost-5.7.44.tar.gz
[root@localhost opt]# ls
mysql-5.7.44 mysql-boost-5.7.44.tar.gz
[root@localhost opt]# cd mysql-5.7.44/
3.4编译安装
[root@localhost mysql-5.7.44]# cmake \
> -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
> -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
> -DSYSCONFDIR=/etc \
> -DSYSTEMD_PID_DIR=/usr/local/mysql \
> -DDEFAULT_CHARSET=utf8 \
> -DDEFAULT_COLLATION=utf8_general_ci \
> -DWITH_EXTRA_CHARSETS=all \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DMYSQL_DATADIR=/usr/local/mysql/data \
> -DWITH_BOOST=boost \
> -DWITH_SYSTEMD=1
make -j 4
make install
3.5.删除自带的数据库
yum remove -y mariadb*
3.6.修改mysql 配置文件mariadb
vim /etc/my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
3.7.更改mysql安装目录和配置文件的属主属组
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf
3.8.设置路径环境变量
vim /etc/profile 在文件最下面加入
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
[root@localhost bin]# source /etc/profile
[root@localhost bin]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin:/usr/local/mysql/lib
3.9.初始化数据库
cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
3.10.添加mysqld系统服务
cd /usr/local/mysql/usr/lib/systemd/system
cp mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start mysqld.service
systemctl enable mysqld
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
[root@localhost system]# netstat -lntp | grep mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 33819/mysqld
3.11.修改mysql 的登录密码
[root@localhost system]# mysqladmin -u root -p password "abc123"
Enter password: #直接回车
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
[root@zx3 system]# mysql -u root -pabc123
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 9
Server version: 5.7.44 Source distribution
Copyright (c) 2000, 2023, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> quit
Bye
[root@localhost system]#
3.12.授权远程登录并刷新
mysql -u root -pabc123
mysql> grant all on *.* to 'root'@'%' identified by 'abc123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
4.安装配置 PHP 解析环境
4.1.安装环境依赖包
yum -y install gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel pcre-devel
4.2.准备软件包并解压
[root@localhost opt]# ls
php-7.1.10.tar.bz2 rh
[root@localhost opt]# tar xf php-7.1.10.tar.bz2
[root@localhost opt]#
4.3.编译安装
cd php-7.1.10
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
make -j 4
make install
4.4.路径优化
[root@localhost php-7.1.10]# cd /usr/local/php/
[root@localhost php]# ls
bin etc include lib php sbin var
[root@localhost php]# ln -s /usr/local/php/bin/* /usr/local/bin/
[root@localhost php]# ln -s /usr/local/php/sbin/* /usr/local/sbin/
[root@localhost php]#
4.5.调整PHP配置文件
php有三个配置文件:
php.ini 主配置文件
php-fpm.conf 进程服务配置文件
www.conf 扩展配置文件
调整主配置文件
[root@localhost php]# php --ini
Configuration File (php.ini) Path: /usr/local/php/lib
Loaded Configuration File: (none)
Scan for additional .ini files in: (none)
Additional .ini files parsed: (none)
[root@localhost php]# cd /opt/php-7.1.10/
[root@localhost php-7.1.10]# ls
...............................大部分内容省略
EXTENSIONS php7.spec.in stub.c
footer php.gif tests
generated_lists php.ini-development travis
genfiles php.ini-production TSRM
header README.EXT_SKEL UPGRADING
include README.GIT-RULES UPGRADING.INTERNALS
[root@localhost php-7.1.10]# cp php.ini-production /usr/local/php/lib/php.ini
[root@localhost php-7.1.10]# cd /usr/local/php/lib/
[root@localhost lib]# ls
php php.ini
[root@localhost lib]# vim php.ini
--1170行--修改
mysqli.default_socket = /usr/local/mysql/mysql.sock
--939行--取消注释,修改
date.timezone = Asia/Shanghai
php -m #验证安装的模块
调整进程服务配置文件
[root@localhost php]# cd /usr/local/php/etc/
[root@localhost etc]# ls
pear.conf php-fpm.conf.default php-fpm.d
[root@localhost etc]#
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# ls
pear.conf php-fpm.conf php-fpm.conf.default php-fpm.d
vim php-fpm.conf
--17行--去掉";"注释
pid = run/php-fpm.pid
调整扩展配置文件
[root@localhost etc]# cd php-fpm.d/
[root@localhost php-fpm.d]# ls
www.conf.default
[root@localhost php-fpm.d]# cp www.conf.default www.conf
[root@localhost php-fpm.d]# vim www.conf
修改用户名和组名
user = php
group = php
将监听地址改为本机地址
listen = 20.0.0.20:9000
修改允许访问的客户端地址即Nginx服务器地址
listen.allowed_clients = 192.168.10.30
4.6.创建用户
useradd -M -s /sbin/nologin php
4.7.启动php-fpm
cd /opt/php-7.1.10/sapi/fpm/
[root@localhost fpm]# cp php-fpm.service /usr/lib/systemd/system
[root@localhost fpm]# systemctl daemon-reload
[root@localhost fpm]# systemctl start php-fpm.service
[root@localhost fpm]# systemctl enable php-fpm.service
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
[root@localhost fpm]# netstat -lntp | grep php
tcp 0 0 20.0.0.20:9000 0.0.0.0:* LISTEN 10837/php-fpm: mast
PHP-FPM(FastCGI Process Manager:FastCGI 进程管理器)是一个 PHPFastCGI 管理器, 由于Nginx服务器不能处理动态页面,需要由 Nginx 把动态请求交给 php-fpm 进程进行解析
5.配置 Nginx 支持 PHP 解析(在配置nginx的虚拟机上)
5.1.修改文件
[root@localhostsystem]# cd /usr/local/nginx/
[root@localhost nginx]# cd conf/
[root@localhost conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
[root@localhost conf]# vim nginx.conf
[root@localhost conf]# 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@localhost conf]#
$document_root代表网页根目录的值
5.2.验证PHP 测试页
[root@localhost conf]# mkdir -p /var/www/html
[root@localhost conf]# cd /var/www/html/ #动态
[root@localhost html]# ls
[root@localhost html]# vim test.php
<?php
phpinfo();
?>
[root@localhost html]# cd /usr/local/nginx/html/ #静态
[root@localhost html]# vim test.php
<h1>this is lll static web</h1>
测试nginx和php是否互通
#nginx虚拟机配置
[root@192 html]# cd /var/www/html
[root@192 html]# ls
test.php
/var/www/html
[root@192 html]# yum install -y rpcbind nfs-utils
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
软件包 rpcbind-0.2.0-49.el7.x86_64 已安装并且是最新版本
软件包 1:nfs-utils-1.3.0-0.68.el7.2.x86_64 已安装并且是最新版本
无须任何处理
[root@192 html]# vim /etc/exports
/var/www/html 192.168.9.0/24(rw,sync,no_root_squash)
#配置完成保存退出
[root@192 html]# systemctl enable --now rpcbind nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@192 html]# showmount -e
Export list for 192.168.9.210:
/var/www/html 192.168.9.0/24
#php虚拟机操作
[root@192 fpm]# showmount -e 192.168.9.210
Export list for 192.168.9.210:
/var/www/html 192.168.9.0/24
[root@192 fpm]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Apr 3 05:42:53 2024
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=5a2d5435-b24c-4838-a524-7a3f8afc6c7d /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
#添加以下一行配置
192.168.9.210:/var/www/html /var/www/html nfs defaults,_netdev 0 0
[root@192 fpm]# mkdir /var/www/html -p
[root@192 fpm]# mount -a
[root@192 fpm]# df
文件系统 1K-块 已用 可用 已用% 挂载点
devtmpfs 1913552 0 1913552 0% /dev
tmpfs 1930628 0 1930628 0% /dev/shm
tmpfs 1930628 20976 1909652 2% /run
tmpfs 1930628 0 1930628 0% /sys/fs/cgroup
/dev/mapper/centos-root 36805060 6155348 30649712 17% /
/dev/sda1 1038336 191088 847248 19% /boot
tmpfs 386128 36 386092 1% /run/user/0
/dev/sr0 4635056 4635056 0 100% /mnt
192.168.9.210:/var/www/html 36805120 4831232 31973888 14% /var/www/html
[root@192 fpm]# ls /var/www/html
test.php
5.3.验证数据库工作是否正常
#mysql虚拟机操作
mysql -u root -pabc123
grant all on *.* to 'root'@'%' identified by 'abc123';
flush privileges;
quit
#php虚拟机操作
vim /var/www/html/mysql.php
<?php
$link=mysqli_connect('192.168.9.140','root','abc123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>
浏览器访问
http://192.168.80.10/index.php
#mysql虚拟机操作
[root@192 bin]# cd /usr/local/mysql
[root@192 mysql]# ls
bin include man mysql.sock.lock README-test usr
data lib mysqld.pid mysql-test share
docs LICENSE mysql.sock README support-files
[root@192 mysql]# vim /etc/exports
[root@192 mysql]# vim /etc/exports
/usr/local/mysql 192.168.9.0/24(rw,sync)
#配置完成保存并退出
[root@192 mysql]# systemctl enable --now rpcbind nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@192 mysql]# showmount -e
Export list for 192.168.9.140:
/usr/local/mysql 192.168.9.0/24
#php虚拟机操作
[root@192 html]# showmount -e 192.168.9.140
Export list for 192.168.9.140:
/usr/local/mysql 192.168.9.0/24
[root@192 html]# mkdir /usr/local/mysql -p
[root@192 html]# mount 192.168.9.140:/usr/local/mysql /usr/local/mysql[root@192 html]# cd /usr/local/mysql
[root@192 mysql]# ls
bin include man mysql.sock.lock README-test usr
data lib mysqld.pid mysql-test share
docs LICENSE mysql.sock README support-files
[root@192 mysql]# cd /usr/local/php
[root@192 php]# ls
bin etc include lib php sbin var
[root@192 php]# cd lib
[root@192 lib]# ls
php php.ini
[root@192 lib]# vim php.ini
#1170行
mysqli.default_socket = /usr/local/mysql/mysql.sock
#保存退出
systemctl restart php-fpm.service
#http://192.168.9.210/mysql.php 页面测试
测试php与mysql是否相同