Install multiple nginx on the same server (summary)

  Background : The test environment and the production environment are installed on the same server at the same time. Our project manager installed tomcat and nginx in different directories for the two projects to be more independent. When I deployed the project, there was no big problem in the backend. Due to the two nginxes in the frontend, there were some problems at runtime and it took me a long time to toss.

  Summary: It is no problem to install multiple nginx on the same server, but the problems that need attention are:

        1. Each Nginx instance needs to use a different port number and configuration file to avoid port conflicts and configuration file confusion. It can be achieved by specifying different installation directories and configuration file paths during installation;

        2. It is necessary to configure an independent system service for each Nginx instance in the system so that it can be started automatically when the system starts. This can be achieved by creating different systemd services. For example, you can create a service file called nginx1.service to start the first Nginx instance, and a service file called nginx2.service to start the second Nginx instance;

      3. Pay attention to the allocation and management of system resources. Running multiple Nginx instances on the same server may take up more system resources, including CPU, memory, and disk space. Therefore, when deploying multiple Nginx instances, it is necessary to ensure that the system resources are sufficient, and perform reasonable resource allocation and management to avoid problems such as excessive system load and performance degradation.

    Problems encountered:

1. (Main problem) Since multiple nginx are installed, when entering the startup command, the system does not know which one to execute, and the specified path needs to be entered

    eg. (1) Check different configuration file paths

/usr/local/nginx/conf/nginx.conf  # 第一个 Nginx 实例的配置文件
/usr/local/nginx2/conf/nginx.conf # 第二个 Nginx 实例的配置文件

         (2) Each configuration file should contain a unique port number, as follows:

# /usr/local/nginx/conf/nginx.conf
http {
    server {
        listen       80;
        server_name  localhost;
        ...
    }
}

# /usr/local/nginx2/conf/nginx.conf
http {
    server {
        listen       8080;
        server_name  localhost;
        ...
    }
}

       (3) When starting each Nginx instance at the end, use different port numbers and configuration file paths:

# 启动第一个 Nginx 实例
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

# 启动第二个 Nginx 实例
/usr/local/nginx2/sbin/nginx -c /usr/local/nginx2/conf/nginx.conf

If you want to run multiple Nginx instances at the same time, you need to add the above command to your startup script

2. When executing the command to reload the configuration file, there will be sudo: nginx: command not found error

# 重新加载 Nginx 配置文件(root用户)

  sudo nginx -s reload

# 报错
  
  sudo: nginx: command not found

# 原因:这可能是因为在root用户的PATH环境变量中没有包含nginx可执行文件的路径。可以尝试在命令前加上完

# 整的路径,例如:

  sudo /usr/local/nginx/sbin/nginx -s reload

# 或者将nginx可执行文件所在的路径添加到root用户的PATH环境变量中,例如:
  
  export PATH=$PATH:/usr/local/nginx/sbin

      Or before loading the configuration file, first check the configuration file for syntax errors, the command is as follows:

sudo nginx -t

3. Check the running nginx process

ps -ef | grep nginx

# 输出结果

nobody   13701 13700  0 10:13 ?        00:00:00 nginx: worker process

root     20973 13070  0 11:02 pts/1    00:00:00 grep --color=auto nginx

# 进程所有者(第一列)  进程ID(第二列) 父进程ID(第三列) CPU占用率(第四列) 

# 内存占用率(第五列)  进程状态(第六列) 进程启动时间(第七列) 进程名称(第八列)

### 最后一行是 grep 命令本身的进程信息,可以忽略

4. In the Nginx configuration file, search for all lines containing the listen keyword, you can use the following command:

sudo grep -R "listen" /etc/nginx/

Guess you like

Origin blog.csdn.net/qq_56489154/article/details/130523876