Detailed explanation of deploying Nginx+Apache dynamic and static separation


Preface

  • Nginx's static processing capabilities are very strong, but dynamic processing capabilities are insufficient, so dynamic and static separation technology is often used in enterprises
  • For PHP, hand over static pages to nginx for processing, and hand over dynamic pages to PHP-FPM module or Apache for processing
  • In the nginx configuration file, the different processing methods of static and dynamic pages are realized through the location configuration section and regular matching.
  • In the enterprise information application environment, the security and response speed of the server need to be configured according to the actual situation to achieve the best user experience
  • The default nginx installation parameters can only be used for the most basic services. You also need to adjust the corresponding parameters such as web cache time, connection timeout, web page compression, etc. The meal can play the best role of the server.

mark

1.1: What is Nginx dynamic and static separation?

  • Nginx has strong static processing capabilities, but its dynamic processing capabilities are insufficient. Therefore, dynamic and static separation technologies are commonly used in enterprises

  • Dynamic and static separation for PHP

    Hand over static pages to Nginx for processing

    Hand over dynamic pages to PHP-FPM module or Apache for processing

  • In the configuration of Nginx, the different processing methods of static and dynamic pages are realized through the occurrence configuration section and regular matching.

1.2: Principle of reverse proxy

  • Nginx can not only be used as a web server, but also has the functions of reverse proxy, load balancing and caching
  • Nginx uses the proxy module to proxy the client's request to the upstream server. At this time, the connection between ngInx and the upstream server is through the http protocol.
  • The most important instruction for Nginx to implement the reverse proxy function is proxy pass, which can and can dispatch user requests to the upstream server according to UR, client parameters or other processing logic

1.3: The most important thing for Nginx to achieve dynamic and static separation is configuration

1.3.1: Demand

  • According to needs, configure Nginx to achieve dynamic and static separation. Requests for php pages are forwarded to LAMP for processing, and static pages are handed over to Nginx for processing to achieve dynamic and static separation.

Two: Experimental steps

  • Assume and call the backend LAMP environment

  • Prepare two virtual machines, one as an Apache web service and the other as an Nginx web server

2.1: First install the Apache service

[root@tom03 ~]# sentenforce 0   #关闭核心防护
[root@tom03 ~]# iptables -F
[root@tom03 ~]# yum -y install httpd httpd-devel

2.2: Set firewall permissions and enable services

[root@tom03 ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@tom03 ~]# firewall-cmd --permanent --zone=public --add-service=https
success
#重新加载防火墙
[root@tom03 ~]# firewall-cmd --reload
success
#开启服务
[root@tom03 ~]# systemctl start httpd

2.3: Client test: enter the server IP for access

mark

Client tom03 IP: 20.0.0.42 as nginx architecture

Client 20.0.0.43 as lamp architecture

2.4: Install the database

  • Install mairadb

  • The mariadb database management system is a branch of MySQL, which is mainly maintained by the open source community. The purpose of using GPL license mariadb is to be fully compatible with MySQL, including API and command line, so that it can easily become a substitute for MySQL.

[root@localhost ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y
#重启服务
[root@tom03 ~]# systemctl start mariadb.service 
#查看端口
[root@tom03 ~]# netstat -ntap | grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      85026/mysqld        
#安全配置向导
[root@tom03 ~]# mysql_secure_installation
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n]    y
下面设置密码
确认新密码
#是否删除弥明用户
Remove anonymous users? [Y/n]     n
#是否拒绝root远程登陆
Disallow root login remotely? [Y/n] 
#删除测试数据库
Remove test database and access to it? [Y/n]   n

Reload privilege tables now? [Y/n] y
  • Install php
[root@tom03 ~]# yum -y install php
  • Install php and mysql connection package
[root@tom03 ~]# yum -y install php-mysql
  • Install environment pack
yum -y install php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-soap curl curl-devel php-bcmath
  • Configure php homepage information
[root@tom03 ~]# cd /var/www/html/
[root@tom03 html]# vim index.php
#填写
<?php
 phpinfo();
?>
#php要支持apache 要重启
[root@tom03 html]# systemctl restart httpd.service

Client input http://20.0.0.43/index.php

mark

2.5: Install nginx architecture

Switch to 20.0.0.44 host

[root@localhost ~]# cd /opt
[root@localhost opt]# rz -E
rz waiting to receive.
[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz 
  • Create program user
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
  • Installation Environment
[root@localhost nginx-1.12.2]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make
  • Compile and install
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module

#编译
[root@localhost nginx-1.12.2]# make 
[root@localhost nginx-1.12.2]# make install
  • Optimization path
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx  /usr/local/sbin/
  • Create startup script
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
  start)
   $PROG
   ;;
  stop)
   kill -s QUIT $(cat $PIDF)
   ;;
  restart)
   $0 stop
   $0 start
   ;;
  reload)
   kill -s HUP $(cat $PIDF)
   ;;
  *)
                echo "Usage: $0 {start|stop|restart|reload}"
                exit 1
esac
exit 0

[root@localhost nginx-1.12.2]# chmod -x /etc/init.d/nginx
[root@localhost nginx-1.12.2]# chkconfig --add nginx
#重启服务
[root@localhost init.d]# service nginx restart
  • Install web verification
[root@localhost init.d]# yum install elinks -y
#重启服务
[root@localhost init.d]# service nginx start
[root@localhost init.d]# systemctl stop firewalld.service 
[root@localhost init.d]# setenforce 0
[root@localhost init.d]# elinks http://20.0.0.42/

mark
Client access test
mark
mark

Three: configure dynamic and static separation

3.1: Write Nginx configuration file

[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.conf

mark

3.2: Restart the Nginx service

[root@localhost init.d]# service nginx stop
[root@localhost init.d]# service nginx start
[root@localhost init.d]:

3.3: Client test input http://20.0.0.42/index.php

mark

Thank you for watching this experiment

Guess you like

Origin blog.csdn.net/weixin_47151643/article/details/108053196