LNMP==Linux+Nginx+Mysql+PHP的结构体系。工作原理如下:
首先,浏览器发送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的形式传送给浏览器。浏览器再进行解析与渲染然后进行呈现
Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket(这个socket可以是文件socket,也可以是ip socket)。 wrapper:为了调用CGI程序,还需要一个FastCGI的wrapper(wrapper可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定socket上,如端口或者文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接收到请求,然后Fork(派生)出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据(html页面或者图片)发送给客户端。这就是Nginx+FastCGI的整个运作过程
主机名 | IP | 角色 | 版本 |
---|---|---|---|
Nginx-server | 192.168.139.156 | web服务器 | nginx version: nginx/1.18.0 |
Mysql-server | 192.168.139.157 | 数据库 | 8.0.20 for Linux on x86_64 |
php-server | 192.168.139.158 | php脚本解析 | PHP 7.4.8 (fpm-fcgi) |
#关闭内核防火墙和包过滤防火墙,在此展示一台机器操作,其余两台一样操作
[root@Ripe ~]# systemctl stop firewalld
[root@Ripe ~]# systemctl disable firewalld
[root@Ripe ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/conf
#下载工具包
[root@Ripe ~]# yum install ntpdata -y
#同步时间
[root@Ripe ~]# ntpdate ntp1.aliyun.com
#同步硬件时间
[root@Ripe ~]# hwclock -w # hwclocl --systohc 也可以
#在此展示一台机器操作,其余两台一样操作
[root@Ripe ~]# hostnamectl set-hostname nginx-server
[root@Ripe ~]# su -l
[root@nginx-server ~]#
[root@Ripe ~]# hostnamectl set-hostname mysql-server
[root@Ripe ~]# su -l
[root@mysql-server ~]#
[root@Ripe ~]# hostnamectl set-hostname php-server
[root@Ripe ~]# su -l
[root@php-server ~]#
#下载并解压软件包
[root@php-server ~]# wget https://www.php.net/distributions/php-7.4.8.tar.gz
[root@php-server ~]# tar -zxvf php-7.4.8.tar.gz -C /usr/local/src/
#安装依赖
[root@php-server ~]# yum install -y gcc zlib zlib-devel openssl openssl-devel perl perl-devel make
[root@php-server ~]# yum install -y gd-devel libjpeg libjpeg-devel libpng libpng-devel libiconv-devel freetype freetype-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel libxslt-devel mhash
[root@php-server ~]# wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
[root@php-server ~]# tar -zxvf libmcrypt-2.5.7.tar.gz
[root@php-server ~]# cd libmcrypt-2.5.7
[root@php-server libmcrypt-2.5.7]# ./configure
[root@php-server libmcrypt-2.5.7]# make && make install
[root@php-server libmcrypt-2.5.7]# echo $?
0
[root@php-server libmcrypt-2.5.7]# yum -y install gd-devel libjpeg libjpeg-devel libpng libpng-devel libiconv-devel freetype freetype-d autoconf
[root@php-server ~]# rpm -e libcurl-7.29.0-42.el7.x86_64 --nodeps
[root@php-server ~]# wget https://curl.haxx.se/download/curl-7.56.0.tar.gz
[root@php-server ~]# tar -zxvf curl-7.56.0.tar.gz
[root@php-server ~]# cd curl-7.56.0
[root@php-server curl-7.56.0]# ./configure --prefix=/usr/local/curl --with-ssl --with-zlib
[root@php-server curl-7.56.0]# make && make install
[root@php-server curl-7.56.0]# echo $?
0
#编译安装php
[root@php-server ~]# cd /usr/local/src/php-7.4.8/
[root@php-server php-7.4.8]# ./configure --prefix=/usr/local/php-7.4.8 \
--with-config-file-path=/usr/local/php7/etc \
--with-curl=/usr/local/curl \
--disable-ipv6 \
--with-pdo-mysql \
--with-openssl \
--with-openssl-dir \
--with-pcre-regex \
--with-kerberos \
--with-libdir=lib \
--with-libxml-dir \
--with-mysqli=shared,mysqlnd \
--with-pdo-mysql=shared,mysqlnd \
--with-pdo-sqlite \
--with-gd \
--with-iconv \
--with-zlib \
--with-xmlrpc \
--with-xsl \
--with-pear \
--with-gettext \
--with-png-dir \
--with-jpeg-dir \
--with-freetype-dir \
--with-mcrypt \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-pdo \
--enable-mysqlnd \
--enable-zip \
--enable-inline-optimization \
--enable-shared \
--enable-libxml \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-mbregex \
--enable-ftp \
--enable-gd-native-ttf \
--enable-pcntl \
--enable-sockets \
--enable-soap \
--enable-session \
--enable-opcache \
--enable-fpm \
--enable-maintainer-zts \
--enable-fileinfo \
--enable-gd-jis-conv \
--with-zlib-dir
[root@php-server php-7.4.8]# echo $?
0
[root@php-server php-7.4.8]# make
[root@php-server php-7.4.8]# echo $?
0
[root@php-server php-7.4.8]# make install
[root@php-server php-7.4.8]# echo $?
0
启动项
[root@php-server php-7.4.8]# cp php.ini-production /usr/local/php-7.4.8/etc/php.ini
[root@php-server php-7.4.8]# cp php.ini-production /etc/php.ini
[root@php-server php-7.4.8]# cd /usr/local/src/php-7.4.8/sapi/fpm/
[root@php-server fpm]# cp -p init.d.php-fpm /etc/init.d/php-fpm
[root@php-server fpm]# cd /etc/init.d/
[root@php-server init.d]# ls
functions netconsole network php-fpm README
[root@php-server init.d]# chmod +x php-fpm
[root@php-server init.d]# chkconfig php-fpm on
[root@php-server init.d]# vim /etc/profile.d/php.sh
export PATH=$PATH:/usr/local/php-7.4.8/bin:/usr/local/php-7.4.8/sbin
[root@php-server init.d]# source /etc/profile.d/php.sh
配置php-fpm模块
#编辑配置文件
[root@php-server init.d]# cd /usr/local/php-7.4.8、etc
[root@php-server etc]# cp php-fpm.conf.default php-fpm.conf
[root@php-server etc]# cd php-fpm.d/
[root@php-server php-fpm.d]# cp www.conf.default www.conf
[root@php-server php-fpm.d]# vim www.conf
[www]
user = nginx
group = nginx
listen = 0.0.0.0:9000
listen.owner = nginx
listen.group = nginx
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
[root@php-server php-fpm.d]# cd ../
[root@php-server etc]# vim php-fpm.conf
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
启动
[root@php-server ~]# cd /usr/local/php-7.4.8/sbin
[root@php-server sbin]# ./php-fpm
[root@php-server ~]# mkdir /www
[root@php-server www]# chown -R nginx:nginx www
[root@php-server www]# vim index.php
<?php
phpinfo();
?>
[root@nginx-server ~]# cat /etc/nginx/nginx.conf
user nginx nginx;
pid /var/run/nginx.pid;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request"';
server {
listen 80;
server_name 192.168.139.156; #Nginx-server_ip
charset koi8-r;
access_log logs/host.access.log main;
location / {
root /www;
index index.html index.htm;
}
location ~ \.php$ {
root /www;
fastcgi_pass 192.168.139.158:9000; #需要监听的php-server_ip
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
重启
[root@nginx-server ~]# systemctl restart nginx
在mysql-server创建测试账户和测试数据库
mysql> create user "superadmin"@"%" identified with mysql_native_password by "Centos@123"; #指定用户认证插件为mysql_native_password
mysql> create database memcacher;
安装php-mysql连接模块
[root@php-server ~]# cd /usr/local/src/php-7.4.8/ext/mysqli
[root@php-server mysqli]# /usr/local/php7/bin/phpize
[root@php-server mysqli]# ./configure \
--with-php-config=/usr/local/php7/bin/php-config \
--enable-embedded-mysqli=shared \
--enable-shared
[root@php-server mysqli]# echo $?
0
[root@php-server mysqli]# make
[root@php-server mysqli]# echo $?
0
[root@php-server mysqli]# make install
[root@php-server mysqli]# echo $?
0
[root@php-server mysqli]# cd /usr/local/php-7.4.8/lib/php/extensions/no-debug-zts-20190902/
[root@php-server no-debug-zts-20190902]# ls
mysqli.a mysqli.so opcache.a opcache.so pdo_mysql.a pdo_mysql.so
[root@php-server no-debug-zts-20190902]# cd /usr/local/src/php-7.4.8/ext/pdo_mysql/
[root@php-server pdo_mysql]# /usr/local/php-7.4.8/bin/phpize
[root@php-server pdo_mysql]# ./configure \
--with-php-config=/usr/local/php-7.4.8/bin/php-config \
--with-pdo-mysql=mysqlnd
[root@php-server pdo_mysql]# echo $?
0
[root@php-server pdo_mysql]# make
[root@php-server pdo_mysql]# echo $?
0
[root@php-server pdo_mysql]# make install
[root@php-server pdo_mysql]# echo $?
0
[root@php-server php-7.4.8]# cd /usr/local/src/php-7.4.8
[root@php-server php-7.4.8]# cp php.ini-production /usr/local/php-7.4.8/etc/php.ini
[root@php-server php-7.4.8]# cd /usr/local/php-7.4.8/etc/
[root@php-server etc]# vim php.ini
extension_dir = "/usr/local/php-7.4.8/lib/php/extensions/no-debug-zts-20190902/"
extension = mysqli.so
extension = pdo_mysql.so
[root@php-server etc]# cd /etc/init.d/
[root@php-server init.d]# ./php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@php-server init.d]# ps -ef | grep "php"
[root@php-server www]# vim index1.php
<?php
$con = new mysqli('192.168.139.157','superadmin','Centos@123','memcacher');
if(!$con)
echo "faling...";
else
echo "success connect mysql\n";
?>
1、编译安装PHP7.4.2时出现错误:configure: error: Package requirements (sqlite3 > 3.7.4) were not met: No package ‘sqlite3’ found。
通过yum命令安装sqlite3即可:
yum install sqlite-devel -y
2、configure: error: Package requirements [libcurl >= 7.15.5] were not met: No package ‘libcurl’ found
yum search libcurl -y
yum install libcurl-devel.x86_64 -y
3、configure: error: Package requirements (oniguruma) were not met
centos7:
oniguruma: wget http://down.24kplus.com/linux/oniguruma/oniguruma-6.7.0-1.el7.x86_64.rpm
oniguruma-devel:wget http://down.24kplus.com/linux/oniguruma/oniguruma-devel-6.7.0-1.el7.x86_64.rpm
centos8:
wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
tar -zxf oniguruma-6.9.4.tar.gz
cd oniguruma-6.9.4
备用地址:http://down.24kplus.com/linux/oniguruma/oniguruma-6.9.4.tar.gz
编译安装: ./autogen.sh && ./configure --prefix=/usr/local/
make && make install
4、重启失败时
#编辑php配置文件,pid文件路径修改为绝对路径 /usr/local/php-7.4.8/var/run/php-fpm.pid
[root@php-server etc]# vim /usr/local/php-7.4.8/etc/php-fpm.conf
pid = /usr/local/php-7.4.8/var/run/php-fpm.pid
#然后杀死进程
pkill php-fpm
#尝试重启
[root@php-server etc]# cd /etc/init.d/
[root@php-server init.d]# ./php-fpm restart
到这里LNMP部署就完成了,还希望大家能够纠错