Nginx alongside Apache on OSX
The other day I wanted to experiment with the superfast nginx server on osx for static files, and to use the built-in apache webserver as power unit to process the dynamic content.
Although not very hard to setup, there are some thing you should bear in mind:
- for each server (aka virtualhost) you will add to nginx, you will need to add a virtualhost to apache (at least if the server you were adding will have dynamic content)
- nginx has a good base documentation on it’s site, but I still think of it as an underdog server. That is, for every article you will find on the web for an nginx problem/issue, there will be 100 to find for apache.
Anyhow, let’s get on with it. Please not that I did not come up with all this myself, I found a lot of information scattered through different tutorials, which will be listed below. All credits to those authors!
Installation
brew install nginx
Installation done! On to the configuration
Configuration
Nginx
First, let’s set up nginx so it uses the somewhat cleaner “sites-enabled/sites-available” structure. Some might already seen it on apache2 or somewhere else, and I find it cleaner than putting all your virtual hosts in one (or several) file. This way, for each site you add you create the config file in the sites-available folder, then symlink it in the sites-enabled folder and just have nginx to load all the files in the sites-enabled folder. That way, whenever you temporarly want do disable a virtual host, you can just remove the symlink, and not have to remove the actual config file (you might want to keep it for future reference).
- Go to your nginx config folder (when installed with homebrew, that will typically be in
/usr/local/etc/nginx/ - There create two folders, sites-enable and sites-available.
- open up
nginx.confin your favorite text editor and update it to your likings. My file is listed below, you can play with the settings, but it is important you have the ‘include sites-enabled/’ line. That will make sure all your servers are loaded.
- go into the sites-available folder and create a server config file
- include the listing below (and change to your needs)
- now create a symlink to the file in the sites-enabled folder
- start nginx
sudo nginx
- or to restart:
sudo nginx -s reload
Apache
- open httpd.conf, should be in
/etc/apache2/httpd.conf - update the port apache listens on to 127.0.0.1:8080 (since nginx will be running on port 80, apache should be on another port), around line
- add your virtualhost (or include and external vhost file and add it there). Note that you could go for the same sites-enabled/sites-available setup here, like I did
- note that your virtualhost should have the same ServerName as the one you used in nginx, and it should listen on port 8080!
- restart apache
Hosts & Go!
Now just update your hosts file ( /etc/hosts ) to include your newly created server:
127.0.0.1 dropbox.local
And point your browser to http://dropbox.local. You should see the directory listing of all your folders, or if you planted a site there, it should run! If you do run into errors, first check out both the nginx and apache error logs in the Console, it might help you.
Credits
Big thanks to the following persons for their great articles and posts on forums: