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.
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:Edit the nginx config file:
nano /etc/nginx2/nginx.conf
. Change these lines: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!