nginx+php,nginx+tomcat動靜分離實戰


1. 動靜分離實戰

1.1.1 nginx+tomcat 動靜分離

主機 用途
10.0.0.63 tomcat服務器
10.0.0.64 nginx服務器

1.1.2 安裝 java+tomcat環境[10.0.0.63]

1.tomcat配置:
mkdir /server/tools
cd /server/tools
wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.34/bin/apache-tomcat-9.0.34.tar.gz
tar xf apache-tomcat-9.0.34.tar.gz -C /application/
ln -s /application/apache-tomcat-9.0.34 /application/tomcat

2. java環境配置:
   rpm -ivh https://www.chenleilei.net/soft/jdk-8u121-linux-x64.rpm  [可能要等很久,可以先下載再安裝]
   
[root@master tools]# rpm -ivh https://www.chenleilei.net/soft/jdk-8u121-linux-x64.rpm
Retrieving https://www.chenleilei.net/soft/jdk-8u121-linux-x64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:jdk1.8.0_121-2000:1.8.0_121-fcs  ################################# [100%]
Unpacking JAR files...
	tools.jar...
	plugin.jar...
	javaws.jar...
	deploy.jar...
	rt.jar...
	jsse.jar...
	charsets.jar...
	localedata.jar...

3. 啟動tomcat:
/application/tomcat/bin/startup.sh

#查看進程
[root@master tomcat]# ps -ef|grep java
root       4005      1 56 19:20 pts/0    00:00:02 /usr/bin/java -Djava.util.logging.config.file=/application/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/application/tomcat -Dcatalina.home=/application/tomcat -Djava.io.tmpdir=/application/tomcat/temp org.apache.catalina.startup.Bootstrap start
root       4036   1390  0 19:20 pts/0    00:00:00 grep --color=auto java

1.1.3 安裝nginx環境[10.0.0.64]

