nginx实现动静分离的方法示例


Posted in Servers onNovember 07, 2021

本文主要介绍了nginx实现动静分离的方法示例,具有一定的学习价值,具体如下

环境:

 

系统/主机名 IP地址 服务
Redhat8 :server1 192.168.244.131 nginx
Redhat8:server2 192.168.244.133 lnmp
Content7:node3 192.168.244.142 httpd

在三台主机上关闭防火墙

[root@server1 ~]# systemctl stop firewalld
[root@server1 ~]# systemctl disable firewalld
[root@server1 ~]# vim /etc/selinux/config 
SELINUX=disabled

在server1上部署nginx

[root@server1 opt]# cat nginx.sh 
#!/bin/bash

if [ $UID -ne 0 ];then
        echo "Please use administrator account"
        exit
fi

app_a=nginx-1.20.1.tar.gz

dir_a=/usr/local
dir_b=/var/log
dir_c=nginx-1.20.1

if [ ! -d $dir_b/nginx ];then
        mkdir -p $dir_b/nginx
fi
chown -R nginx.nginx $dir_b/nginx

yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++  make
yum -y groups mark install 'Development Tools'

id nginx &>/dev/null 
if [ $? -ne 0 ];then
        useradd -r -M -s /sbin/nologin nginx
fi

tar xf bag/$app_a -C $dir_a

cd  $dir_a/$dir_c
if [ ! -d $dir_a/nginx ];then
        ./configure \
                --prefix=$dir_a/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=$dir_b/nginx/access.log \
                --error-log-path=$dir_b/nginx/error.log  && make  && make install
fi

cd ..
if [ ! -f /etc/profile.d/nginx.sh  ];then
        echo "export PATH=$dir_a/nginx/sbin:\$PATH" > /etc/profile.d/nginx.sh
fi

cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=Nginx server daemon
Wants=sshd-keygen.target

[Service]
Type=forking
ExecStart=$dir_a/nginx/sbin/nginx
ExecStop=$dir_a/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF


systemctl daemon-reload
systemctl enable --now nginx

查看端口

[root@server1 ~]# ss -antl
State    Recv-Q   Send-Q       Local Address:Port                     Peer Address:Port                 
LISTEN   0        128                0.0.0.0:22                            0.0.0.0:*                    
LISTEN   0        128                0.0.0.0:80                            0.0.0.0:*                    
LISTEN   0        128                   [::]:22                               [::]:*

访问页面

nginx实现动静分离的方法示例

在server上部署lnmp

部署nginx

[root@server2 lnmp]# cat install.sh 
#!/bin/bash

if [ $UID -ne 0 ];then
        echo "Please use administrator account"
        exit
fi

app_a=nginx-1.20.1.tar.gz

dir_a=/usr/local
dir_b=/var/log
dir_c=nginx-1.20.1

if [ ! -d $dir_b/nginx ];then
        mkdir -p $dir_b/nginx
fi
chown -R nginx.nginx $dir_b/nginx

yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++  make
yum -y groups mark install 'Development Tools'

id nginx &>/dev/null 
if [ $? -ne 0 ];then
        useradd -r -M -s /sbin/nologin nginx
fi

tar xf bag/$app_a -C $dir_a

cd  $dir_a/$dir_c
if [ ! -d $dir_a/nginx ];then
        ./configure \
                --prefix=$dir_a/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=$dir_b/nginx/access.log \
                --error-log-path=$dir_b/nginx/error.log  && make  && make install
fi

cd ..
if [ ! -f /etc/profile.d/nginx.sh  ];then
        echo "export PATH=$dir_a/nginx/sbin:\$PATH" > /etc/profile.d/nginx.sh
fi

cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=Nginx server daemon
Wants=sshd-keygen.target

[Service]
Type=forking
ExecStart=$dir_a/nginx/sbin/nginx
ExecStop=$dir_a/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now nginx

部署mysql

[root@server2 lnmp]# cat mysql.sh 
#!/bin/bash

if [ $UID -ne 0 ];then
        echo "root?"
        exit
fi

dir_a=/usr/local
dir_b=/opt/data
app_a=mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
app_b=mysql-5.7.34-linux-glibc2.12-x86_64

id mysql  &>/dev/null
if [ $? -ne 0 ];then
        useradd -r -M -s /sbin/nologin mysql
 fi


yum -y install ncurses-compat-libs ncurses-devel openssl-devel openssl cmake mariadb-devel 


if [ ! -d $dir_a/$app_b ];then
        tar xf bag/$app_a -C $dir_a
fi

if [ ! -d $dir_a/mysql ];then
        ln -sv $dir_a/$app_b  $dir_a/mysql
fi
chown -R mysql:mysql $dir_a/mysql*

echo "export PATH=$dir_a/mysql/bin:\$PATH" > /etc/profile.d/mysql.sh

