nginx實現(xiàn)動靜分離的方法示例
本文主要介紹了nginx實現(xiàn)動靜分離的方法示例,具有一定的學習價值,具體如下
環(huán)境:
| 系統(tǒng)/主機名 | IP地址 | 服務(wù) |
|---|---|---|
| Redhat8 :server1 | 192.168.244.131 | nginx |
| Redhat8:server2 | 192.168.244.133 | lnmp |
| Content7:node3 | 192.168.244.142 | httpd |
在三臺主機上關(guān)閉防火墻
[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 accou
查看端口
[root@server1 ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:220.0.0.0:* LISTEN 0 128 0.0.0.0:800.0.0.0:* LISTEN 0 128 [::]:22 [::]:*
訪問頁面

在server上部署lnmp
部署nginx
[root@server2 lnmp]# cat install.sh
#!/bin/bash
if [ $UID -ne 0 ];then
echo "Please use administrator accou
部署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
配置環(huán)境變量
[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:800.0.0.0:* LISTEN 0 128 0.0.0.0:220.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_age
訪問

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 :::*
訪問

實現(xiàn)分離部署
在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_age
訪問 192.168.244.131

在訪問 192.168.244.131/index.php

到此這篇關(guān)于nginx實現(xiàn)動靜分離的方法示例的文章就介紹到這了,更多相關(guān)nginx 動靜分離內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信