Home Operation and Maintenance Nginx How to deploy Nginx+Apache and separate dynamic and static

How to deploy Nginx+Apache and separate dynamic and static

May 13, 2023 am 10:49 AM
apache nginx

nginx动静分离介绍

nginx的静态处理能力很强,但是动态处理能力不足,因此,在企业中常用动静分离技术
针对php的动静分离

  • 静态页面交给nginx处理

  • 动态页面交给php-fpm模块或apache处理

在nginx的配置中,是通过location配置段配合正则匹配实现静态与动态页面的不同处理方式

反向代理原理

nginx不仅能作为web服务器,还具有反向代理、负载均衡和缓存的功能

nginx通过proxy模块实现将客户端的请求代理至上游服务器,此时nginx与上游服务器的连接是通过http协议进行的

nginx在实现反向代理功能时的最重要指令为proxy_ pass,它能够并能够根据uri、客户端参数或其它的处理逻辑将用户请求调度至上游服务器

配置nginx实现动静分离

本案例根据企业需要,将配置nginx实现动静分离,对php页面的请求转发给lamp处理,而静态页面交给nginx处理,以实现动静分离

架构如图所示

How to deploy Nginx+Apache and separate dynamic and static

配置步骤

1、架设并调试后端lamp环境

①安装apache服务

[root@localhost ~]# yum install httpd httpd-devel -y

②在防火墙设置http服务的权限

1

2

3

4

5

6

7

[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=http

success

[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=https

success  

[root@localhost ~]# firewall-cmd --reload

success

[root@localhost ~]# systemctl start httpd

Copy after login

③安装mariadb

mariadb数据库管理系统是mysql的一个分支,主要由开源社区在维护,采用gpl授权许可 mariadb的目的是完全兼容mysql,包括api和命令行,使之能轻松成为mysql的代替品

1

2

[root@localhost ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y

[root@localhost ~]# systemctl start mariadb.service

Copy after login

④mysql安全配置向导

1

[root@localhost ~]# mysql_secure_installation

Copy after login

⑤安装php及支持的软件

1

2

3

[root@localhost ~]# yum install php -y

[root@localhost ~]# yum install php-mysql -y

[root@localhost ~]# yum install php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel php-bcmath -y

Copy after login

⑥更改网页主页面

1

2

3

4

5

6

7

[root@localhost ~]# cd /var/www/html

[root@localhost html]# vim index.php

<?php

  echo "this is apache test web";

?>

 

[root@localhost html]# systemctl restart httpd

Copy after login

⑦访问测试,输入网址

How to deploy Nginx+Apache and separate dynamic and static

2、编译安装nginx

①安装支持软件

1

[root@localhost ~]# yum install gcc gcc-c++ pcre-devel zlib-devel -y

Copy after login

②创建运行用户和组

1

[root@localhost ~]# useradd -m -s /sbin/nologin nginx

Copy after login

③编译安装

1

2

3

4

5

6

7

8

9

10

[root@localhost lnmp-c7]# tar zxvf nginx-1.12.2.tar.gz -c /opt

[root@localhost lnmp-c7]# cd /opt/nginx-1.12.2/

[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 && make install

[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin

Copy after login

④服务管理控制

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

[root@localhost ~]# vim /etc/init.d/nginx

#!/bin/bash

# chkconfig: - 99 20

# description: ngins 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 ~]# chmod +x /etc/init.d/nginx

[root@localhost ~]# chkconfig --add nginx

[root@localhost ~]# service nginx start

Copy after login

⑤启动服务

1

2

3

[root@nginx ~]# systemctl stop firewalld.service

[root@nginx ~]# setenforce 0

[root@nginx ~]# service nginx start

Copy after login

⑥配置nginx处理动态页面请求

1

2

3

4

5

6

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf

    location ~ \.php$ {

      proxy_pass  http://192.168.150.214;

    }

 

[root@nginx ~]# service nginx restart

Copy after login

⑦访问测试

How to deploy Nginx+Apache and separate dynamic and static
How to deploy Nginx+Apache and separate dynamic and static

The above is the detailed content of How to deploy Nginx+Apache and separate dynamic and static. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to build a Zookeeper cluster in CentOS How to build a Zookeeper cluster in CentOS Apr 14, 2025 pm 02:09 PM

Deploying a ZooKeeper cluster on a CentOS system requires the following steps: The environment is ready to install the Java runtime environment: Use the following command to install the Java 8 development kit: sudoyumininstalljava-1.8.0-openjdk-devel Download ZooKeeper: Download the version for CentOS (such as ZooKeeper3.8.x) from the official ApacheZooKeeper website. Use the wget command to download and replace zookeeper-3.8.x with the actual version number: wgethttps://downloads.apache.or

How to solve CentOS system failure How to solve CentOS system failure Apr 14, 2025 pm 01:57 PM

There are many ways to solve CentOS system failures. Here are some common steps and techniques: 1. Check the log file /var/log/messages: system log, which contains various system events. /var/log/secure: Security-related logs, such as SSH login attempts. /var/log/httpd/error_log: If you use the Apache server, there will be an error message here. 2. Use the diagnostic tool dmesg: display the contents of the kernel ring buffer, which helps understand hardware and driver questions

See all articles