source /etc/profile.d/mysql.sh

if [ ! -d /$dir_b ];then
        mkdir -p /$dir_b 
        chown -R mysql.mysql /$dir_b
fi


content=$(ls $dir_b | wc -l)
if [ $content -eq 0  ];then
        mysqld --initialize-insecure --user mysql --datadir $dir_b
fi

cat > /etc/my.cnf <<EOF
[mysqld]
basedir = $dir_a/mysql    
datadir = $dir_b           
socket = /tmp/mysql.sock      
port = 3306                   
pid-file = $dir_b/mysql.pid
user = mysql                  
skip-name-resolve
EOF



sed -ri "s#^(basedir=).*#\1$dir_a/mysql#g" $dir_a/mysql/support-files/mysql.server
sed -ri "s#^(datadir=).*#\1$dir_b#g" $dir_a/mysql/support-files/mysql.server


cat > /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=mysql server daemon
After=network.target 

[Service]
Type=forking

ExecStart=$dir_a/mysql/support-files/mysql.server start
ExecStop=$dir_a/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF

systemctl  daemon-reload
systemctl  enable --now  mysqld

部署php

https://www.php.net/distributions/php-8.0.10.tar.xz

解压

[root@server2 ~]# tar -xf  php-8.0.10.tar.gz  -C /usr/local/

安装依赖包

[root@server2 ~]# wget http://mirrors.aliyun.com/repo/epel-7.repo
[root@server1 ~]# 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@server2 ~]# 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@server2 ~]# cd /usr/local/php-8.0.10/
[root@server2 php-8.0.10]# ./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
......
......
......
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE. By continuing this installation  |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.


[root@server2 php-8.0.10]# make
.......
.......
.......
invertedregexiterator.inc
pharcommand.inc
phar.inc

Build complete.
Don't forget to run 'make test'.

[root@server2 php-8.0.10]# make install
......
......
/root/php-8.0.10/build/shtool install -c ext/phar/phar.phar /usr/local/php8/bin/phar.phar
ln -s -f phar.phar /usr/local/php8/bin/phar
Installing PDO headers:           /usr/local/php8/include/php/ext/pdo/

配置php-fpm

[root@server2 php-8.0.10]# cp /etc/php.ini /opt/
[root@server2 php-8.0.10]# cp php.ini-production /etc/php.ini 
cp: overwrite '/etc/php.ini'? y
[root@server2 php-8.0.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@server2 php-8.0.10]# chmod +x /etc/init.d/php-fpm
[root@server2 php-8.0.10]# cd  ..
[root@server2 local]# cd  php8/
[root@server2 php8]# cd  etc/
[root@server2 etc]# cp php-fpm.conf.default php-fpm.conf
[root@server2 etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf
[root@server2 etc]# cd  php-fpm.d
[root@server2 php-fpm.d]# ls
www.conf  www.conf.default

配置环境变量

[root@server2 ~]#  echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@server2 ~]# cat /etc/profile.d/php.sh
export PATH=/usr/local/php8/bin:$PATH
[root@server2 ~]# source /etc/profile.d/php.sh
[root@server2 ~]# which php
/usr/local/php8/bin/php

编写service文件

[root@server2 ~]# cat /usr/lib/systemd/system/php-fpm.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 $MAINPID

[Install]
[root@server2 ]# systemctl  daemon-reload

启动php

[root@server2 ~]# systemctl start php-fpm
[root@server2 ~]# ss -antl
State    Recv-Q   Send-Q       Local Address:Port                     Peer Address:Port                 
LISTEN   0        128              127.0.0.1:9000                          0.0.0.0:*                    
LISTEN   0        128                0.0.0.0:80                            0.0.0.0:*                    
LISTEN   0        128                0.0.0.0:22                            0.0.0.0:*                    
LISTEN   0        80                       *:3306                                *:*                    
LISTEN   0        128                   [::]:22                               [::]:*

在nginx.conf里配置虚拟主机

[root@server2 ~]# cd  /usr/local/nginx/html/
[root@server2 html]# ls
50x.html  index.html
[root@server2 html]# vim index.php
[root@server2 html]# cat index.php 
<?php
    phpinfo();
?>
[root@server2 conf]# pwd
/usr/local/nginx/conf
[root@server2 conf]# vim  nginx.conf
........
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;

......
        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@server2 conf]# nginx -s reload

访问

nginx实现动静分离的方法示例

node3部署httpd

[root@node3 ~]# yum -y install httpd

启动

