Serie: Sharedhosting to VPS. Create date: 09-09-2017
In this blog I am going to configure my first website and print “Hello world” on a web page. As mentioned in my first blog, I have done this in the past but with Webmin. This time I don’t want to use a control panel and learn how I can do stuff like this by myself. It also feels like an extra security risk to have online tools like PHPMyAdmin and Webmin installed.
Installing Apache
My first goal is to get a “Hello world” message online. I’ve already created a DNS subdomain named “test” which points to my VPS. I’m not sure if Apache is installed by default, I think so but I’m not sure.
But how do I check if Apache is installed? I’ve tried a few things after Googling:
apt-cache policy apache
apt-cache policy apache2
dpkg -l | grep -E '^ii' | grep apache
dpkg -l | grep -E '^ii' | grep apache2
which apache
which apache2
But it seems like I have to install Apache myself:
sudo apt-get update
sudo apt-get install apache2
I’m curious if the search commands that I used earlier are now able to find Apache. Yes, all three commands say Apache is installed.
If I remember correctly I also need to make a hole in my firewall for incoming traffic but I’m going to check that later.
I also want to check if Apache is running, I used this command:
sudo systemctl status apache2
Now I’m going to test if I get an Apache landings page when I type the IP of my VPS in my browser. You can get this IP with this command:
hostname -I
Yes this works right away! I didn’t need to change my firewall. My test subdomain also works (of course, because this is the same IP).
I might need to restart Apache someday, use this command to restart Apache:
sudo systemctl restart apache2
After changing the Apache config you can use this command to keep the webserver running:
sudo systemctl reload apache2
The default Apache page is located in this directory:
/var/www/html
After viewing the Apache directory (/etc/apache2/) I found out that one of my favorite tools (tree) is not yet installed, so let’s fix this right away:
sudo apt-get install tree
Configuring a website
The next step is configuring a website. I’ll start with creating a folder for my website files:
sudo mkdir -p /var/www/testwebsite.nl/public_html
This directory is now a root user directory, I changed it to the logged in user with this command and also changed the permissions:
sudo chown -R $USER:$USER /var/www/testwebsite.nl/public_html
sudo chmod -R 755 /var/www
Next, I created a HTML file like this and put some text in it:
touch index.html
nano index.html
Now I’m going to create a Virtual Host. I copied the default Apache site:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/testwebsite.nl.conf
Edit the new file. I removed all the comments to make the file more readable. Change the ServerAdmin e-mail address and add the following lines:
ServerName testwebsite.nl
ServerAlias www.testwebsite.nl
Also change the path to the location of the website files:
DocumentRoot /var/www/testwebsite.nl/public_html
Now I need to activate my website, you can do this with a tool that is installed together with Apache:
sudo a2ensite testwebsite.nl.conf
And disable the default website and restart apache:
sudo a2dissite 000-default.conf
sudo systemctl restart apache2
Okay let’s check if it works!? Owww yeah, it works!
After this I created a folder in my website directory to check if you can show directory contents via the browser because this is something I definitely not want. Yes this is the default behavior, let’s change this.
Add the following text to testwebsite.nl.conf:
< Directory /var/www/testwebsite.nl>
allow from all
Options -Indexes
< /Directory>
Next a reload is sufficient:
sudo systemctl reload apache2
Yes this works too, if you try to access a directory without an index file you get a “Forbidden, You don't have permission to access this resource” message instead of a list with files.
Sources
How can I find out if a specific program is installed?
How To Install the Apache Web Server on Ubuntu 16.04
How To Set Up Apache Virtual Hosts on Ubuntu 16.04
Set up Apache virtual hosts on the Ubuntu operating system