一、架构原理介绍
(1)LNMP=Linux+nginx+mysql+PHP
(2)工作原理
首先,浏览器发送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的形式传送给浏览器。浏览器在进行解析与渲染然后进
行呈现。
(3)环境规划
主机名 | IP | 角色 |
php | 192.168.142.199 | php脚本程序解析 |
nginx | 192.168.142.141 | web服务器 |
mysql | 192.168.142.44 | 数据库节点 |
(4)节点处理
①、处理防火墙
[root@nginx sbin]# systemctl stop firewalld
[root@nginx sbin]# systemctl disable firewalld
[root@nginx sbin]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@nginx sbin]# setenforce 0
三台机子上都需要处理
②、时间同步
[root@nginx ]# yum install ntpdate -y
[root@nginx ]# ntpdate cn.pool.ntp.org
[root@nginx ]# hwclock --systohc
三台机子都需要处理
③、修改主机名
[root@nginx ~]# hostnamectl set-hostname nginx
[root@nginx ~]# su -l
Last login: Sun Jul 5 16:00:20 CST 2020 from 192.168.142.1 on pts/0
三台机子都需要处理
(5)程序安装
①、mysql程序安装
见博客
②、nginx程序安装
见博客
③、php程序安装
下载并解压软件包
[root@php ~]# wget http://php.net/distributions/php-7.2.0.tar.gz
[root@php ~]# tar xf php-7.2.0.tar.gz -C /usr/local/src/
安装依赖
[root@php ~]# yum install -y gcc zlib zlib-devel openssl openssl-devel perl perl-devel make -y
[root@php ~]# wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
[root@php ~]# yum -y install gd-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel libxslt-devel mhash
[root@php ~]# tar zxf libmcrypt-2.5.7.tar.gz
[root@php ~]# cd libmcrypt-2.5.7
[root@php libmcrypt-2.5.7]# ./configure
[root@php libmcrypt-2.5.7]# make && make install
[root@php libmcrypt-2.5.7]# echo $?
[root@php ~]# yum -y install gd-devel libjpeg libjpeg-devel libpng libpng-devel libiconv-devel freetype freetype-d autoconf
[root@php ~]# rpm -e libcurl-7.29.0-42.e17.x86_64 --nodeps
[root@php ~]# wget https://curl.haxx.se/download/curl-7.56.0.tar.gz
[root@php ~]# tar zxf curl-7.56.0.tar.gz
[root@php ~]# cd curl-7.56.0
[root@php curl-7.56.0]# ./configure --prefix=/usr/local/curl --with-ssl --with-zlib
[root@php curl-7.56.0]# make && make install
编译安装PHP
[root@php ~]# cd /usr/local/src/php-7.2.0/
[root@localhost php-7.2.0]# ./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 php-7.2.0]# make
[root@php php-7.2.0]# make install
④、LNMP环境配置与测试
《1》PHP配置
配置启动项
[root@php ~]# cd /usr/local/src/php-7.2.0/
[root@php php-7.2.0]# cp php.ini-production /usr/local/php7/etc/php.ini
[root@php php-7.2.0]# cp php.ini-production /etc/php.ini
[root@php php-7.2.0]# cd /usr/local/src/php-7.2.0/sapi/fpm/
[root@php fpm]# cp -p init.d.php-fpm /etc/init.d/php-fpm
[root@php fpm]# cd /etc/init.d/
[root@php init.d]# chmod +x php-fpm
[root@php init.d]# chkconfig php-fpm on
[root@php init.d]# vim /etc/profile
export PATH=/usr/local/php7/bin:/usr/local/php7/sbin:$PATH
[root@php init.d]# source /etc/profile
[root@php init.d]# php -v
PHP 7.2.0 (cli) (built: Jul 2 2020 12:00:00) ( ZTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies
配置php-fpm模块
[root@php init.d]# cd /usr/local/php7/etc/
[root@php etc]# cp php-fpm.conf.default php-fpm.conf
[root@php etc]# cd php-fpm.d/
[root@php php-fpm.d]# cp www.conf.default www.conf
[root@php ~]# useradd nginx
[root@php php-fpm.d]# vim /usr/local/php7/etc/php-fpm.conf
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
[root@php php-fpm.d]# 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 sbin]# cd
[root@php ~]# cd /usr/local/php7/sbin/
[root@php sbin]# ./php-fpm
[root@php sbin]# ps -ef | grep php
root 7442 1 0 17:00 ? 00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nginx 7443 7442 0 17:00 ? 00:00:00 php-fpm: pool www
nginx 7444 7442 0 17:00 ? 00:00:00 php-fpm: pool www
nginx 7445 7442 0 17:00 ? 00:00:00 php-fpm: pool www
nginx 7446 7442 0 17:00 ? 00:00:00 php-fpm: pool www
nginx 7447 7442 0 17:00 ? 00:00:00 php-fpm: pool www
root 7449 7054 0 17:00 pts/0 00:00:00 grep --color=auto php
《2》nginx配置
[root@nginx ~]# 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; #监听80端口
server_name 192.168.142.141;
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.142.199:9000; #php服务器的ip
fastcgi_index index.php ; #和访问文件名相同
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
修改完配置,重启服务
[root@nginx ~]# cd /opt/data/nginx/sbin/
[root@nginx sbin]# ./nginx
[root@nginx sbin]# ps -ef | grep nginx
root 7245 1 0 17:02 ? 00:00:00 nginx: master process ./nginx
nginx 7246 7245 0 17:02 ? 00:00:00 nginx: worker process
root 7248 7171 0 17:03 pts/0 00:00:00 grep --color=auto nginx
编辑测试网页
[root@php ~]# mkdir /www
[root@php ~]# chown -R nginx:nginx /www
[root@php ~]# cat /www/index.php
<?php
phpinfo()
?>
在浏览器上测试:
《3》 mysql配置
mysql> create user 'superadmin'@'%' identified with mysql_native_password by 'CHUYE123@chuye';
mysql> create database memcacge;
《4》安装PHP-mysql链接模块
[root@php ~]# cd /usr/local/src/php-7.2.0/ext/mysqli/
[root@php mysqli]# /usr/local/php7/bin/phpize
Configuring for:
PHP Api Version: 20170718
Zend Module Api No: 20170718
Zend Extension Api No: 320170718
[root@php mysqli]# ./configure --with-php-config=/usr/local/php7/bin/php-config --enable-embedded-mysqli=shared ---enable-shared
[root@php mysqli]# make && make install
[root@php mysqli]# cd /usr/local/php7/lib/php/extensions/no-debug-zts-20170718/
[root@php no-debug-zts-20170718]# ls
mysqli.a mysqli.so opcache.a opcache.so pdo_mysql.a pdo_mysql.so
[root@php no-debug-zts-20170718]# cd /usr/local/src/php-7.2.0/ext/pdo_mysql/
[root@php pdo_mysql]# /usr/local/php7/bin/phpize
Configuring for:
PHP Api Version: 20170718
Zend Module Api No: 20170718
Zend Extension Api No: 320170718
[root@php pdo_mysql]#./configure --with-php-config=/usr/local/php7/bin/php-config --with-pdo-mysql=mysqlnd
[root@php pdo_mysql]#make && make install
添加模块
[root@php pdo_mysql]# cd /usr/local/src/php-7.2.0/
[root@php php-7.2.0]# cd /usr/local/php7/etc/
[root@php etc]# vim php.ini
extension_dir = "/usr/local/php7/lib/php/extensions/no-debug-zts-20170718"
extension = mysqli.so
extension = pdo_mysql.so
[root@php etc]# service php-fpm restart
[root@php etc]# ps -ef | grep php
root 8085 1 0 17:31 ? 00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nginx 8086 8085 0 17:31 ? 00:00:00 php-fpm: pool www
nginx 8087 8085 0 17:31 ? 00:00:00 php-fpm: pool www
nginx 8088 8085 0 17:31 ? 00:00:00 php-fpm: pool www
nginx 8089 8085 0 17:31 ? 00:00:00 php-fpm: pool www
nginx 8090 8085 0 17:31 ? 00:00:00 php-fpm: pool www
root 8092 7054 0 17:31 pts/0 00:00:00 grep --color=auto php
编辑mysql测试网页
[root@php etc]# cat /www/index1.php
<?php
$con = new mysqli('192.168.142.44','superadmin','CHUYE123@chuye','memcacher');
if(!$con)
echo "faling...";
else
echo "success connect mysql\n";
?>
在浏览器上测试:
至此,LNMP搭建成功