Virtual web server

I am not sure what it is actually called, virtual web server or virtual host, whatever. This configuration is to allow multiple web applications to be hosted in single machine.

The default configuration file of httpd service is at /etc/httpd/conf/httpd.conf. Notable default settings are :

ServerRoot "/etc/httpd"
Listen 80
DocumentRoot "/var/www/html"

ServerRoot : where the server configuration/log files are kept.
Listen : listening to which port
DocumentRoot : where the incoming requests will refer to

<ifmodule dir_module>
  DirectoryIndex index.html
</ifmodule>

The above section sets the default file that Apache will serve if a directory is requested.


Another "default" DocumentRoot location to put your hosted files are at /srv/<folder>/www. We will use this location to host our virtual host.

Now, let's proceed to create the configuration file in /etc/httpd/conf.d/


Now, let's create the index.html file. Take note on the file properties.


We can proceed to restart httpd service.

systemctl restart httpd

However, when you try to access, you'll get this page instead.


Notice the error message in error log.


This is due to I set the SELinux (Security-Enhanced Linux) to enforcing. Since this is a new path, thus it doesn't have the correct permission setup. What I need to do is, a restorecon command on the directory.


Now the web page is displayed.


Creating new file on the same directory will automatically has the file context setup properly after the restorecon command run.


You can create as many virtual host as your machine support by repeating the above steps.

Additional note : You may put the DocumentRoot anywhere besides the "default" locations. However, you'll need to set the fcontext of the folder and files to have httpd_sys_content_t using semanage. I'll talk about SELinux in another post.

One important thing that I didn't show here is, I updated my hosts file, so that the URL point to my web server. :D

Just add this line in your hosts file will do.

<ip> <host name>

For windows, it's located at C:\Windows\System32\drivers\etc.
For linux, it's located at /etc/

Also, I did not mentioned in earlier, to allow external to access to the web server, the firewall need to configure to allow http access.

firewall-cmd --permanent --add-service=http
firewall-cmd --reload

Comments

Popular Posts