In my last post I covered the basic installation and deployment of LAMP. Once that has been done the server will be ready to configure virtual hosts. This will allow the Linux server in question to run several websites simultaneously. There are numerous ways to achieve this but I prefer name based virtual hosts. Here is my experience, troubles and outline for configuring virtual hosts on a Debian server.
Enable Mod Rewrite
After deploying or rebuilding several webservers, I find this is the step I miss the most. I always end up breaking my brain when trying to change permalinks structure later and it fails. So, I tend to do this immediately after installing apache2.
- sudo a2enmod rewrite
This command turns the module on or informs you if it is already on. If it was not already activated then restart apache2.
- sudo service apache2 restart
Alright, awesome, the part the normally kills me is taken care of!
Configure virtual host
The virtual host configuration process starts by disabling apache’s default virtual host.
- sudo a2dissite *default
Next, create the folder for your domain. For ease of understanding “website.com” will be used as a place holder for your domain name! Here we will navigate to the proper directory, create folders and subfolders.
- cd /var/www/html
- sudo mkdir website.com
- sudo mkdir -p website.com/public_html
- sudo mkdir -p website.com/log
- sudo mkdir -p website.com/backups
The folder structure depends on what you prefer. I have seen some people not create the /public_html folder and just use the root of /var/www/html/website.com for their sites home directory. But in this example and more commonly /public_html is used. Now that the folders are created its time for the more technical part. Create the virtual host file your website will use! Again, replace website.com with your domain name.
- sudo nano /etc/apache2/sites-available/website.com.conf
Nano is a built in text editor. This command creates the file and opens the text editor. Input these basic settings I learned from and replace website.com with your domain name. There are a multitude of other settings to control how the website resolves. These are plain settings that are typically enough for a basic setup.
# domain: website.com
# public: /var/www/html/website.com/public_html/
# Admin email, Server Name (domain name), and any aliases
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
# Log file locations
CustomLog /var/www/html/website.com/log/access.log combined
Press CTRL+X then Y to save this information. The Virtual host configuration is complete. Now it needs to be enabled! Replace website.com with your domain name.
- sudo a2ensite website.com.conf
An Apache restart is common after this and the last command will normally notify you to perform one for the changes to take affect.
- sudo service apache2 restart
If this fails it is typically caused by mispelling the .conf file or domain in the .conf file. Apache won’t see the right folder path and give you a “failed” to restart in red. If it doesn’t fail, then woohoo! Virtual hosts are configured. All that’s left is to upload your sites files via FTP! I usually use filezilla to connect to the server and navigate to /var/www/html/public_html and upload an index.html or index.php file! However, you will have needed to point your domain to your hosting service if you did not purchase it from the same place.
If you are like me you purchased your domain name from a 3rd party registrar like GoDaddy, 1&1, NameCheap, Google Domains, etc. In order to get your domain name pointed to the hosted webserver you have to edit the DNS settings. The Registrar will have an options for this. For instance in GoDaddy it looks like this:
Just navigate to the DNS or Nameservers settings and point them to your webhost. Your webhost will have documentation of their nameservers. This picture obviously is Linode’s nameservers. The final step is to go into the dashboard for your webhost, in this case Linode, and Add the domain zone! This process varies based on the host but is usually as simple as clicking add domain zone and typing in the domain you just pointed there.
Alright, the monster is completed. My, your, everyone’s first site is up! Just a heads up you can repeat the steps after disabling the default Apache virtual host to create as many websites as your server can host!