LNMP架构介绍及部署实践

1.1架构原理介绍

LNMP是指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的形式传送给浏
览器。浏览器再进行解析与渲染然后进行呈现。

1.2环境规划

主机名 IP
php-server 192.168.79.156
nginx-server 192.168.79.157
mysql-server 192.168.79.161

软件版本:
nginx:1.17.0
PHP: 7.4.8
mysql:8.0.19

1.3节点处理

1.3.1处理防火墙

systemctl stop firewalld
systemctl disable firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0

1.3.2 时间同步

yum -y install ntp ntpdate
ntpdate cn.pool.ntp.org
hwclock --systohc

1.3.3 修改主机名

[root@php-server ~]# hostnamectl set-hostname nginx-server
[root@php-server ~]# su -l
[root@server02 ~]# hostnamectl set-hostname php-server
[root@server02 ~]# su -l
[root@localhost ~]# hostnamectl set-hostname mysql-server
[root@localhost ~]# su -l

1.4 程序安装

1.4.1 MySQL、nginx安装略。

1.4.2 php程序安装

(1)下载并解压软件包

[root@php-server ~]# wget https://www.php.net/distributions/php-7.4.8.tar.gz
[root@php-server ~]# tar xf php-7.4.8.tar.gz -C /usr/local/src/

(2)安装依赖

[root@php-server ~]# yum install -y gcc zlib zlib-devel openssl openssl-devel
perl perl-devel make
[root@php-server ~]# wget
ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
[root@php-server ~]# yum -y install 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 ~]# tar zxf 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

(3)编译安装php

[root@php-server libmcrypt-2.5.7]# cd /usr/local/src/php-7.4.8/
[root@php-server php-7.4.8]# ./configure --prefix=/usr/local/php7 \
--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
0
[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

1.5 LNMP环境配置与测试

1.5.1 PHP配置

配置启动项

[root@php-server php-7.4.8]# cp php.ini-production /usr/local/php7/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/
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
export PATH=/usr/local/php7/bin:/usr/local/php7/sbin:$PATH
[root@php-server init.d]# source /etc/profile
[root@php-server init.d]# php -v

配置php-fpm模块
编辑php-fpm的配置文件(/usr/local/php7/etc/php-fpm.conf)
配置fpm相关选项为你所需要的值

[root@php-server ~]# cd /usr/local/php7
[root@php-server php7]# cd 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-7.4.8]# vim /usr/local/php7/etc/php-fpm.conf
pm.max_children = 50      //最多同时提供50个进程提供50个并发服务
pm.start_servers = 5       //启动时启动5个进程
pm.min_spare_servers = 2  //最小空闲进程数 
pm.max_spare_servers = 8  //最大空闲进程数
[root@php-server ~]# cat /usr/local/php7/etc/php-fpm.d/www.conf |grep -iv "^\;"
[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 ~]# cd /usr/local/php7/sbin
[root@php-server sbin]# ./php-fpm 

1.5.2 nginx配置

[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.79.157;

        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.79.156:9000;
                fastcgi_index   index.php;
                fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include         fastcgi_params;
        }
    }
}

修改完配置,重启服务

[root@nginx-server ~]# systemctl restart nginx
[root@nginx-server ~]# ps -ef|grep nginx
root      22928      1  0 18:18 ?        00:00:00 nginx: master process /opt/data nginx/sbin/nginx -c /opt/data/nginx/conf/nginx.conf
nginx     22929  22928  0 18:18 ?        00:00:00 nginx: worker process
root      24415  21961  0 21:43 pts/0    00:00:00 grep --color=auto nginx

编辑测试网页
在php-server上创建测试目录

[root@php-server /]# mkdir www
[root@php-server /]# chown -R nginx:nginx www
编辑测试网页
[root@php-server /]# vim www/index.php
<?php
phpinfo();
?>

在浏览器上测试:
在这里插入图片描述

1.5.3 mysql配置

在mysql-server上创建测试账号和测试数据库。

注意:在你MySQL8上,创建用户时修改认证插件为mysql_native_password,并开放客户端。
在这里插入图片描述

mysql> create user 'superadmin'@'%' identified by 'ABC123.com';
Query OK, 0 rows affected (0.11 sec)

mysql> select user,host,plugin from mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| copy             | %         | mysql_native_password |
| root             | %         | caching_sha2_password |
| superadmin       | %         | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
6 rows in set (0.00 sec)
mysql> alter user 'superadmin'@'%' identified with mysql_native_password by 'ABC123.com';
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,plugin from mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| copy             | %         | mysql_native_password |
| root             | %         | caching_sha2_password |
| superadmin       | %         | mysql_native_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
6 rows in set (0.00 sec)

mysql> create database memcacher;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| memcacher          |
| mysql              |
| performance_schema |
| school             |
| sys                |
| test1              |
| xingyun            |
+--------------------+
8 rows in set (0.00 sec)

安装PHP-mysql链接模块

[root@php-server ~]# cd /usr/local/src/php-7.4.8/ext/
[root@php-server ~]# cd mysqli/
[root@localhost mysqli]# /usr/local/php7/bin/phpize
[root@localhost mysqli]# ./configure \
--with-php-config=/usr/local/php7/bin/php-config \
--enable-embedded-mysqli=shared \
--enable-shared
[root@localhost mysqli]# echo $?
0
[root@localhost mysqli]# make
[root@localhost mysqli]# make install
[root@localhost mysqli]# echo $?
[root@php-server mysqli]# cd /usr/local/php7/lib/php/extensions/no-debug-zts-20190902/
[root@php-server no-debug-zts-20190902]# ls
mysqli.a   opcache.a   pdo_mysql.a
mysqli.so  opcache.so  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/php7/bin/phpize
Configuring for:
PHP Api Version:         20190902
Zend Module Api No:      20190902
Zend Extension Api No:   320190902
[root@php-server pdo_mysql]# ./configure \
> --with-php-config=/usr/local/php7/bin/php-config \
> --with-pdo-mysql=mysqlnd
[root@localhost pdo_mysql]# echo $?
0
[root@localhost pdo_mysql]# make
[root@localhost pdo_mysql]# echo $?
0
[root@localhost pdo_mysql]# make install

添加模块:

[root@php-server php-7.4.8]# cp php.ini-production /usr/local/etc/php.ini
[root@php-server php-7.4.8]# cd /usr/local/php7/etc/
[root@php-server etc]# vim php.ini
[root@php-server etc]# vim php.ini
extension_dir = "/usr/local/php7/lib/php/extensions/no-debug-zts-20190902"
extension = mysqli.so
extension = pdo_mysql.so
[root@php-server etc]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@php-server etc]# ps -ef |grep php
root      51710      1  0 19:13 ?        00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nginx     51711  51710  0 19:13 ?        00:00:00 php-fpm: pool www
nginx     51712  51710  0 19:13 ?        00:00:00 php-fpm: pool www
nginx     51713  51710  0 19:13 ?        00:00:00 php-fpm: pool www
nginx     51714  51710  0 19:13 ?        00:00:00 php-fpm: pool www
nginx     51715  51710  0 19:13 ?        00:00:00 php-fpm: pool www
root      51717   9253  0 19:13 pts/1    00:00:00 grep --color=auto php

编辑mysql测试网页

[root@php-server /]# vim www/index1.php
<?php
$con = new mysqli('192.168.79.161','superadmin','ABC123.com','memcacher');
if(!$con)
 echo "faling...";
else
 echo "success connect mysql\n";
?>

在这里插入图片描述

到这里测试就结束了,谢谢大家!!!

猜你喜欢

转载自blog.csdn.net/weixin_45961525/article/details/107736486