httpd:virtual_host

Virtual Host

Here is an example for Apache to listen to any ip from port 80 for the name wiki.yourdomain.com. wiki.yourdomain.com could be something your type in to your web browser. The root directory of this virtual host is /var/www/YOURSITE.

<VirtualHost *:80>
   DocumentRoot "/var/www/YOURSITE"
   ServerName wiki.yourdomain.com
   <Directory "/var/www/YOURSITE">
      allow from all
      Options None
      Require all granted
   </Directory>
</VirtualHost>

Add this code into your apache.conf file, or for some OS, save this code under /etc/apache2/sites-enabled/any_name_you_like.conf, and then restart the apache server.

To test if this is working:

  1. Apache server should be able to restart without any problem. (If error, try systemctl status apache2.service in Ubuntu)
  2. Put an index.html file in the directory /var/www/YOURSITE. Make sure your Apache server has access right to it (sudo chown -R www-date:www-data /var/www/YOURSITE in Ubuntu 16.04)
  3. Use a web browser; type the url, and you should be able to access your index.html
  1. You can do mod rewrite in virtual host (redirect to https, for example)
  2. You can do virtual host on port 443, and set up the cert needed inside a virtual host.
  • httpd/virtual_host.txt
  • Last modified: 2019/12/20 11:11
  • by chongtin