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 ~
wget http://wordpress.org/latest.tar.gz
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/mywebsite.com
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/mywebsite.com/
$ sudo chown -R demouser:www-data /var/www/mywebsite.com/*
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 mywebsite.com
to server_name
Note: http://111.111.111.111 = your server’s IP address
$ sudoedit /etc/nginx/sites-availble/apache
server {
listen 80;
server_name foobar.net www.foobar.net mywebsite.com www.mywebsite.com
location / {
proxy_pass http://111.111.111.111:8080;
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 mywebsite.com
configuration on Apache’s side, assuming Apache2 server listens on port 8080:
$ sudoedit /etc/apache2/sites-available/mywebsite.com.conf
--VirtualHost *:8080--
ServerAdmin admin@mywebsite.com
ServerName mywebsite.com
ServerAlias www.mywebsite.com
DocumentRoot /var/www/mywebsite.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
DirectoryIndex index.php index.html index.htm
--Directory /var/www/alexrusin.com/public_html--
Options FollowSymLinks
AllowOverride All
--/Directory--
--/VirtualHost--
Now let’s enable the website and restart Apache server.
$ sudo a2ensite mywebsite.com
$ 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 111.111.111.111. mywebsite.com
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 mail.myserver.com we need to create virtual alias for a separate domain mywebsite.com. To do so, add mywebsite.com to virtual_alias_domains
in /etc/postfix/main.cf
Then edit the file that virtual_alias_maps
points to. In my case it is hash:/etc/postfix/virtual/addresses
:
$ sudoedit /etc/postfix/main.cf
virtual_alias_domains = foobar.net mywebsite.com
virtual_alias_maps = hash:/etc/postfix/virtual/addresses
$ sudoedit /etc/postfix/virtual/addresses
info@foobar.net info_foobar
#catch-all address
@foobar.net info_foobar
admin@mywebsite.com 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 mail.myserver.com (priority 10)
Put the lowest TTL in order for changes to propagate faster through DNS system.
Conclusion
This is a typical workflow I go through when creating a website on my private cloud VPS.