Nginx + Apache
This is a guide to set up Nginx and Apache web servers to run at the same time for developer environment.
💡 WARNING
This way your system will only listen to requests from your own machine. There will be no response to neither LAN nor internet requests.
Prerequisites
- Debian-based linux system
- Installed Nginx and Apache
Setup
hosts
Set hostnames mapping to the hosts file:
sudo nano /etc/hosts
Add these lines:
127.0.0.1 nginx
127.0.0.2 apache
Nginx
Update Nginx to listen to its mapped hostname:
sudo nano /etc/nginx/sites-enabled/default
Replace default lines:
server {
listen 80 default_server;
listen [::]:80 default_server;
listen nginx:80;
...
}
Restart Nginx service:
sudo service nginx restart
Apache
Update Apache to listen to its mapped hostname:
sudo nano /etc/apache2/ports.conf
Replace default lines:
Listen 80
Listen apache:80
Listen 443
Listen apache:443
Update default virtual host:
sudo nano /etc/apache2/sites-available/000-default.conf
Replace default lines:
<VirtualHost *:80>
<VirtualHost apache:80>
Restart Apache service:
sudo service apache2 restart
How to use
For new or existing configuration files just prepend needed hostname before port:
server {
listen nginx:80;
...
}
server {
listen nginx:443 ssl http2;
...
}
And for Apache:
<VirtualHost apache:80>
...
</VirtualHost>
<VirtualHost apache:443>
...
</VirtualHost>
When adding domain in /etc/hosts
file specify mapped local IP address:
127.0.0.1 example1.lan # nginx
127.0.0.2 example2.lan # apache
What it solves
Using this approach you have both services working at the same time with no additional ports required.
This way you can use needed web server for different projects to replicate production environments more precisely.
Suitable when Docker is not an option.
Understanding
This works by creating some sort of local proxy domains - aliases - for Nginx and Apache services.
Your project domain pointed to mapped local IP address will be handled by web server mapped to this IP address, as it listens to it by alias.