Set Up Virtual Hosts for Apache On Centos 7 RHEL With Extra File And Folder Permissions

Many of us don't have computers powerful enough to run some of the Virtual Machine and Container solutions out there properly for developing applications, or prefer to use a traditional server on the host machine for this purpose.

Here we look at a solution for setting up extra VirtualHosts on a standard Linux (Centos 7 or RHEL) machine using Apache Web Server, which is expandable, ie, you can create extra Virtual Hosts as you require by repeating many of the same steps.

This is extremely useful in many scenarios and as you can create as many VirtualHosts as you want and you can set up a different one for every development environment you are using. Assuming you are not at high security on your machine, below is all you need to do, if not there may be extra steps to perform which are not covered here.

It is assumed that you have the standard Apache (Httpd) installed and running correctly at the usual localhost  (probably port 80) with default folders at /var/www/html for RHEL or Centos.

There are plenty of tutorials for setting that up online, if you do not, then do this first!

PHP is not necessary for a VirtualHost but I've added a  bit for users who want to check that is working also, if they have it.

We will be setting up a new Virtual Host and domain name with its own folders and permissions for easy access to them from your desktop without having to use sudo . 

You can also use this tutorial to change the permissions and access location for your existing /var/www/html folders even if you are not setting up a Virtual Host by following steps 2-8 after first creating the new group www-data  and adding the users apache and user, as in the preliminary task immediately below.
  
If you are setting up a new VirtualHost then use steps 1-14 after this.

Preliminary Task.

The first task is to set up a new group called www-data, so that both you and apache can access and change files without you having to use sudo all the time.

So create a new www-data group and add users and apache to it.

$ sudo groupadd www-data

Add the users to group

$ sudo usermod -a -G www-data anton (change anton for your user)
$ sudo usermod -a -G www-data apache

Check it with

$ groups apache anton (change anton for your user)



Should show both you and apache as users for the new group.

You only have to do the above once regardless of how many virtual hosts you are setting up and its probably a good idea to do it on just a standard apache install anyway.

Primary Tutorial 

(2-8 for standard apache access, change vhost1.com to html, or 1-14 for full VirtualHost setup read verbatim)

Now set up the directory for the new vhost server and files

1. $ sudo mkdir -p /var/www/vhost1.com

The permissions, we fix these so that both apache and user have access to change files, so change the group of this directory to the new www-data group and allow apache access to read & write files.

2. $ sudo chgrp -R www-data /var/www/vhost1.com
3. $ sudo find /var/www/vhost1.com -type d -exec chmod g+rx {} +
4. $ sudo find /var/www/vhost1.com -type f -exec chmod g+r {} +

Now change ownership to your user so you can also read & write files easily.

5. $ sudo chown -R anton /var/www/vhost1.com   (change anton for your user)
6. $ sudo find /var/www/vhost1.com -type d -exec chmod u+rwx {} +
7. $ sudo find /var/www/vhost1.com -type f -exec chmod u+rw {} +

Now create symlink to your desktop for easy access to add and alter files, you don't now need to use sudo.

8. $ ln -s /var/www/vhost1.com /home/anton/Desktop  (change anton for your user)

(Optional, repeat steps 2-8 on your existing standard apache folder at /var/www/html for parity, just change vhost1.com to /html, these will also add that folder to the desktop with the new group and user permissions) so you can have your standard folder set up plus the new VirtualHost folder as well, (and any you add later) all on your desktop)

To finish this part, add an index.html file into vhost1.com directory (now its on your desktop) with below contents.


<html>
<head>
    <title>Welcome to vhost1.com!</title>
  </head>
<body>
    <h3>Hello, your virtual host is set up correctly.</h3>
  </body>
</html>

Optional, you can also add a phpinfo page to check your php details, create a php file in vhost1.com directory called info.php with the usual php function in it.
<?php
phpinfo();
?>

You can check this when you've finished the set up, but first we set up the VirtualHost. 

We will need to set up the directory that  all the virtual hosts will be stored in, as well as the directory that tells Apache that a virtual host is ready to serve. The sites-available directory will keep all of our virtual host files, while the sites-enabled directory will hold symbolic links to virtual hosts that we want to publish. 

Make both directories with:

$ sudo mkdir /etc/httpd/sites-available
$ sudo mkdir /etc/httpd/sites-enabled

You dont need to set any permissions on these, just leave as is & you only need to do it once regardless of how many VitualHosts you are setting up.

Now tell Apache to look for virtual hosts in the sites-enabled directory. To do this, we will edit Apache's main configuration file and add a line declaring an optional directory for additional configuration files:

$ sudo nano /etc/httpd/conf/httpd.conf

Add this line to the end of the file:

IncludeOptional sites-enabled/*.conf

(Only once for the above)


Now set up the first VirtualHost file, it has to end in .conf

9. $ sudo nano /etc/httpd/sites-available/vhost1.com.conf

Into this file add the VirtualHost directive as below

<VirtualHost *:80>
    ServerName www.vhost1.com
    ServerAlias vhost1.com
</VirtualHost>

Now add the document root which is the directory you created earlier, and optional logs

 DocumentRoot /var/www/vhost1.com
 ErrorLog /var/www/vhost1.com/error.log
 CustomLog /var/www/vhost1.com/requests.log combined

So the final file looks like this.

10.
 <VirtualHost *:80>
    ServerName www.vhost1.com
    ServerAlias vhost1.com
    DocumentRoot /var/www/vhost1.com
    ErrorLog /var/www/vhost1.com/error.log
    CustomLog /var/www/vhost1.com/requests.log combined
</VirtualHost>

Now enable the file and site for apache to find and use,
11. $ sudo ln -s /etc/httpd/sites-available/vhost1.com.conf /etc/httpd/sites-enabled/vhost1.com.conf

This creates a symlink to the file in sites-available inside the sites-enabled folder you created earlier.

Now find your Ip address for your machine with

$ ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'

Add it to your /etc/hosts file along with the name of your new VirualHost server.
12. $ sudo nano /etc/hosts

Add the line as below
192.168.1.218 vhost1.com   (change for your ip and virtual host server name.

Restart apache
13. $ sudo service httpd restart

14. Browse to your new server/site using

http://vhost1.com



Optionally, check your php details by browsing to the info.php file if you created it.




You can repeat steps 1-14 for every extra VirtualHost you want to set up, just change the values ie, vhost1.com to vhost2.com and so on.


You don't have to create the index.html files or info.php files but it is recommended so you can check the site is working properly.


(Technically, this adds 2 extra steps to 1-14 above!)



So the above should work on most Centos 7 RHEL machines as is, and some other Linux distros depending on the apache installation path and folder locations which you may have to alter for those distros.



Hope it helps!























Labels: , , , , ,