文章最后更新时间:2025年06月12日
LNMP工作原理:
客户端发送请求,Nginx负责接收,如果是静态资源(html、js、css、png等)则自己处理并返回给客户!
Nginx接收的是动态请求(.php请求文件)则转发到PHP服务端,交由PHP服务端处理!
PHP接收处理Nginx转发过来动态请求,并返回给Nginx服务端!
如果请求中包含了 SQL 语句,PHP则会连接mysql服务器,进行增删改查等操作,最终将处理完的数据返回给Nginx,再由Nginx返回给客户端请求!
也就是说,在lnmp分布式部署时候,只需要配置Nginx+PHP,PHP+Mysql即可!
最后安装phpMyAdmin来验证效果!
- 如果phpMyAdmin能成功运行,说明lnmp分布式部署是成功的!
- phpMyAdmin安装在实际工作中也是经常能碰到的!
环境准备:
Nginx | 192.168.140.130 |
---|---|
PHP | 192.168.140.132 |
Mysql | 192.168.140.129 |
一、Nginx安装
之前文章已经说过怎么源码安装nginx,这边不再详细记录,具体安装请参考这篇文章 —>常用的Web服务源码编译安装
二、安装MySQL
这边主要使用基于glib(半成品包,可以指定配置文件、数据文件路径)包来安装mysql;
对源码编译安装mysql感兴趣的读者,可以参考这篇文章 —> Mysql5.7源码编译安装
环境清理:
# 查看是否自带的mysql
rpm -qa | grep mysql
rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64mariadb-5.5.68-1.el7.x86_64
# 删除mariadb
yum remove mariadb
rpm -e mariadb-libs-5.5.68-1.el7.x86_64 --nodeps
rm -rf /etc/my.cnf
下载参考链接:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.39.tar.gz
创建运行mysql的用户
useradd -r -s /sbin/nologin mysql
创建数据目录、日志目录并授权
# 创建数据目录
[root@localhost src]# mkdir -p /data/mysql57/data
# 创建日志目录
[root@localhost src]# mkdir -p /data/mysql57/log
# 更改目录所有者
[root@localhost src]# chown -R mysql.mysql /data
mysql5.7解压编译安装
# 解压mysql5.7
[root@localhost local]# tar zxvf src/mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
# 重命名安装目录并进入该目录
[root@localhost local]# mv mysql-5.7.39-linux-glibc2.12-x86_64/ mysql5.7
[root@localhost local]# cd mysql5.7/
[root@localhost mysql5.7]
在安装目录下创建配置文件my.cnf
vim my.cnf
[client]
port=3306
socket=/tmp/mysql57.sock
[mysqld]
socket=/tmp/mysql57.sock
character-set-server=utf8
collation-server=utf8_general_ci
skip-name-resolve
user=mysql
port=3306
basedir=/usr/local/mysql5.7/
datadir=/data/mysql57/data
tmpdir=/tmp
log-error=/data/mysql57/log/mysqld57.log
pid-file=/data/mysql57/mysqld57.pid
Mysql5.7初始化:
[root@localhost mysql5.7]#/usr/local/mysql5.7/bin/mysqld --defaults-file=/usr/local/mysql5.7/my.cnf --initialize --user=mysql --basedir=/usr/local/mysql5.7/ --datadir=/data/mysql57/data
修改mysql5.7启动脚本:分别修改basedir、datadir、conf的路径
拷贝启动脚本:
[root@localhost mysql5.7]# cp -a ./support-files/mysql.server /etc/init.d/mysql57
启动mysql5.7
[root@localhost mysql5.7]#/etc/init.d/mysql57 start
查看临时密码:
[root@localhost mysql5.7]# cat /data/mysql57/log/mysqld57.log | grep password
2022-09-23T05:25:47.783251Z 1 [Note] A temporary password is generated for root@localhost: nd>phWNwB7YS
2022-09-23T05:35:41.498538Z 0 [Note] Shutting down plugin 'sha256_password'
2022-09-23T05:35:41.498540Z 0 [Note] Shutting down plugin 'mysql_native_password'
重置密码:
[root@localhost mysql5.7]# ./bin/mysql -S /tmp/mysql57.sock -uroot -p '临时密码'
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.39
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> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
三、安装PHP
依赖包安装:
yum -y install gcc gcc-c++ openssl readline-devel openssl-devel curl-devel curl libjpeg-devel libjpeg gd-devel gd libxslt-devel libxslt bzip2-devel bzip2 sqlite-devel autoconf oniguruma-devel
以上依赖包出现无法下载安装情况,请更换使用阿里源—> Centos7更换阿里源
安装libzip
直接使用迅雷下载,参考链接:https://libzip.org/download/libzip-1.3.2.tar.gz
上传服务器,解压编译安装:
[root@localhost src]# tar zxvf libzip-1.3.2.tar.gz
[root@localhost src]#cd libzip-1.3.2
[root@localhost src]# ./configure && make && make install
[root@localhost libzip-1.3.2]# whereis libzip
libzip: /usr/local/lib/libzip.so /usr/local/lib/libzip.la /usr/local/lib/libzip.a
[root@localhost libzip-1.3.2]# export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/"
PHP源码包导入服务器,解压进入编译安装
[root@localhost memcached-3.1.5]# cd /usr/local/src/php-7.4.32
[root@localhost php-7.4.32]# ./configure --prefix=/usr/local/php7 \
--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-7.4.32]#make && make install
# 拷贝配置文件
[root@localhost php-7.4.32]# cp -a php.ini-development /usr/local/php7/etc/php.ini
# 拷贝启动脚本并授权
[root@localhost php-7.4.32]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-7.4.32]# chmod +x /etc/init.d/php-fpm
# 拷贝配置文件
[root@localhost php-7.4.32]# cd /usr/local/php7/etc/
[root@localhost etc]# cp -a php-fpm.conf.default php-fpm.conf
[root@localhost etc]# cp -a php-fpm.d/www.conf.default php-fpm.d/
# 建立软链接:
[root@localhost etc]# ln -s /usr/local/php7/bin/* /usr/local/bin/
[root@localhost etc]# ln -s /usr/local/php7/sbin/* /usr/local/sbin/
[root@localhost etc]# php -v
PHP 7.4.32 (cli) (built: Oct 10 2022 04:04:28) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
# 启动php-fpm
[root@localhost etc]# /etc/init.d/php-fpm start
Starting php-fpm done
[root@localhost etc]# netstat -pltun | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 93401/php-fpm: mast
tcp 0 0 192.168.140.132:9000 0.0.0.0:* LISTEN 23499/php-fpm: mast
[root@localhost etc]# ps aux | grep php
root 23499 0.0 0.3 240004 6824 ? Ss 02:54 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
nginx 23500 0.0 0.3 240004 6240 ? S 02:54 0:00 php-fpm: pool www
nginx 23501 0.0 0.4 240004 7944 ? S 02:54 0:00 php-fpm: pool www
nginx 23502 0.0 0.3 240004 6240 ? S 02:54 0:00 php-fpm: pool www
nginx 23503 0.0 0.3 240004 6240 ? S 02:54 0:00 php-fpm: pool www
nginx 23504 0.0 0.3 240004 6240 ? S 02:54 0:00 php-fpm: pool www
root 93401 0.0 0.3 231176 6620 ? Ss 04:53 0:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nobody 93402 0.0 0.3 231176 6120 ? S 04:53 0:00 php-fpm: pool www
nobody 93403 0.0 0.3 231176 6120 ? S 04:53 0:00 php-fpm: pool www
root 93426 0.0 0.0 112812 980 pts/0 S+ 04:54 0:00 grep --color=auto php
四、PHP+Nginx关联
工作原理:Nginx第一时间接收到用户的请求,只要是php的请求都转发到PHP服务器,交由PHP服务器处理!
Nginx服务器配置
# 创建nginx的系统用户
[root@localhost html]# useradd -r -s /sbin/nologin nginx
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
root /www;# 这个是PHP的目录,php的文件要放置该目录下
fastcgi_pass 192.168.140.132:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
运行用户设置为nginx系统用户
PHP服务配置:
# 创建nginx系统用户
[root@localhost html]# useradd -r -s /sbin/nologin nginx
# 创建放置php的文件目录,并设置目录所有者为nginx
[root@localhost ~]# mkdir /www
[root@localhost ~]# chown -R nginx:nginx /www/
# 修改配置文件:
[root@localhost ~]# vim /usr/local/php7/etc/php-fpm.d/www.conf
user = nginx
group = nginx
# php服务器的地址
listen = 192.168.140.132:9000
# 进程的管理方式 dynamic为动态管理 static为静态管理
pm = dynamic
# 静态方式下开启的php-fpm进程数量,在动态方式下他限定php-fpm的最大进程数(这里要注意pm.max_spare_servers的值只能小于等于pm.max_children)
pm.max_children = 120
# 动态方式下的起始php-fpm进程数量
pm.start_servers = 20
# 动态方式空闲状态下的最小php-fpm进程数量
pm.min_spare_servers = 10
# 动态方式空闲状态下的最大php-fpm进程数量
pm.max_spare_servers = 30
# 保存之后重新加载配置文件
[root@localhost ~]# /etc/init.d/php-fpm reload
Reload service php-fpm done
# 新建一个测试php文件
[root@localhost ~]# vim /www/test.php
<html>
<h1>welcome to php !</h1>
<?php
phpinfo();
?>
</html>
最终结果测试:
五、Mysql+PHP
工作原理:需要查询数据相关的操作,都是由PHP连接数据,执行相对应的mysql语句完成的,所以Mysql需要授权,允许PHP服务器远程连接Mysql!
mysql> create user php@'192.168.140.132' identified by "123456";
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on *.* to php@192.168.140.132;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host,authentication_string from mysql.user;
+---------------+-----------------+-------------------------------------------+
| user | host | authentication_string |
+---------------+-----------------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| php | 192.168.140.132 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+---------------+-----------------+-------------------------------------------+
4 rows in set (0.00 sec)
php测试连接mysql:
测试文件:
[root@localhost www]# cat test.php
<html>
<h1>welcome to php !</h1>
<?php
$con = new mysqli('192.168.140.129','php','123456');
if(!$con)
die("connect error:".mysqli_connect_error());
else
echo "connet mysql server ok!\n";
phpinfo();
?>
最终效果:
六、phpMyAdmin安装
phpmyadmin的压缩包包含动、静态资源,也就是说动态资源得放在PHP服务器上,静态资源放在Nginx服务器上!
两种方式实现:
- 一台服务器搭建NFS服务,把网页动、静态资源都放在NFS挂载出去的目录上,这样PHP和Nginx都可以同时读取网页资源的文件!
- 静态资源放在Nginx,动态资源放在PHP上,但是phpmyadmin的动静态资源文件繁杂,不好分辨,为了省事,直接在Nginx和PHP服务器上各拷贝一份phpmyadmin的解压包!
Nginx服务器操作:
上传安装包到Nginx的html目录下并解压
[root@localhost html]# tar zxvf phpmyadmin.tar.gz
[root@localhost html]#cd phpmyadmin
[root@localhost html]chown -R nginx:nginx /usr/local/nginx/
PHP服务器操作:
[root@localhost ~]# cd /www/
[root@localhost www]# ls
phpmyadmin phpmyadmin.tar.gz test.php
[root@localhost www]# tar zxvf phpmyadmin.tar.gz
[root@localhost www]# cd phpmyadmin
[root@localhost libraries]# pwd
/www/phpmyadmin/libraries
# 告诉phpmyadmin的mysql地址是哪一个!
[root@localhost libraries]# vim config.default.php
$cfg['Servers'][$i]['host'] = 'Mysql的地址';
最终效果:
PHP拓展:
php添加memcache模块
下载参考链接:
http://pecl.php.net/package/memcache
http://pecl.php.net/package/memcached
phpize是用来拓展php新模块的,通过phpize可以建立php的外挂模块
autoconf是一个用于生成shell脚本的工具,可以自动配置软件源代码以适应多种类似POSIX的系统,为了让你的软件包在所有的不同系统上都可以进行编译。
yum install autoconf
源码包导入系统,解压进入源码编译
[root@localhost memcache-8.0]# yum install autoconf
[root@localhost src]# tar zxvf memcache-8.0.tgz
[root@localhost src]# cd memcache-8.0
[root@localhost memcache-8.0]# ls
cloudbuild.yaml config.m4 CREDITS Dockerfile LICENSE README tests
config9.m4 config.w32 docker example.php memcache.php src
[root@localhost memcache-8.0]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20190902
Zend Module Api No: 20190902
Zend Extension Api No: 320190902
[root@localhost memcache-8.0]# ll
total 664
drwxr-xr-x 2 root root 54 Oct 10 02:20 autom4te.cache
drwxr-xr-x 2 root root 241 Oct 10 02:14 build
-rw-rw-r-- 1 root root 372 Dec 6 2020 cloudbuild.yaml
-rw-rw-r-- 1 root root 4545 Dec 6 2020 config9.m4
-rw-r--r-- 1 root root 1649 Oct 10 02:20 config.h.in
-rw-rw-r-- 1 root root 67 Dec 6 2020 config.m4
-rwxr-xr-x 1 root root 454377 Oct 10 02:20 configure
-rw-r--r-- 1 root root 5082 Oct 10 02:20 configure.ac
-rw-rw-r-- 1 root root 1081 Dec 6 2020 config.w32
-rw-rw-r-- 1 root root 32 Dec 6 2020 CREDITS
drwxr-xr-x 2 root root 39 Oct 10 02:14 docker
-rw-rw-r-- 1 root root 247 Dec 6 2020 Dockerfile
-rw-rw-r-- 1 root root 509 Dec 6 2020 example.php
-rw-rw-r-- 1 root root 3208 Dec 6 2020 LICENSE
-rw-rw-r-- 1 root root 29064 Dec 6 2020 memcache.php
-rw-rw-r-- 1 root root 5975 Dec 6 2020 README
-rw-r--r-- 1 root root 127055 Oct 10 02:20 run-tests.php
drwxr-xr-x 2 root root 299 Oct 10 02:14 src
drwxr-xr-x 2 root root 4096 Oct 10 02:14 tests
[root@localhost memcache-8.0]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@localhost memcache-8.0]# make && make install
----------------------------------------------------------------------
Libraries have been installed in:
/usr/local/src/memcache-8.0/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
Don't forget to run 'make test'.
# 记住这个路径,非常重要
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/
[root@localhost memcache-8.0]# vim /usr/local/php/etc/php.ini # 添加以下两行
extension="memcache.so";
extension_dir="/usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/"
# 重启php-fpm服务
[root@localhost memcache-8.0]# /etc/init.d/php-fpm stop
Gracefully shutting down php-fpm . done
[root@localhost memcache-8.0]# /etc/init.d/php-fpm start
Starting php-fpm done
最终效果:
PHP添加memcached模块
yum install libmemcached libmemcached-devel zlib zlib-devel libevent-devel
[root@localhost src]# tar zxvf memcached-3.1.5.tgz
[root@localhost src]# cd memcached-3.1.5
[root@localhost memcached-3.1.5]# ls
ChangeLog fastlz memcached-api.php php_memcached.c php_memcached_server.h tests
config.m4 g_fmt.c memcached.ini php_memcached.h php_memcached_session.c
config.w32 g_fmt.h php_libmemcached_compat.c php_memcached_private.h php_memcached_session.h
CREDITS LICENSE php_libmemcached_compat.h php_memcached_server.c README.markdown
[root@localhost memcached-3.1.5]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20190902
Zend Module Api No: 20190902
Zend Extension Api No: 320190902
[root@localhost memcached-3.1.5]# ll
total 888
drwxr-xr-x 2 root root 54 Oct 10 02:47 autom4te.cache
drwxr-xr-x 2 root root 241 Oct 10 02:47 build
-rw-r--r-- 1 501 games 12047 Dec 3 2019 ChangeLog
-rw-r--r-- 1 root root 2274 Oct 10 02:47 config.h.in
-rw-r--r-- 1 501 games 15022 Dec 3 2019 config.m4
-rwxr-xr-x 1 root root 480935 Oct 10 02:47 configure
-rw-r--r-- 1 root root 5082 Oct 10 02:47 configure.ac
-rw-r--r-- 1 501 games 1502 Dec 3 2019 config.w32
-rw-r--r-- 1 501 games 59 Dec 3 2019 CREDITS
drwxr-xr-x 2 root root 53 Oct 10 02:46 fastlz
-rw-r--r-- 1 501 games 2297 Dec 3 2019 g_fmt.c
-rw-r--r-- 1 501 games 1181 Dec 3 2019 g_fmt.h
-rw-r--r-- 1 501 games 3218 Dec 3 2019 LICENSE
-rw-r--r-- 1 501 games 7223 Dec 3 2019 memcached-api.php
-rw-r--r-- 1 501 games 6496 Dec 3 2019 memcached.ini
-rw-r--r-- 1 501 games 2702 Dec 3 2019 php_libmemcached_compat.c
-rw-r--r-- 1 501 games 1707 Dec 3 2019 php_libmemcached_compat.h
-rw-r--r-- 1 501 games 136038 Dec 3 2019 php_memcached.c
-rw-r--r-- 1 501 games 1878 Dec 3 2019 php_memcached.h
-rw-r--r-- 1 501 games 6596 Dec 3 2019 php_memcached_private.h
-rw-r--r-- 1 501 games 24349 Dec 3 2019 php_memcached_server.c
-rw-r--r-- 1 501 games 1473 Dec 3 2019 php_memcached_server.h
-rw-r--r-- 1 501 games 14272 Dec 3 2019 php_memcached_session.c
-rw-r--r-- 1 501 games 1607 Dec 3 2019 php_memcached_session.h
-rw-r--r-- 1 501 games 1875 Dec 3 2019 README.markdown
-rw-r--r-- 1 root root 127055 Oct 10 02:47 run-tests.php
drwxr-xr-x 2 root root 4096 Oct 10 02:46 tests
[root@localhost memcached-3.1.5]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@localhost memcached-3.1.5]# make && make install
----------------------------------------------------------------------
Libraries have been installed in:
/usr/local/src/memcached-3.1.5/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
Don't forget to run 'make test'.
# 记住这个路径,非常重要
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/
[root@localhost memcached-3.1.5]# ls /usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/
memcached.so memcache.so opcache.a opcache.so
[root@localhost memcached-3.1.5]# vim /usr/local/php/etc/php.ini # 添加以下两行
extension="memcached.so";
extension_dir="/usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/"
# 重新加载配置文件:
[root@localhost memcached-3.1.5]# /etc/init.d/php-fpm reload
Reload service php-fpm done