Apache Virtual Hosts on CentOS 7

Robert AndresenUncategorized 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
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
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
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;
ServerName <span class="highlight">www.example.com</span>
ServerAlias <span class="highlight">example.com</span>
DocumentRoot /var/www/<span class="highlight">example.com</span>/public_html
ErrorLog /var/www/<span class="highlight">example.com</span>/error.log
CustomLog /var/www/<span class="highlight">example.com</span>/requests.log combined
&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
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