Installing a Website on Apache Virtual Host with Nginx as a Reverse Proxy

In this post I would like to share a workflow I use to install a website on my Amazon Web Services cloud server.  My server configuration consists of Nginx – a server for my Node.js applications and a reverse proxy for Apache, Apache that I use for hosting websites, PHP5-FPM, MySQL database, and Postfix email server.

Step 1 – Create MySQL database

Since I am going to install a WordPress website we create a user and assign a database using PHPMyadmin.  This process is straightforward.

Step 2 – Download, install, and configure WordPress

Download and extract WordPress in your home directory:

cd ~
tar xzvf latest.tar.gz

This will create wordpress directory. Let’s modify the main configuration file in this directory. We create a copy of wp-config.php from wp-config-sample.php and edit it using command sudoedit.

cd ~/wordpress
cp wp-config-sample.php wp-config.php
sudoedit wp-config.php

The parameters we need to set are DB_NAME, DB_USER, and DB_PASSWORD. Save changes and close the file.

Step 3 – Copy files to the document root

Let’s make directory /var/www/ and copy the files to this location. We will use the rsync utility to do the transfer. This has the advantage of preserving permissions, ownership, and ensuring data integrity. The second line of code recursively copies contents of ~/wordpress directory into our website directory. The last line changes permissions in order for our server to be able to modify certain files and directories.

$ sudo mkdir -p /var/www/html
$ sudo rsync -avP ~/wordpress/ /var/www/
$ sudo chown -R demouser:www-data /var/www/*

Step 4 – Modify Nginx and Apache server blocks

First, we modify /etc/nginx/sites-available/apache file that contains redirects to the Apache server. We just simply add to server_name Note: = your server’s IP address

$ sudoedit /etc/nginx/sites-availble/apache

server {
    listen 80;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

Don’t forget to reload Nginx $ sudo service nginx reload

Second, we create and enable configuration on Apache’s side, assuming Apache2 server listens on port 8080:

$ sudoedit /etc/apache2/sites-available/

--VirtualHost *:8080--
    DocumentRoot /var/www/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    DirectoryIndex index.php index.html index.htm
   --Directory /var/www/
        Options FollowSymLinks
        AllowOverride All

Now let’s enable the website and restart Apache server.

$ sudo a2ensite
$ sudo service apache2 reload

Step 5 – Point your domain to the host IP
You can purchase domain name from any hosting provider like Godaddy, for example. After that, create an A record in DNS settings that points to your host’s IP address and put TTL (time to live) to the lowest available in order for the record to propagate faster.
If you are not ready to purchase your domain yet, you can test your website by creating a record in your hosts file

Step 6 – Create an email (if Postfix is installed)
I have Postfix installed on my server, so I usually create an email address with the same domain. Assuming the mail server is we need to create virtual alias for a separate domain To do so, add to virtual_alias_domains in /etc/postfix/ Then edit the file that virtual_alias_maps points to. In my case it is hash:/etc/postfix/virtual/addresses:

$ sudoedit /etc/postfix/

virtual_alias_domains =
virtual_alias_maps = hash:/etc/postfix/virtual/addresses

$ sudoedit /etc/postfix/virtual/addresses       info_foobar
#catch-all address           info_foobar   admin_mywebsite

After all done execute commands $ sudo postmap /etc/postfix/virtual/addresses and $ sudo postfix reload
Now we just need to create a user admin_mywebsite using the following command $ sudo useradd -m admin_mywebsite -s /sbin/nologin and create a password for this user $ sudo passwd admin_mywebsite. Now we can use Squirrelmail (if it is installed on the server) to login to mailbox.
In order to receive mail, we need to create an MX record in DNS settings.
MX record
@ points to (priority 10)
Put the lowest TTL in order for changes to propagate faster through DNS system.

This is a typical workflow I go through when creating a website on my private cloud VPS.

Share this article