[root@node3 ~]# systemctl start httpd
[root@node3 ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128     *:22                  *:*                  
LISTEN     0      100    127.0.0.1:25                  *:*                  
LISTEN     0      128    :::80                 :::*                  
LISTEN     0      128    :::22                 :::*                  
LISTEN     0      100       ::1:25                 :::*

访问

nginx实现动静分离的方法示例

实现分离部署

在server1上的nginx.conf上配置

[root@server1 ~]# cat /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 cm {                #静态资源地址
        server 192.168.244.142;
    }
    
    upstream nm {                #动态资源地址
        server 192.168.244.133;
    }


    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://cm;     #指向静态
        }
        
        #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://nm;
        }

[root@server1 ~]# nginx -s reload

访问 192.168.244.131

nginx实现动静分离的方法示例

在访问 192.168.244.131/index.php

nginx实现动静分离的方法示例

到此这篇关于nginx实现动静分离的方法示例的文章就介绍到这了,更多相关nginx 动静分离内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Servers 相关文章推荐
nginx前后端同域名配置的方法实现
Mar 31 Servers
Nginx配置并兼容HTTP实现代码解析
Mar 31 Servers
Nginx Rewrite使用场景及配置方法解析
Apr 01 Servers
Apache Calcite 实现方言转换的代码
Apr 24 Servers
Nginx防盗链与服务优化配置的全过程
Jan 18 Servers
Nginx实现会话保持的两种方式
Mar 18 Servers
Linux、ubuntu系统下查看显卡型号、显卡信息详解
Apr 07 Servers
Tomcat starup.bat 脚本实现开机自启动
Apr 20 Servers
Windows Server 2008 修改远程登录端口以及配置防火墙
Apr 28 Servers
vscode远程免密登入Linux服务器的配置方法
Jun 28 Servers
使用 DataAnt 监控 Apache APISIX的原理解析
Jul 07 Servers
ubuntu开机后ROS程序自启动问题
Dec 24 Servers
Nginx内网单机反向代理的实现
Nov 07 #Servers
zabbix自定义监控nginx状态实现过程
总结高并发下Nginx性能如何优化
Rhit高效可视化Nginx日志查看工具
Nginx+Tomcat负载均衡集群的实现示例
Nginx反向代理学习实例教程
使用Nginx搭载rtmp直播服务器的方法
Oct 16 #Servers
You might like
php错误、异常处理机制(补充)
2012/05/07 PHP
实测在class的function中include的文件中非php的global全局环境
2013/07/15 PHP
php简单实现MVC
2015/02/05 PHP
php curl 上传文件代码实例
2015/04/27 PHP
PHP设计模式之观察者模式实例
2016/02/22 PHP
PHP基于openssl实现非对称加密代码实例
2020/06/19 PHP
在jQuery 1.5中使用deferred对象的代码(翻译)
2011/03/10 Javascript
使用原生javascript创建通用表单验证——更锋利的使用dom对象
2011/09/13 Javascript
对Web开发中前端框架与前端类库的一些思考
2015/03/27 Javascript
javascript的正则匹配方法学习
2016/02/24 Javascript
解析浏览器端的AJAX缓存机制
2016/06/21 Javascript
jQuery学习笔记之回调函数
2016/08/15 Javascript
JavaScript插件Tab选项卡效果
2017/11/14 Javascript
vue内置指令详解
2018/04/03 Javascript
详解使用vue-admin-template的优化历程
2018/05/20 Javascript
重新认识vue之事件阻止冒泡的实现
2018/08/02 Javascript
vue.js实现的幻灯片功能示例
2019/01/18 Javascript
Vue Element UI + OSS实现上传文件功能
2019/07/31 Javascript
JavaScript基于面向对象实现的无缝滚动轮播示例
2020/01/17 Javascript
[05:37]DOTA2-DPC中国联赛 正赛 Elephant vs iG 选手采访
2021/03/11 DOTA
pycharm 使用心得(一)安装和首次使用
2014/06/05 Python
在Gnumeric下使用Python脚本操作表格的教程
2015/04/14 Python
利用Python实现网络测试的脚本分享
2017/05/26 Python
Python搜索引擎实现原理和方法
2017/11/27 Python
Python排序搜索基本算法之归并排序实例分析
2017/12/08 Python
PyCharm 配置远程python解释器和在本地修改服务器代码
2019/07/23 Python
python防止随意修改类属性的实现方法
2019/08/21 Python
Keras设置以及获取权重的实现
2020/06/19 Python
详解基于 Canvas 手撸一个六边形能力图
2019/09/02 HTML / CSS
kmart凯马特官网:美国最大的打折零售商和全球最大的批发商之一
2016/11/17 全球购物
台湾旅游网站:雄狮旅游网
2017/08/16 全球购物
美国最值得信赖的宠物药房:Allivet
2019/03/23 全球购物
PHP如何设置和取得Cookie值
2015/06/30 面试题
公司门卫的岗位职责
2014/02/19 职场文书
会计工作决心书
2014/03/11 职场文书
错误码NET::ERR_CERT_DATE_INVALID证书已过期解决方法?
2022/07/07 数码科技