1. 程式人生 > >nginx反向代理tomcat8和php7(四)

nginx反向代理tomcat8和php7(四)

pass children dev ces word fpm tomcat服務器 ash tab


nginx1.15反向代理:


正向代理:

用戶用瀏覽器與代理服務器建立連接。 代理服務器去找web服務器。

代理服務器是在客戶端瀏覽器設置。


反向代理:

用戶與反向代理服務器建立連接。反向代理服務器將請求代理到後端web服務器。

反向代理服務器是在服務器端配置代理。常用的反向代理為:nginx.


系統環境: centos7.5

安裝環境:

服務名稱 服務IP地址 服務描述
node01
18.18.23.109 nginx1.15
node0218.18.23.110

mysql5.7.22

node03
18.18.23.111 tomcat8/php7.1.2


一. nginx反向代理tomcat:

1.nginx反向代理tomcat配置:

nginx.conf配置:

    http段配置:
     include vhost/*.conf;


#mkdir vhosts

#vi vhosts/proxy-tomcat.conf

    server {
		listen 88;
		server_name tomcat;
		access_log logs/tomcat-access.log;
		error_log  logs/tomcat-error.log;	

		location / {
			proxy_pass http://18.18.23.111:8080;


tomcat服務器配置:

安裝jdk1.8

配置環境變量:

  export JAVA_HOME=/usr/local/jdk1.8.0_45
  export PATH=$JAVA_HOME/bin:$PATH
  export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

安裝tomcat服務器:

下載tomcat:

    #curl -o apache-tomcat-8.5.31.tar.gz  https://tomcat.apache.org/download-80.cgi#8.5.31/apache-tomcat-8.5.31.tar.gz


解壓tomcat.

啟動tomcat.

./bin/start.sh


瀏覽器訪問:

http://18.18.23.109:88 , 返回的是tomcat的頁面。

技術分享圖片


二、nginx1.15反向代理php

1.安裝php依賴的第三方庫:

# yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel  pcre-devel  gd-devel libcurl-devel curl-devel libxslt-devel openssl openssl-devel


2.編譯安裝php:

# tar -zxvf php-7.1.2.tar.gz
#cd  php-7.1.2
# ./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php/etc --with-mysqli --enable-mysqlnd --enable-embedded-mysqli  --with-curl  --with-freetype-dir  --with-gd  --with-gettext  --with-iconv-dir  --with-kerberos  --with-libdir=lib64  --with-libxml-dir  --with-mysqli  --with-openssl  --with-pcre-regex  --with-pdo-mysql    --with-pear  --with-png-dir  --with-xmlrpc  --with-xsl  --with-zlib  --enable-fpm  --enable-bcmath  --enable-libxml  --enable-inline-optimization  --enable-gd-native-ttf  --enable-mbregex  --enable-mbstring  --enable-opcache  --enable-pcntl  --enable-shmop  --enable-soap  --enable-sockets  --enable-sysvsem  --enable-xml  --enable-zip  --with-jpeg-dir --with-png-dir --with-iconv  --with-pcre-dir  --with-mhash  
#make &&  make install


3. 復制並修改配置文件:

在PHP服務器上,建立nginx用戶,要保證和nginx服務器上的nginx用戶id 號、組id號一致。

     # cp php.ini-development   /usr/local/php7/etc/php.ini


配置php-fpm

        # cp /usr/local/php7/etc/php-fpm.conf.default  /usr/local/php7/etc/php-fpm.conf
       # cp /usr/local/php7/etc/php-fpm.d/www.conf.default  /usr/local/php7/etc/php-fpm.d/www.conf


修改配置文件:

# vim /usr/local/php7/etc/php.ini

      short_open_tag = On
      default_charset = "UTF-8"
      date.timezone = Asia/Shanghai

#vim /usr/local/php7/etc/php-fpm.d/www.conf

        [www]
       user = nginx
       group = nginx 
       listen.owner = nginx
       listen.group = nginx
       listen = 18.18.23.111:9000
       pm = dynamic
       pm.max_children = 50       #子進程最大數量
       pm.start_servers = 20       #在啟動時創建的子進程數量
       pm.min_spare_servers = 5     # 空閑子進程的最小數量
       pm.max_spare_servers = 50     # 空閑子進程的最大數量
       pm.process_idle_timeout = 10s  # 一個空閑進程空閑多少秒後被殺死
       php_admin_value[error_log] = /var/log/fpm-php.www.log

4. 啟動php-fpm:

添加為系統服務:

# cat /usr/lib/systemd/system/php-fpm.service 
    [Unit]
    Description=php7.1.2
    After=network.target
    [Service]
    Type=forking
    ExecStart=/usr/local/php7/sbin/php-fpm
    ExecStop=/bin/pkill -9 php-fpm
    PrivateTmp=true
    [Install]
    WantedBy=multi-user.target


        #systemctl daemon-reload
    #systemctl enable php-fpm
    #systemctl start php-fpm


5.Nginx 配置php程序通過FastCGI轉發給php

# vim /usr/local/nginx/conf/nginx.conf

在server段上面配置:

  include vhost/*.conf;


# vim /usr/local/nginx/conf/vhost/proxyphp.conf

     server {        
         listen 81;
         server_name php;
         access_log logs/php-access.log;
         location / {
          root html/wordpress;
          index index.php index.html index.htm;   
         }
         location ~ \.php$ {
          root           html/wordpress;
          fastcgi_pass   18.18.23.111:9000;  #php服務器ip地址。
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
          include        fastcgi.conf;
         }
       }


php腳本存放位置:

# tar -zxvf wordpress-4.9.4-zh_CN.tar.gz   -C  /usr/local/nginx/html/
#chown -R nginx  /usr/local/nginx/wordpress



6.mysql配置:

mysql> create database wordpress;
mysql> create user 'blog'@'%' identified by '123';
mysql> grant all privileges on wordpress.* to 'blog'@'%';
mysql> flush privileges;


瀏覽器訪問:

http://18.18.23.109:81/index.php

技術分享圖片


技術分享圖片


技術分享圖片


wordpress已經搭建完成。



nginx反向代理tomcat8和php7(四)