Need to run multiple projects on the same server – ruby, python, php, nodeJS projects, all at once? It’s possible to have Nginx and Apache running side by side. The preferred way to do this is to have one server in front of another – basically you must choose which server will accept the initial requests and proxy to the correct application or to the second server if needed.
Since nginx is a little bit simpler to configure and more flexible (I find), we’ll put nginx in front of apache. To keep it simple, let’s say we wanted to have
- a django project (python) running at mydjangourl.com
- a wordpress (php) site at my-php-project-url.com
Both servers are configured to listen to port 80, so that’s not going to work. Since we’re putting nginx in front of apache though, nginx will have port 80.
Configuring nginx for your django app
- Go to /etc/nginx/conf.d
- Add (touch) a new file called django_project.conf
- Assuming your django app is already running on port 8000 (in a screen session, or preferably by a process supervisor), add something like this to the file:
upstream my_django_app { server 127.0.0.1:8000; } server { listen 80; server_name mydjangourl.com; #put the domain here location / { proxy_pass_header Server; proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_pass http://my_django_app; } } |
- An upstream server is defined as “my_django_app”
- All requests with the “host” as mydjangourl.com will be routed through this directive
- Proxy pass these requests to my_django_app, at 127.0.0.1:8000
Configuring nginx for the wordpress site
Since nginx is listening to port 80, we need to grab the requests for our php app and route them to apache on a different port. You should still be in /etc/nginx/conf.d
- Create a new file called php_project.conf (name doesn’t matter, except for the .conf extension)
- Add this to it (make sure to change the domain and note the port number, in this case 8050):
server { listen 80; server_name my-php-project-url.com; location / { proxy_pass http://127.0.0.1:8050; } proxy_set_header Host $host; } |
- This means that all requests looking for my-php-project-url.com will be routed to port 8050 on our localhost
- Make sure to reload nginx (sudo service nginx reload)
Configuring apache for the wordpress site
Now that we’re proxying requests from nginx to our port 8050, let’s make sure apache is listening on that one.
Firstly, go to /etc/apache2/ports.conf and add the following:
NameVirtualHost *:8050 Listen 8050 |
Then go to wherever you keep the virtualhost configuration (it should be in /etc/apache2/sites-available or /etc/apache2/conf.d) and change the VirtualHost, ServerName and DocumentRoot lines like this:
ServerName my-php-project-url.com DocumentRoot /var/www/mysite Options FollowSymLinks AllowOverride All Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all ... |
Don’t forget to enable your site (sudo a2ensite mysite.conf) and reload apache (sudo service apache2 reload)!