本文需要上节博客内容作为基础
1.链接nginx与php
进入fpm路径:cd /root/php-7.4.12/sapi/fpm
拷贝模板文件:cp init.d.php-fpm /etc/init.d/php-fpm
给予执行权限:chmod +x /etc/init.d/php-fpm
执行fpm程序:/etc/init.d/php-fpm
进入目录:cd /usr/local/php/etc/
复制php-fpm配置文件:cp php-fpm.conf.default php-fpm.conf
修改配置文件:vim php-fpm.conf
进入目录:/usr/local/php/etc/php-fpm.d
复制:cp www.conf.default www.conf
编辑:vim www.conf
进入目录:cd php-7.4.12/
拷贝php生产环境文件:cp php.ini-production /usr/local/php/etc/php.ini
启动php-fpm:/etc/init.d/php-fpm start
编辑nginx配置文件:
cd /usr/local/nginx/conf/
vim nginx.conf
#access_log logs/host.access.log main;
location / {
root html;
index index.php index.html index.htm;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
添加nginx到systemd
此处参考官方文档
进入目录cd /usr/lib/systemd/system
添加文档:vim nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
重启deamon:systemctl daemon-reload
启动nginx:systemctl enable nginx.service
进入目录:cd /etc/init.d/
以chkcongfig设置启动(默认35启动):chkconfig --level 35 php-fpm on
关闭php-fpm:/etc/init.d/php-fpm stop
进入目录:cd php-7.4.12/sapi/fpm/
复制php服务端文件模板:cp php-fpm.service /usr/lib/systemd/system
修改文件vim php-fpm.service
:
注销一行:
编写测试页:cd /usr/local/nginx/html/
vim index.php
<?php
phpinfo()
?>
重新加载:systemctl daemon-reload
启动服务:systemctl start php-fpm.service
开机自启:systemctl enable php-fpm.service
测试:
2.整合数据库
2.1 添加php命令至全局
打开数据库:etc/init.d/mysqld start
将php命令添加至全局:
cd
vim .bash_profile
#添加
PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin:/usr/local/mysql/bin:/usr/local/php/bin
更新命令:source .bash_profile
此时php -m
可以显示相关命令:
2.2 用phpMyAdmin测试数据库
进入目录:cd /usr/local/php/etc/
编辑vim php.ini
:
#修改添加
pdo_mysql.default_socket=/usr/local/mysql/data/mysql.sock
mysqli.default_socket = /usr/local/mysql/data/mysql.sock
重启:systemctl reload php-fpm
安装zip解压文件:yum install unzip -y
准备phpMyAdmin文件:phpMyAdmin-5.0.2-all-languages.zip
解压:unzip phpMyAdmin-5.0.2-all-languages.zip
移动至发布页:mv phpMyAdmin-5.0.2-all-languages /usr/local/nginx/html/phpadmin
进入目录:cd /usr/local/mysql/
给data添加权限:chmod 755 data
测试:
3.添加memcache缓存模块
准备文件:memcache-4.0.5.2.tgz
解压:tar zxf memcache-4.0.5.2.tgz
安装环境:yum install -y autoconf
检测安装环境:phpize
编译:./configure --enable-memcache
make
make install
进入目录:cd /usr/local/php/etc/
添加memcache:vim php.ini
重新加载:systemctl reload php-fpm
此时完成了php与memcache.so模块之间的链接,还需安装memcache程序:yum install -y memcached
启动memcache:systemctl start memcached
memcache默认使用64M内存作为缓存,最大可设置为1024M
进入编译目录,复制一个样本文件:cp example.php /usr/local/nginx/html/
编辑此文件vim example.php
:
进入编译目录,复制主配置文件:cp memcache.php /usr/local/nginx/html
编辑此文件:
$VERSION='$Id$';
define('ADMIN_USERNAME','admin'); // Admin Username
define('ADMIN_PASSWORD','lee'); // Admin Password
define('DATE_FORMAT','Y/m/d H:i:s');
define('GRAPH_SIZE',200);
define('MAX_ITEM_DUMP',50);
$MEMCACHE_SERVERS[] = 'localhost:11211'; // add more as an array
#$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array
浏览器访问:
4.构建nginx高速缓存
以下步骤需完成上步
4.1 配置openresty
准备:oniguruma-6.8.2-1.el7.x86_64.rpm
在根目录解压:tar zxf openresty-1.19.3.1.tar.gz
编译二进制文件:gmake
安装:gmake install
此处需要先停止nginx:systemctl stop nginx
确保nginx已经停止后,进入该目录下,
cd /usr/local/openresty/nginx/conf/
vim nginx.conf
#修改以下部分:
user nginx nginx;
worker_processes auto;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
#access_log logs/host.access.log main;
location / {
root html;
index index.php index.html index.htm;
}
检查配置文件有无语法错误:/usr/local/openresty/nginx/sbin/nginx -t
开启openresty:/usr/local/openresty/nginx/sbin/nginx
4.2 构建nginx高效缓存
进入openresty发布页:cd /usr/local/openresty/nginx/html
复制模板文件cp ~/memcache-4.0.5.2/example.php .
进入配置目录文件:cd /usr/local/openresty/nginx/conf
4.3结果对比
未开启高效缓存
首先设置nginx配置文件vim nginx.conf
:
在真机上检测未开启nginx缓存时的处理速度:ab -c10 -n10000 http://172.25.38.1/example.php
可以看到未开启高效缓存时每秒处理只有2300次左右
开启高效缓存
设置nginx配置文件vim nginx.conf
:
#此处只列出修改处
# cd /usr/local/openresty/nginx/conf
# vim nginx.conf
upstream memcache {
server 127.0.0.1:11211;
keepalive 512;
}
location /memc {
internal;
memc_connect_timeout 100ms;
memc_send_timeout 100ms;
memc_read_timeout 100ms;
set $memc_key $query_string;
set $memc_exptime 300;
memc_pass memcache;
}
location ~ \.php$ {
set $key $uri$args;
srcache_fetch GET /memc $key;
srcache_store PUT /memc $key;
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
检测语法错误:/usr/local/openresty/nginx/sbin/nginx -t
重新加载:/usr/local/openresty/nginx/sbin/nginx -s reload
在真机测试速度:ab -c10 -n10000 http://172.25.38.1/example.php
5.nginx反向代理
关闭openresty代理的nginx:/usr/local/openresty/nginx/sbin/nginx -s stop
编辑nginx配置文件:vim /etc/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 2;
worker_cpu_affinity 01 10;
#pid logs/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
http {
upstream westos {
server 172.25.38.2:80;
server 172.25.38.3:80;
}
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
修改系统配置:vim /etc/security/limits.conf
准备两个虚拟机server2,server3,安装好httpd;
分别注释两个服务端主页:
echo server2 > /var/www/html/index.html
echo server3 > /var/www/html/index.html
启动两个的服务端的httpd:systemctl enable --now httpd
在真机上解析nginx反向代理服务器,查看是否负载均衡:
6.nginx平滑升级
6.1 升级
取消debug调试vim auto/cc/gcc
:
# debug
#CFLAGS="$CFLAGS -g"
配置:./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
编译:make
查看当前nginx版本:nginx -v
进入目录:cd /usr/local/nginx/sbin/
备份:mv nginx nginx.old
进入新版本目录:cd ~/nginx-1.19.1/objs/
转移新版本至目录:cp nginx /usr/local/nginx/sbin/
检测本地nginx版本:curl -I localhost
抓取nginx进程id:ps ax |grep nginx
kill -USR2 18415
,此时还未升级,但是多了nginx进程
kill -WINVH 18415
,升级完成
6.2 降级
改名:
mv nginx nginx.new
mv nginx.old nginx
kill -HUB 18415
,多出了2个子进程:
kill -WINCH 26366
,此处的数字为要关闭的新版本,关闭后只剩旧版本:
7.集群
停止所有nginx程序后,进入nginx配置目录:cd /usr/local/nginx/conf
编辑主配置文件:vim nginx.conf
#打开反向代理功能后,在最后添加
server {
listen 80;
server_name www.westos.org;
location / {
proxy_pass http://westos;
}
}
完成后重载nginx并启动nginx
在主机上添加解析:vim /etc/hosts
172.25.38.1 server1 www.westos.org
此时,在主机上解析地址即可:
再在配置文件最后添加:
server {
listen 80;
server_name www.linux.org;
location / {
root /web1;
index index.html;
}
}
在根下建立指定的目录:mkdir /web1
导入一个首页信息:echo web1 > /web1/index.html
在真机上添加解析:vim /etc/hosts
172.25.38.1 server1 www.westos.org www.linux.org
此时的结果为:在外部访问www.westos.org
时,server1上的nginx负载均衡至server2和server3上,而访问www.linux.org时,指向固定网页web1。
8.开启安全性网页
编辑nginx主配置文件
#取消注释
# HTTPS server
#
server {
listen 443 ssl;
server_name localhost;
ssl_certificate cert.pem;
ssl_certificate_key cert.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
进入目录:cd /etc/pki/tls/certs
生成证书:make cert.pem
移动证书:mv cert.pem /usr/local/nginx/conf/
此时在浏览器中浏览https://172.25.38.1
由于是个人签名,所以浏览器会提示该网页不受信任
9. nginx不同server权重
编辑nginx主配置文件:
权重默认为1:
http {
upstream westos {
server 172.25.38.2:80 weight=3;
server 172.25.38.3:80;
server 127.0.0.1:80 backup
}
include mime.types;
default_type application/octet-stream;
可以指定nginx本机为备用服务器,在所有节点挂掉时才会调用
转载:https://blog.csdn.net/Fancyll_Lee/article/details/115414343