SETTING UP VIRTUAL HOSTS
Virtual Hosts are used when we have more than one domain to run with a single IP address. We can have multiple websites on our server and there is no limit to the number of virtual hosts that can be added.
For setting up virtual hosts on CentOS 7, follow the below steps. It is assumed that you have installed LAMP by following the Part One.
Create a new user and then create a directory for the website with the ownership as the new user.
# mkdir -p /var/www/site1/public_html # chown -R site1: /var/www/site1
Now, create a test page for later checking.
# vi /var/www/site1/public_html/index.html <html> <head> <title>site1.com</title> </head> <body> <h1>Success: You Have Set Up your first Virtual Host</h1> </body> </html>
For turning on the virtual host, enter the below contents to the Apache configuration.
# vi /etc/httpd/conf/httpd.conf <VirtualHost *:80> ServerAdmin master@site1.com DocumentRoot /var/www/site1/public_html ServerName www.site1.com ServerAlias site1.com ErrorLog /var/www/site1/error.log </VirtualHost>
These are the most important lines. The star (*) means any one of the server IP. You can enter your server dedicated IP instead. Document Root is the path where the website files are kept. Server name and Alias are the domain name we would be calling. Error log is to track errors.
Restart Apache and check if any error occurs from
/var/log/httpd/error_log. # service httpd restart
If you find any error that the document root or files inside have permission issues, do the following. This will manage the SELinux policies.
# semanage fcontext -a -t httpd_log_t "/var/www/site1(/.*)?" # restorecon -Rv /var/www/site1 restorecon reset /var/www/site1 context unconfined_u:object_r:httpd_sys_content_t:s0->unconfined_u:object_r:httpd_log_t:s0 restorecon reset /var/www/site1/public_html context unconfined_u:object_r:httpd_sys_content_t:s0->unconfined_u:object_r:httpd_log_t:s0 restorecon reset /var/www/site1/public_html/index.html context unconfined_u:object_r:httpd_sys_content_t:s0->unconfined_u:object_r:httpd_log_t:s0 restorecon reset /var/www/site1/error.log context unconfined_u:object_r:httpd_sys_content_t:s0->unconfined_u:object_r:httpd_log_t:s0 # ls -lZ /var/www/site1/ -rw-r-xr-x. site1 site1 unconfined_u:object_r:httpd_log_t:s0 error.log drwxr-xr-x. site1 site1 unconfined_u:object_r:httpd_log_t:s0 public_html
And finally reset the file permissions.
# cd /var/www/site1/ # find . -type d -exec chmod 755 {} \; # find . -type f -exec chmod 644 {} \;
Check the site by calling the domain in browser. If you are testing the site before changing the DNS records, you can do this by adding an host entry on your system.
<IP address> site1.com www.site1.com
In Linux, add the above entry in /etc/hosts after opening it with root privilege.
In Windows, open c:\Windows\System32\Drivers\etc\hosts in a notepad with administrator privilege. You can follow these steps to add multiple virtual hosts. That’s it, you will see the test page loaded in the browser.
We will be discussing other aspects of building a WordPress website with AWS in the next article.