Apache Virtual Hosts on CentOS 7

Robert Andresen Uncategorized Leave a Comment

This is a short guide for creating to virtual hosts on Apache with CentOS 7. For detailed description, see source at the bottom. Use sudo in front of the commands if your not root.

 

Create folders

mkdir -p /var/www/<span class="highlight">example.com</span>/public_html<br />
mkdir -p /var/www/<span class="highlight">example2.com</span>/public_html

Give users permission to the folders

chown -R $USER:$USER /var/www/<span class="highlight">example.com</span>/public_html<br />
chown -R $USER:$USER /var/www/<span class="highlight">example2.com</span>/public_html

Modify permissions to www-root

chmod -R 755 /var/www

Create a index.php file with some content on the web-folders we created.

Create virtual host files

mkdir /etc/httpd/sites-available<br />
mkdir /etc/httpd/sites-enabled

vi /etc/httpd/conf/httpd.conf

Add this line at the bottom:

IncludeOptional sites-enabled/*.conf

 

Add the virtual host config

Open config file

vi /etc/httpd/sites-available/<span class="highlight">example.com</span>.conf

Add this to the config-file

&lt;VirtualHost *:80&gt;<br />
ServerName <span class="highlight">www.example.com</span><br />
ServerAlias <span class="highlight">example.com</span><br />
DocumentRoot /var/www/<span class="highlight">example.com</span>/public_html<br />
ErrorLog /var/www/<span class="highlight">example.com</span>/error.log<br />
CustomLog /var/www/<span class="highlight">example.com</span>/requests.log combined<br />
&lt;/VirtualHost&gt;

Remember to create conf file for example2.com domain if you want to add two virtual hosts.

 

Create symbolic link in sites-enabled

Enable the virtual host by creating a symbolic link for each host in sites-enabled directory.

ln -s /etc/httpd/sites-available/<span class="highlight">example.com</span>.conf /etc/httpd/sites-enabled/<span class="highlight">example.com</span>.conf<br />
ln -s /etc/httpd/sites-available/<span class="highlight">example2.com</span>.conf /etc/httpd/sites-enabled/<span class="highlight">example2.com</span>.conf

 

Source: https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-centos-7