By now we have a working private server with very basic features and a demo user. By default your website is data is accessed from the following directory:
So if you open up your server address in your browser the files in this directory are parsed. This is not bad, however if you are planning to use 2 or more domains on the same server, then you need to tell the apache which directories should it use for each domain. This is what the virtual hosts files are used for. In this case please check out the digital oceans official virtual hosts tutorial.
Creating the new default directory
Even if you are not planning to use another domain with your droplet I still recommend changing the directory to the following structure:
It's much nicer and you are probably used to see the public_html folder at your previous host a lot.
Create the new folders
It's time to log in to your terminal. By running the following command you can create the new public_html directory. Change the example.com to your domain name of course.
sudo mkdir -p /var/www/example.com/public_html
Now we have the directory, but we still need to tell the apache server that we want to use this instead of var/www
After creating the folder we need to make it accessible to the current user as well as the www-data process. If the ownerships are incorrect, your application won't be able to access the file system correctly. Run this command on the newly created directory:
sudo chown -R demo:www-data /var/www/example.com/public_html
The file system permisions needs to be set accordingly:
sudo chmod g+w /var/www/example.com/public_html -R
You might need to do these operations again, once the files are copied to the server.
Changing the Virtual Hosts files
Now that the directories are created the virtual hosts file is ready to be changed. There is a default hosts file in use, which you can find in the following location:
Instead of changing this file, let's make a copy of it's contents to a new file (don't forget to change the name):
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
Let's make changes to that file. Open it up in the nano (or vi, or any) editor:
sudo nano /etc/apache2/sites-available/example.com.conf
You can disregard everything in that file if you want. Put this into that file:
Hit CTRL + X to exit, it will ask you if you want to save the changes. Of course don't forget to change the domain name and the admin contact information.
You may have noticed that there is a directory tag. That will ensure that the symbolic links will work correctly. (.htaccess files)
After finishing we need to tell the apache server to use this virtual hosts configuration file instead of the default one.
The following commands will do the job:
sudo a2ensite example.com.conf sudo a2dissite 000-default.conf
After everything is done we still need to restart the webserver:
sudo service apache2 restart
Creating an index file
This step is optional, but it will make sure that your configuration is right. Since there is nothing in your public_html directory you should see an error message. Let's quickly make an index file to make sure, that our configuration is indeed right:
Copy&paste this html code there:
<html> <head> <title>Welcome to Example.com!</title> </head> <body> <h1>Success! The example.com virtual host is working!</h1> </body> </html>
Save the file (CTRL + X then y). If you know open your server in your browser, you should be seeing the success message.
- Chapter #1 – Preparation & Backup
- Chapter #2 – Droplet Setup
- Chapter #3 – SSH and Users
- Chapter #4 – Virtual Hosts
- Chapter #5 – phpMyAdmin
- Chapter #6 – FTP & SFTP
- Chapter #7 – Mailing
- Chapter #8 – Swap file
- Chapter #9 – Saving space automatically
Latest posts by Ernest Marcinko (see all)
- Ajax Search Pro for WordPress 4.11.10 Developer Notes - January 13, 2018
- Saving space automatically – Beginners guide to Digital Ocean - May 6, 2016
- From XAMPP to Vagrant WordPress - January 5, 2016