Virtual Host

Posted on

Setup Directories

Host more than one domain by adding a directory for each website under /var/www/

sudo mkdir -p /var/www/
sudo mkdir -p /var/www/

assign ownership to environment $USER value

sudo chown -R $USER:$USER /var/www/
sudo chmod -R 755 /var/www

Create demo website

sudo nano /var/www/
    <title>Welcome to!</title> 
 <h1>Success! The virtual host is working!</h1> </body> 

Create sites-available and enabled directory

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

Add directory to Apache configuration

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

add to the end of file “IncludeOptional sites-enabled/*.conf”
save and close the file

Create a config file

sudo nano /etc/httpd/sites-available/
<VirtualHost *:80>
    DocumentRoot /var/www/ 
    ErrorLog /var/www/ 
    CustomLog /var/www/ combined

Save and close file

Enable conf files in apache

sudo ln -s /etc/httpd/sites-available/ /etc/httpd/sites-enabled/

Adjust SELinux Permissions

Directory Policy for Apache

to get current context type

sudo ls -dZ /var/www/

The current context httpd_sys_content_t only allows for read only.
Changing the context to httpd_log_t will grant apache to modify log files

sudo semanage fcontext -a -t httpd_log_t "/var/www/*)?"

Make the changes persistent

sudo restorecon -R -v /var/www/

list the context to see changes

sudo ls -dZ /var/www/

Restart the Apache server

sudo systemctl restart httpd

Test Virtual Host

Check if log files were created

ls -lZ /var/www/

Check on web browser http://IPaddress the website create above should render.