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/hostsAdd these lines:
127.0.0.1 nginx
127.0.0.2 apacheNginx
Update Nginx to listen to its mapped hostname:
sudo nano /etc/nginx/sites-enabled/defaultReplace default lines:
server {
listen 80 default_server;
listen [::]:80 default_server;
listen nginx:80;
...
}Restart Nginx service:
sudo service nginx restartApache
Update Apache to listen to its mapped hostname:
sudo nano /etc/apache2/ports.confReplace default lines:
Listen 80
Listen apache:80
Listen 443
Listen apache:443Update default virtual host:
sudo nano /etc/apache2/sites-available/000-default.confReplace default lines:
<VirtualHost *:80>
<VirtualHost apache:80>Restart Apache service:
sudo service apache2 restartHow 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 # apacheWhat 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.