Score:0

How can you run two instances of Nginx on the same machine without Docker?

in flag

I've been searching for how to install two instances of nginx on my server without Docker/containerization, but I've found little results. How can this be done?

Possible use cases:

  • You have two applications and you don't want one to take down both if one crashes. (This is my scenario.)
  • You want to give an end user control of the nginx config and process/service. Since the processes are isolated from each another, they can be stopped/started & configured independently.
cn flag
Edit the question to explain the actual problem you are trying to solve (why you need 2 instances of nginx running). There is probably a better solution (e.g. configuring virtual hosts or multiple `server` instances to listen on different interfaces/ports...
pkSML avatar
in flag
@hardillb Done.
Score:3
in flag

I've managed to solve this problem using Debian Bullseye, but the steps should be similar for other Linux flavors.

  • Find the nginx service file: find / -iname "nginx.service" -type f -ls

  • Copy the service file to /etc/systemd/system. Here is the command for my scenario: cp /usr/lib/systemd/system/nginx.service /etc/systemd/system/nginx2.service (the filename becomes the name of the service, so choose carefully)

  • Copy the nginx config to another folder: cp -rp /etc/nginx /etc/nginx2

  • Edit the nginx2 service file: nano /etc/systemd/system/nginx2.service. Change these lines:

     [Service]
     PIDFile=/run/nginx2.pid
     ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx2/nginx.conf -q -g 'daemon on; master_process on;'
     ExecStart=/usr/sbin/nginx -c /etc/nginx2/nginx.conf -g 'daemon on; master_process on;'
     ExecReload=/usr/sbin/nginx -c /etc/nginx2/nginx.conf -g 'daemon on; master_process on;' -s reload
     ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx2.pid
    
  • Edit the nginx config file: nano /etc/nginx2/nginx.conf. Change these lines:

     pid /run/nginx2.pid;
     include /etc/nginx2/modules-enabled/*.conf;
    
     http {
    
             include /etc/nginx2/mime.types;
    
             access_log /var/log/nginx/access2.log;
             error_log /var/log/nginx/error2.log;
    
             include /etc/nginx2/conf.d/*.conf;
             include /etc/nginx2/sites-enabled/*;
     }
    
  • Delete the symbolic link for the default site, as it points to the original nginx instance's folder: rm /etc/nginx2/sites-enabled/default. ( To re-create, ln -s /etc/nginx2/sites-available/default /etc/nginx2/sites-enabled/default.)

  • You cannot have two nginx instances listening on the same port & IP address (unless you're doing port sharding, which is an advanced topic...), so you must change the port(s) to not conflict with the first instance.

  • Inform systemd of the new service: systemctl daemon-reload

  • Enable the new service: systemctl start nginx2

  • Enjoy!

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.