动静分离
在Web开发中,通常来说,动态资源其实就是指那些后台资源,而静态资源就是指HTML,JavaScript,CSS,img等文件。
一般来说,都需要将动态资源和静态资源分开,将静态资源部署在Nginx上,当一个请求来的时候,如果是静态资源的请求,就直接到nginx配置的静态资源目录下面获取资源,如果是动态资源的请求,nginx利用反向代理的原理,把请求转发给后台应用去处理,从而实现动静分离。
在使用前后端分离之后,可以很大程度的提升静态资源的访问速度,同时在开过程中也可以让前后端开发并行可以有效的提高开发时间,也可以有些的减少联调时间 。
环境:
主机 | IP | 服务 |
---|---|---|
master | 192.168.8.131 | nginx |
RS-1 | 192.168.8.130 | nginx,mysql,php |
RS-2 | 192.168.8.132 | nginx |
Master和RS-2上安装nginx:
nginx官网
#使用脚本安装
[root@master nginx]# tree
.
├── nginx.sh
└── soft
└── nginx-1.21.3.tar.gz
1 directory, 2 files
[root@master nginx]# vim nginx.sh
#!/bin/bash
log_dir=/var/log
install_dir=/usr/src
config_dir=/usr/local
useradd -r -M -s /sbin/nologin nginx
#安装依赖包
yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
yum -y groups mark install 'Development Tools'
#创建日志存放目录
if [ ! -d $log_dir/nginx ];then
mkdir -p $log_dir/nginx
chown -R nginx.nginx $log_dir/nginx
fi
tar xf soft/nginx-1.21.3.tar.gz -C $install_dir
#install
if [ ! -d $config_dir/nginx ];then
cd $install_dir/nginx-1.21.3
./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 && make && make install
fi
echo "export PATH=$config_dir/nginx/sbin:\$PATH" > /etc/profile.d/nginx.sh
cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now nginx
RS-1上安装LNMP架构
nginx软件包 下载
mysql软件包下载
php软件包下载
安装nginx
# 创建用户
[root@RS-1 ~]# useradd -r -M -s /sbin/nologin nginx
# 安装依赖环境
[root@RS-1 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@RS-1 ~]# yum -y groups mark install 'Development Tools'
# 创建日志存放目录
[root@RS-1 ~]# mkdir -p /var/log/nginx
[root@RS-1 ~]# chown -R nginx.nginx /var/log/nginx
# 解压安装包
[root@RS-1 src]# tar xf nginx-1.20.1.tar.gz
[root@RS-1 src]# ls
debug kernels nginx-1.20.1 nginx-1.20.1.tar.gz
# 编译安装
[root@RS-1 src]# tar xf nginx-1.20.1.tar.gz
[root@RS-1 src]# ls
debug kernels nginx-1.20.1 nginx-1.20.1.tar.gz
[root@RS-1 src]# cd nginx-1.20.1/
[root@RS-1 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@RS-1 nginx-1.20.1]# make && make install
# 配置环境变量
[root@RS-1 ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@RS-1 ~]# source /etc/profile.d/nginx.sh
#设置开机自启
[root@RS-1 ~]# cat > /usr/lib/systemd/system/nginx.service <<EOF
> [Unit]
> Description=nginx
> After=network.target
>
> [Service]
> Type=forking
> ExecStart=/usr/local/nginx/sbin/nginx
> ExecReload=/usr/local/nginx/sbin/nginx -s reload
> ExecStop=/usr/local/nginx/sbin/nginx -s quit
> PrivateTmp=true
>
> [Install]
> WantedBy=multi-user.target
> EOF
[root@RS-1 ~]# systemctl daemon-reload
[root@RS-1 ~]# systemctl enable --now nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
安装mysql
# 安装mysql所需依赖包
[root@RS-1 ~]# yum -y install ncurses-compat-libs
# 创建mysql用户
[root@RS-1 ~]# useradd -r -M -s /sbin/nologin mysql
# 解压软件包
[root@RS-1 ~]# cd /usr/src/
[root@RS-1 src]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost local]# mv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql
# 修改文件属主属组
[root@RS-1 local]# chown -R mysql.mysql /usr/local/mysql/
# 添加环境变量
[root@RS-1 local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@RS-1 local]# source /etc/profile.d/mysql.sh
# 修改文件
[root@RS-1 mysql]# ln -s /usr/local/mysql/include/ /usr/include/mysql
[root@localhost mysql]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man
MANDATORY_MANPATH /usr/local/mysql/man #添加此行
#生成配置文件
[root@RS-1 ~]# vim /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
# 设置开机自启
[root@RS-1 ~]# cat /usr/lib/systemd/system/mysqld.service
[Unit]
Description=Mysql server daemon
After=network.targe
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.serve stop
[Install]
WantedBy=multi-user.target
[root@RS-1 ~]# sed -i 's!^basedir=!basedir=/usr/local/mysql!g;s!^datadir=!datadir=/opt/data!g' /usr/local/mysql/support-files/mysql.server
[root@RS-1 ~]# systemctl daemon-reload
[root@RS-1 ~]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
# 修改myql数据库密码
[root@RS-1 ~]# mysql -uroot -e "set password=password('123')"
安装php
php软件包下载
#安装epel源
[root@RS-1 ~]# yum -y install epel-release
[root@RS-1 ~]# 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
#下载依赖软件
[root@RS-1 ~]# yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
# 解压软件包
[root@RS-1 src]# tar xf php-8.0.10.tar.gz
#编译安装
[root@localhost php-8.0.10]# ./configure --prefix=/usr/local/php --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@RS-1 php-8.0.10]# make && make install
# 设置环境变量
[root@RS-1 ~]# echo "export PATH=/usr/local/php/bin:$PATH" > /etc/profile.d/php.sh
[root@RS-1 ~]# source /etc/profile.d/php.sh
[root@RS-1 ~]# which php
/usr/local/php/bin/php
[root@RS-1 php-8.0.10]# cp /etc/php.ini /opt/
[root@RS-1 php-8.0.10]# cp php.ini-production /etc/php.ini
cp:是否覆盖'/etc/php.ini'? y
[root@RS-1 php-8.0.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@RS-1 php-8.0.10]# chmod +x /etc/rc.d/init.d/php-fpm
[root@RS-1 php-8.0.10]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@RS-1 php-8.0.10]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@RS-1 php-8.0.10]#
#设置开机自启
[root@RS-1 ~]#vim /usr/lib/systemd/system/php.service
[Unit]
Description=php-fpm 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
[Install]
WantedBy=multi-user.target
[root@RS-1 ~]# systemctl daemon-reload
[root@RS-1 ~]# systemctl enable --now php
# 修改配置文件
[root@RS-1 ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.php index.html index.htm; #添加index.php
}
#取消注释
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;
}
#创建web网页文件
[root@RS-1 html]# vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
#重启nginx
[root@RS-1 ~]# systemctl restart nginx
文件配置
[root@master ~]# vim /usr/local/nginx/conf/nginx.conf
···
upstream static {
server 192.168.8.130;
}
upstream dynamic {
server 192.168.8.132;
}
location / {
proxy_pass http://static;
}
location ~ \.php$ {
proxy_pass http://dynamic;
}
···
location / {
root html;
index index.html 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;
}
···
编写php页面
[root@RS-1 ~]# vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
访问动态页面
访问静态页面