什么是動靜分離
動靜分離主要是通過nginx+PHP-FPM來實(shí)現(xiàn),其中nginx處理圖片,html等靜態(tài)的文件,PHP處理動態(tài)程序痊银。
動靜分離是指在web服務(wù)器架構(gòu)中衣屏,將靜態(tài)頁面與動態(tài)頁面或者靜態(tài)內(nèi)容接口和動態(tài)內(nèi)容接口分開不同系統(tǒng)訪問的架構(gòu)設(shè)計(jì)方法,進(jìn)而提升整個服務(wù)訪問性能和可維護(hù)性酝静。
簡單點(diǎn)來說,就是用戶在請求的時候,如果只是簡單的訪問圖片,html等靜態(tài)的請求時,nginx直接返回,如果是發(fā)送動態(tài)請求時候,需要程序進(jìn)行就由nginx把請求發(fā)送給程序,進(jìn)行動態(tài)處理节榜。
nginx反向代理與負(fù)載均衡
nginx通常被用作后端服務(wù)器的反向代理,這樣就可以很方便的實(shí)現(xiàn)動靜分離以及負(fù)載均衡形入,從而大大提高服務(wù)器的處理能力全跨。
nginx實(shí)現(xiàn)動靜分離,其實(shí)就是在反向代理的時候亿遂,如果是靜態(tài)資源浓若,就直接從nginx發(fā)布的路徑去讀取,而不需要從后臺服務(wù)器獲取了蛇数。
但是要注意挪钓,這種情況下需要保證后端跟前端的程序保持一致,可以使用Rsync做服務(wù)端自動同步或者使用NFS耳舅、MFS分布式共享存儲碌上。
Http Proxy模塊倚评,功能很多,最常用的是proxy_pass和proxy_cache
如果要使用proxy_cache馏予,需要集成第三方的ngx_cache_purge模塊天梧,用來清除指定的URL緩存。這個集成需要在安裝nginx的時候去做霞丧,如:
./configure --add-module=../ngx_cache_purge-1.0 .....nginx通過upstream模塊來實(shí)現(xiàn)簡單的負(fù)載均衡呢岗,upstream需要定義在http段內(nèi)
在upstream段內(nèi),定義一個服務(wù)器列表蛹尝,默認(rèn)的方式是輪詢后豫,如果要確定同一個訪問者發(fā)出的請求總是由同一個后端服務(wù)器來處理,可以設(shè)置ip_hash突那,如:
upstream idfsoft.com {
ip_hash;
server 127.0.0.1:9080 weight=5;
server 127.0.0.1:8080 weight=5;
server 127.0.0.1:1111;
}
注意
:這個方法本質(zhì)還是輪詢挫酿,而且由于客戶端的ip可能是不斷變化的,比如動態(tài)ip愕难,代理早龟,翻墻等,因此ip_hash并不能完全保證同一個客戶端總是由同一個服務(wù)器來處理猫缭。
定義好upstream后拄衰,需要在server段內(nèi)添加如下內(nèi)容:
server {
location / {
proxy_pass http://idfsoft.com;
}
}
nginx實(shí)現(xiàn)負(fù)載均衡
環(huán)境說明
系統(tǒng) | IP | 服務(wù) | 主機(jī)名 |
---|---|---|---|
Redhat8.2 | 192.168.182.141 | nginx | LB |
Redhat8.2 | 192.168.182.142 | nginx | rs1 |
Redhat8.2 | 192.168.182.143 | httpd | rs2 |
先在rs1和rs2上部署web服務(wù)器
[root@RS1 ~]# yum -y install nginx
[root@RS2 ~]# yum -y install httpd
[root@RS1 ~]# systemctl start nginx.service
[root@RS2 ~]# systemctl start httpd.service
修改LB主機(jī)上nginx的配置文件
upstream webserver {
server 192.168.182.142;
server 192.168.182.143;
}
location / {
44 proxy_pass http://webservers;
45 }
[root@LB conf]# nginx -s reload
輸入LB主機(jī)上的IP實(shí)現(xiàn)負(fù)載均衡
nginx實(shí)現(xiàn)動靜分離
環(huán)境說明
系統(tǒng) | IP | 服務(wù) | 主機(jī)名 |
---|---|---|---|
Redhat8.2 | 192.168.182.141 | nginx | dr |
Redhat8.2 | 192.168.182.143 | lnmp | rs1 |
Redhat8.2 | 192.168.182.142 | httpd | rs2 |
首在rs1和rs2部署web服務(wù)的服務(wù)
rs1上部署lnmp
創(chuàng)建系統(tǒng)用戶nginx
[root@RS1 ~]# useradd -r -M -s /sbin/nologin nginx
安裝依賴環(huán)境
[root@RS1 ~]# yum -y install pcre-devel pcre gcc gcc-c++ openssl-devel zlib zlib-devel make vim wget openssl openssl-devel gd-devel
創(chuàng)建日志存放目錄
[root@RS1 ~]# mkdir -p /var/log/nginx
[root@RS1 ~]# chown nginx.nginx /var/log/nginx/
下載nginx
[root@RS1 ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
[root@RS1 ~]# tar xf nginx-1.20.1.tar.gz
[root@RS1 ~]# cd nginx-1.20.1/
[root@RS1 nginx-1.20.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
[root@RS1 nginx-1.20.1]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
[root@RS1 nginx-1.20.1]# cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=Nginx server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s quit
ExecReload=/bin/kill -HUP $MAINPID
EOF
[root@RS1 ~]# systemctl daemon-reload
[root@RS1 ~]# systemctl enable --now nginx.service
部署mysql
安裝依賴包
[root@RS1 ~]# yum -y install gcc gcc-c++ make zlib zlib-devel pcre pcre-devel openssl openssl-devel ncurses-compat-libs perl ncurses-devel cmake
創(chuàng)建用戶和組
[root@RS1 ~]# useradd -r -M -s /sbin/nologin mysql
下載MySQL的tar包
[root@RS1 ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -P /usr/local
[root@RS1 local]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
[root@RS1 local]# ln -s /usr/local/mysql-5.7.34-linux-glibc2.12-x86_64/ /usr/local/mysql
添加環(huán)境變量
[root@RS1 local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@RS1 local]# source /etc/profile.d/mysql.sh
創(chuàng)建數(shù)據(jù)存放目錄
[root@RS1 local]# mkdir -p /opt/data
[root@RS1 local]# chown -R mysql.mysql /opt/data/
初始化數(shù)據(jù)庫
[root@RS1 local]# mysqld --initialize-insecure --user mysql --datadir /opt/data/
生成配置文件
[root@RS1 ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
配置服務(wù)啟動的腳本
[root@RS1 ~]# vim /usr/local/mysql/support-files/mysql.server
basedir=/usr/local/mysql
datadir=/opt/data
配置系統(tǒng)服務(wù)使用systemctl來管理MySQL
[Unit]
Description=Mysql server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@RS1 ~]# systemctl daemon-reload
[root@RS1 ~]# systemctl enable --now mysqld.service
安裝php
需要網(wǎng)絡(luò)倉庫:curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
[root@RS1 ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd libsqlite3x-devel libzip-devel http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
[root@RS1 ~]# wget https://www.php.net/distributions/php-8.0.11.tar.gz
[root@RS1 ~]# tar xf packages/php-8.0.11.tar.xz -C /usr/local/
[root@RS1 php-8.0.11]# ./configure --prefix=/usr/local/php8 --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@RS1 php-8.0.11]# make && make install
配置環(huán)境變量
[root@RS1 ~]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@RS1 ~]# source /etc/profile.d/php.sh
[root@RS1 php-8.0.11]# cp php.ini-production /etc/php.ini
[root@RS1 php-8.0.11]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@RS1 php-8.0.11]# chmod +x /etc/rc.d/init.d/php-fpm
[root@RS1 php-8.0.11]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@RS1 php-8.0.11]#cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
[root@RS1 php-8.0.11]# service php-fpm start
配置系統(tǒng)服務(wù)使用systemctl來管理PHP
[root@RS1 ~]# cp /usr/lib/systemd/system/mysql.service /usr/lib/systemd/system/php-fpm.service
[root@RS1 ~]# cat /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php server daemon
After=network.target
[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@RS1 ~]# pkill php-fpm
[root@RS1 ~]# systemctl daemon-reload
[root@RS1 ~]# systemctl enable --now php-fpm.service
創(chuàng)建php訪問界面
[root@RS1 ~]# vim /usr/local/nginx/html/index.php
[root@RS1 ~]# cat /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
[root@RS1 ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.php index.html index.htm; //修改這一行
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $Document_Root$fastcgi_script_name; //修改這一行
include fastcgi_params;
}
[root@RS1 ~]# systemctl restart nginx.service
在rs2上安裝httpd
[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# systemctl start httpd
在LB主機(jī)上操作
[root@DR ~]# vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream static {
server 192.168.182.142; //設(shè)置靜態(tài)訪問
}
upstream dynamic {
server 192.168.182.143; //設(shè)置動態(tài)訪問
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://static; //處理靜態(tài)資源
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
location ~ \.php$ {
proxy_pass http://dynamic; //處理以.php結(jié)尾的動態(tài)資源。
}
# 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_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# 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;
# }
#}
}
[root@DR ~]# nginx -s reload