nginx实现动静分离的方法示例
Nginx的静态处理能力很强,但是动态处理能力不足,因此,在企业中常用动静分离技术,本文就详细的介绍一下如何使用,感兴趣的可以了解一下目录
[*]在server1上部署nginx
[*]在server上部署lnmp
[*]node3部署httpd
[*]实现分离部署
本文主要介绍了nginx实现动静分离的方法示例,具有一定的学习价值,具体如下
环境:
系统/主机名IP地址服务Redhat8 :server1192.168.244.131nginxRedhat8:server2192.168.244.133lnmpContent7:node3192.168.244.142httpd在三台主机上关闭防火墙
# systemctl stop firewalld
# systemctl disable firewalld
# vim /etc/selinux/config
SELINUX=disabled
在server1上部署nginx
# 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
Description=Nginx server daemon
Wants=sshd-keygen.target
Type=forking
ExecStart=$dir_a/nginx/sbin/nginx
ExecStop=$dir_a/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now nginx
查看端口
# 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 [::]:*
访问页面
在server上部署lnmp
部署nginx
# 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
Description=Nginx server daemon
Wants=sshd-keygen.target
Type=forking
ExecStart=$dir_a/nginx/sbin/nginx
ExecStop=$dir_a/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now nginx部署mysql
# 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
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
Description=mysql server daemon
After=network.target
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
WantedBy=multi-user.target
EOF
systemctldaemon-reload
systemctlenable --nowmysqld部署php
https://www.php.net/distributions/php-8.0.10.tar.xz
解压
# tar -xfphp-8.0.10.tar.gz-C /usr/local/
安装依赖包
# wget http://mirrors.aliyun.com/repo/epel-7.repo
# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-develpcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-develphp-mysqlndlibsqlite3x-devel libzip-devel
# 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
编译安装
# cd /usr/local/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.
# make
.......
.......
.......
invertedregexiterator.inc
pharcommand.inc
phar.inc
Build complete.
Don"t forget to run "make test".
# 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
# cp /etc/php.ini /opt/
# cp php.ini-production /etc/php.ini
cp: overwrite "/etc/php.ini"? y
# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm
# cd..
# cdphp8/
# cdetc/
# cp php-fpm.conf.default php-fpm.conf
# cp php-fpm.d/www.conf.default php-fpm.d/www.conf
# cdphp-fpm.d
# ls
www.confwww.conf.default配置环境变量
#echo "export PATH=/usr/local/php8/bin:$PATH" > /etc/profile.d/php.sh
# cat /etc/profile.d/php.sh
export PATH=/usr/local/php8/bin:$PATH
# source /etc/profile.d/php.sh
# which php
/usr/local/php8/bin/php编写service文件
# cat /usr/lib/systemd/system/php-fpm.service
Description=php-fpm server daemon
After=network.target
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID
# systemctldaemon-reload启动php
# systemctl start php-fpm
# 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里配置虚拟主机
# cd/usr/local/nginx/html/
# ls
50x.htmlindex.html
# vim index.php
# cat index.php
<?php
phpinfo();
?>
# pwd
/usr/local/nginx/conf
# vimnginx.conf
........
http {
include mime.types;
default_typeapplication/octet-stream;
log_formatmain"$remote_addr - $remote_user [$time_local] "$request" "
"$status $body_bytes_sent "$http_referer" "
""$http_user_agent" "$http_x_forwarded_for"";
access_loglogs/access.logmain;
sendfile on;
......
location / {
root html;
indexindex.php index.html index.htm;
}
.....
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME $Document_root$fastcgi_script_name;
include fastcgi_params;
}
# nginx -s reload
访问
node3部署httpd
# yum -y install httpd
启动
# systemctl start httpd
# 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 :::* 访问
实现分离部署
在server1上的nginx.conf上配置
# cat /usr/local/nginx/conf/nginx.conf
#usernobody;
worker_processes1;
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
#pid logs/nginx.pid;
events {
worker_connections1024;
}
http {
include mime.types;
default_typeapplication/octet-stream;
#log_formatmain"$remote_addr - $remote_user [$time_local] "$request" "
# "$status $body_bytes_sent "$http_referer" "
# ""$http_user_agent" "$http_x_forwarded_for"";
#access_loglogs/access.logmain;
sendfile on;
#tcp_nopush on;
#keepalive_timeout0;
keepalive_timeout65;
#gzipon;
upstream cm { #静态资源地址
server 192.168.244.142;
}
upstream nm { #动态资源地址
server 192.168.244.133;
}
server {
listen 80;
server_namelocalhost;
#charset koi8-r;
#access_loglogs/host.access.logmain;
location / {
proxy_pass http://cm; #指向静态
}
#error_page404 /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;
}
# nginx -s reload访问 192.168.244.131
在访问 192.168.244.131/index.php
到此这篇关于nginx实现动静分离的方法示例的文章就介绍到这了,更多相关nginx 动静分离内容请搜索CodeAE代码之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持CodeAE代码之家!
原文链接:https://blog.csdn.net/YSTWD_WY/article/details/121067849
http://www.zzvips.com/article/204090.html
页:
[1]