useradd  www -u 1200 -M -s /sbin/nologin
mkdir -p /var/log/nginx
yum install -y cmake pcre pcre-devel openssl openssl-devel gd-devel \
    zlib-devel gcc gcc-c++ net-tools iproute telnet wget curl &&\
    yum clean all && \
    rm -rf /var/cache/yum/*
mkdir -p /server/tools
cd /server/tools
wget https://www.chenleilei.net/soft/nginx-1.16.1.tar.gz
tar xf nginx-1.16.1.tar.gz
cd nginx-1.16.1
./configure --prefix=/usr/local/nginx --with-http_image_filter_module --user=www --group=www \
    --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module \
    --pid-path=/var/run/nginx/nginx.pid
make -j 4 && make install && \
    rm -rf /usr/local/nginx/html/*  && \
    echo "nginx daemo hello" >/usr/local/nginx/html/index.html  && \
echo "export PATH=$PATH:/usr/local/nginx/sbin" >>/etc/profile
chown -R www.www /var/log/nginx /usr/local/nginx
source /etc/profile
nginx -s reload
ps -ef|grep nginx

1.1.4 nfs配置[10.0.0.64]

[root@k8s-master2 webapps]# cat /etc/exports
/application/tomcat/webapps 10.0.0.0/24(rw,sync,no_root_squash)
#這里是配置任何人可讀可寫.

重新啟動nfs服務
systemctl restart nfs.service rpcbind.service

1.1.5 tomcat服務器掛載NFS目錄

本機掛載本機 沒辦法一樣的目錄掛載 所以改成了webapps1 然后掛載為網頁目錄 /application/tomcat/webapps
其他服務器同樣的掛載方式

[]#  mount -t nfs 10.0.0.64:/application/tomcat/webapps1 /application/tomcat/webapps
[root@master tools]# cd /application/tomcat/webapps/
[root@master webapps]# ls
docs  examples  host-manager  manager  ROOT

[root@master tools]# cd /application/tomcat/webapps1/
[root@master webapps1]# ls
docs  examples  host-manager  manager  ROOT


簡單測試:
10.0.0.63:
[root@master webapps]# touch leilei
[root@master webapps]# ls
docs  examples  host-manager  leilei  manager  ROOT

10.0.0.64:
[root@k8s-master2 webapps]# ls
docs  examples  host-manager  leilei  manager  ROOT     #看到創建成功就沒什么問題了,說明現在兩台服務器使用的都是一個共享目錄 /application/tomcat/webapps/ROOT. 這里面就有jsp頁面.用於測試tomcat.動態頁面.

1.1.6 nginx反向代理tomcat,配置動靜分離[10.0.0.64]

[root@k8s-master2 nginx]# egrep -v "#|^$" conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream tomcat {
	server 10.0.0.63:8080;
	}
    server {
        listen       80;
        server_name  10.0.0.64;
	location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf)$ {
		root /application/tomcat/webapps/ROOT/;
	}
	location ~ \.jsp$ {
    		proxy_set_header Host $host;
    		proxy_set_header X-Real-IP $remote_addr;
    		proxy_set_header REMOTE-HOST $remote_addr;
    		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    		proxy_pass http://tomcat;
	}
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

1.1.7 成功結果

1.2 nignx+php 通過TCP連接實現動靜分離實驗

實驗環境:

主機 用途
10.0.0.63 nginx服務器
10.0.0.64 php服務器
通過TCP連接實現動靜分離 實際上就是通過9000端口解析php文件

1. 如果php和nginx不在同一台服務器,那么就需要雙方都有nginx的用戶,和用戶uid,以及相同目錄.這樣才能夠請求解析成功
2. 同時 www.conf 文件中的127.0.0.1:9000 需要改為 0.0.0.0:9000 用於監聽所有,或者指定php服務器的ip地址.

useradd www -u 1200 -M -s 
mkdir -p /usr/local/nginx/html
chown -R www.www /usr/local/nginx/html
chmod -R 755 /usr/local/nginx/html

這里我們規划網頁: /usr/local/nginx/html
在php環境中也需要創建該目錄,或者直接使用NFS來處理數據一致性問題.

1.2.1 安裝PHP [10.0.0.64]

yum install -y curl && \
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo && \
yum clean all && \
yum remove oniguruma-devel -y && \
yum remove oniguruma -y && \
rpm -ivh https://www.chenleilei.net/soft/docker/oniguruma-6.7.0-1.el7.x86_64.rpm && \
rpm -ivh https://www.chenleilei.net/soft/docker/oniguruma-devel-6.7.0-1.el7.x86_64.rpm && \
yum install -y cmake pcre pcre-devel openssl openssl-devel gd-devel \
zlib-devel gcc gcc-c++ net-tools iproute telnet wget curl \
wget vim libxml2 libxml2-devel openssl openssl-devel libjpeg-turbo \
libjpeg-turbo-devel libpng-devel libpng freetype-devel freetype icu \
libicu-devel libicu libmcrypt libmcrypt-devel libxslt libxslt-devel  php-mysql libsqlite3x-devel && \
rm -rf /var/cache/yum/*
useradd www -u 1200 -M -s /sbin/nologin && \
mkdir -p /usr/local/nginx/html && \
chown -R www.www /usr/local/nginx/html && chmod -R 755 /usr/local/nginx/html && \
wget https://www.chenleilei.net/soft/php-7.4.3.tar.gz  && \
tar xf php-7.4.3.tar.gz && \
cd php-7.4.3 && \
./configure --prefix=/usr/local/php-7.4.3 \
--with-config-file-path=/usr/local/php-7.4.3/etc \
--with-config-file-scan-dir=/usr/local/php-7.4.3/conf.d \
--enable-fpm --with-fpm-user=www \
--with-fpm-group=www \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-zlib \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--enable-opcache \
--enable-intl \
--with-xsl && \
make -j 4 && make install && \
cp ./sapi/fpm/php-fpm.conf /usr/local/php-7.4.3/etc/  && \
cp php.ini-production /usr/local/php-7.4.3/etc/php.ini && \
cp /usr/local/php-7.4.3/etc/php-fpm.d/www.conf.default /usr/local/php-7.4.3/etc/php-fpm.d/www.conf && \
sed  -i  "s#127.0.0.1:9000#0.0.0.0:9000#g" /usr/local/php-7.4.3/etc/php-fpm.d/www.conf  && \
sed -in '99idaemonize = yes' /usr/local/php-7.4.3/etc/php-fpm.conf && \
cp /php-7.4.3/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && \
chmod a+x /etc/init.d/php-fpm && \
rm -rf ../php-7.4.3.tar.gz && \
 /etc/init.d/php-fpm start

1.2.2 安裝Nginx [10.0.0.65]

useradd  www -u 1200 -M -s /sbin/nologin
mkdir -p /var/log/nginx
yum install -y cmake pcre pcre-devel openssl openssl-devel gd-devel \
    zlib-devel gcc gcc-c++ net-tools iproute telnet wget curl &&\
    yum clean all && \
    rm -rf /var/cache/yum/*
mkdir -p /server/tools
cd /server/tools
wget https://www.chenleilei.net/soft/nginx-1.16.1.tar.gz
tar xf nginx-1.16.1.tar.gz
cd nginx-1.16.1
./configure --prefix=/usr/local/nginx --with-http_image_filter_module --user=www --group=www \
    --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module \
    --pid-path=/var/run/nginx/nginx.pid
make -j 4 && make install && \
    rm -rf /usr/local/nginx/html/*  && \
    echo "nginx daemo hello" >/usr/local/nginx/html/index.html  && \
echo "export PATH=$PATH:/usr/local/nginx/sbin" >>/etc/profile
chown -R www.www /var/log/nginx /usr/local/nginx
source /etc/profile
nginx -s reload
ps -ef|grep nginx

1.2.3 TCP動靜分離配置要點

nginx開啟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;
}


注意: 如果nginx php不在同一台服務器,這里需要指向php服務器地址,並且 php服務器需要有nginx的html目錄.
建議做 NFS,讓php服務器nginx服務器的 html目錄內容保持一致,這里還有個問題就是.php服務器也需要有nginx的運行用戶
且他們的UID PID相同. 因為經過遠程調用還是使用的nginx去請求的,對端如果沒有nginx用戶,則無法請求到資源.
所以php服務器需要執行:  useradd  www -u 1200 -M -s /sbin/nologin

1.2.4 安裝NFS [做數據一致性]

在php nginx任意一台配置一個nfs都可以

這里就選擇在 php [10.0.0.64]服務器上配置一個nfs 吧
1. 檢查是否安裝
rpm -aq nfs-utils rpcbind

2. 安裝nfs服務
yum install nfs-utils rpcbind -y

3. 檢查rcpbind狀態
systemctl status rpcbind

4. 檢查nfs狀態
systemctl status nfs

5. 開機啟動nfs 
systemctl enabled nfs

6. 配置NFS
 mkdir /usr/local/nginx/html -p
 chown -R www.www /usr/local/nginx/html
 
 編輯 /etc/exports 改為下面cat的內容
 [root@k8s-master2 php-7.4.3]# cat /etc/exports
 /usr/local/nginx/html 10.0.0.0/24(rw,sync,all_squash,anonuid=1200,anongid=1200)

7. 啟動rpcbind,nfs
  systemctl status rpcbind
  systemctl start nfs
  
8. 檢查是否能夠獲取到掛載:
  [root@k8s-master2 php-7.4.3]# showmount -e 127.0.0.1
  Export list for 127.0.0.1:
  /usr/local/nginx/html 10.0.0.0/24

9. 由於我們是在 PHP服務器 上配置的NFS,我們還需要在nginx上掛載這個NFS磁盤.
   安裝相關軟件: yum install nfs-utils rpcbind -y
   檢查是否檢測到共享的目錄:
   [root@k8s-node1 html]# showmount -e 10.0.0.64
   Export list for 10.0.0.64:
   /usr/local/nginx/html 10.0.0.0/24

    掛載nfs目錄到本地:
    mount -t nfs 10.0.0.64:/usr/local/nginx/html /usr/local/nginx/html
    systemctl restart nfs rpcbind
    
10. 檢查同步
    10.0.0.64服務器 /usr/local/nginx/html 創建 chenleilei.txt
    touch /usr/local/nginx/html/chenleilei.txt
    
    10.0.0.65服務器檢查 /usr/local/nginx/html 是否創建了 chenleilei.txt
    [root@k8s-node1 html]# ls /usr/local/nginx/html
    chenleilei.txt
    檢查無誤.

1.2.5 php解析配置

echo '<?php phpinfo(); ?>' > /usr/local/nginx/html/index.php

1.2.6 nginx配置php解析

1. 添加php的默認頁:
index  index.php index.html index.htm;

2. 增加php解析:
location ~ \.php$ {
    root           html;
    fastcgi_pass   10.0.0.64:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

1.2.7 成功的結果

訪問 ip/index.php測試

1.3 ninx+php通過soket文件實現動靜分離

通過soket實現動靜分離,它沒有網絡開銷,速度更快. 
缺點: 通過socket的方式雖然可以實現更快的解析.但是,php和nginx必須在同一台服務器中.
所以這個實驗都是在 10.0.0.64 服務器中配置完成.

1.3.1 安裝php

1. 安裝php
yum install -y curl
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum remove oniguruma-devel -y
yum remove oniguruma -y
rpm -ivh https://www.chenleilei.net/soft/docker/oniguruma-6.7.0-1.el7.x86_64.rpm
rpm -ivh https://www.chenleilei.net/soft/docker/oniguruma-devel-6.7.0-1.el7.x86_64.rpm
yum install -y cmake pcre pcre-devel openssl openssl-devel gd-devel \
zlib-devel gcc gcc-c++ net-tools iproute telnet wget curl \
wget vim libxml2 libxml2-devel openssl openssl-devel libjpeg-turbo \
libjpeg-turbo-devel libpng-devel libpng freetype-devel freetype icu \
libicu-devel libicu libmcrypt libmcrypt-devel libxslt libxslt-devel  php-mysql libsqlite3x-devel && \
rm -rf /var/cache/yum/*
useradd www -u 1200 -M -s /sbin/nologin && mkdir -p /usr/local/nginx/html && chown -R www.www /usr/local/nginx/html && chmod -R 755 /usr/local/nginx/html
wget https://www.chenleilei.net/soft/php-7.4.3.tar.gz
tar xf php-7.4.3.tar.gz
WORKDIR php-7.4.3
./configure --prefix=/usr/local/php-7.4.3 \
--with-config-file-path=/usr/local/php-7.4.3/etc \
--with-config-file-scan-dir=/usr/local/php-7.4.3/conf.d \
--enable-fpm --with-fpm-user=www \
--with-fpm-group=www \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-zlib \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--enable-opcache \
--enable-intl \
--with-xsl
make -j 4 && make install
cp ./sapi/fpm/php-fpm.conf /usr/local/php-7.4.3/etc/php-fpm.conf && \
cp /php-7.4.3/php.ini-production /usr/local/php-7.4.3/etc/php.ini && \
cp /usr/local/php-7.4.3/etc/php-fpm.d/www.conf.default /usr/local/php-7.4.3/etc/php-fpm.d/www.conf && \
sed  -i  "s#127.0.0.1:9000#0.0.0.0:9000#g" /usr/local/php-7.4.3/etc/php-fpm.d/www.conf && \
sed -in '99idaemonize = yes' /usr/local/php-7.4.3/etc/php-fpm.conf && \
cp /php-7.4.3/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && \
chmod a+x /etc/init.d/php-fpm
rm -rf /php-7.4.3.tar.gz
 /etc/init.d/php-fpm start
 
 
2. 配置php socket : vim /usr/local/php-7.4.3/etc/php-fpm.d/www.conf  大約36行加入:

listen = /dev/shm/php-fpm.sock
listen.mode = 0660

3. 保存退出后重啟php:
   /etc/init.d/php-fpm restart
   
   此時的php已經不再監聽網卡了,而是通過 /dev/shm/php-fpm.sock 與nginx進行交互.
   
   [root@k8s-master2 nginx-1.16.1]# ll /dev/shm/php-fpm.sock 
   srw-rw---- 1 root root 0 Apr 15 16:20 /dev/shm/php-fpm.sock

4. 讓nginx的php解析也向 socket配置

location ~ \.php$ {
    root           html;
    fastcgi_pass   unix:/dev/shm/php-fpm.sock;     #<<<--- 這里指定socket地址
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    include        fastcgi_params;
}

5. 配置測試頁面:
echo '<?php phpinfo(); ?>' > /usr/local/nginx/html/index.php

1.3.2. 安裝nginx

useradd  www -u 1200 -M -s /sbin/nologin
mkdir -p /var/log/nginx
yum install -y cmake pcre pcre-devel openssl openssl-devel gd-devel \
    zlib-devel gcc gcc-c++ net-tools iproute telnet wget curl &&\
    yum clean all && \
    rm -rf /var/cache/yum/*
mkdir -p /server/tools
cd /server/tools
wget https://www.chenleilei.net/soft/nginx-1.16.1.tar.gz
tar xf nginx-1.16.1.tar.gz
cd nginx-1.16.1
./configure --prefix=/usr/local/nginx --with-http_image_filter_module --user=www --group=www \
    --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module \
    --pid-path=/var/run/nginx/nginx.pid
make -j 4 && make install && \
    rm -rf /usr/local/nginx/html/*  && \
    echo "nginx daemo hello" >/usr/local/nginx/html/index.html  && \
echo "export PATH=$PATH:/usr/local/nginx/sbin" >>/etc/profile
chown -R www.www /var/log/nginx /usr/local/nginx
source /etc/profile
nginx -s reload
ps -ef|grep nginx

1.3.3 socket動靜分離配置

1. php-fpm.conf配置文件修改:
vim /usr/local/php-7.4.3/etc/php-fpm.d/www.conf 大約36行添加或修改為如下:
 listen = /dev/shm/php-fpm.sock
 listen.mode = 0666

2.編輯 nginx.conf 配置文件添加或 "修改為" 如下行:
 
 location ~ \.php$ {
     root           html;
     fastcgi_pass   unix:/dev/shm/php-fpm.sock;
     fastcgi_index  index.php;
     fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
     include        fastcgi_params;
 }
 
3. 重啟php-fpm和nginx
nginx -s reload
/etc/init.d/php-fpm restart

1.3.4 成功結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM