Let’s learn how to configure a new site/URL on an Apache web server. As an example, I’ll configure a site via the URL http://demo.codewithsusan.com. I’m using a subdomain for the example but the same procedures apply for a primary domain.
I’ll be demonstrating this on a DigitalOcean server running Ubuntu and Apache.
In the instructions that follow, swap in demo.codewithsusan.com
with whatever site/URL you’re configuring.
Step 1) Configure domain
First, we need to update the settings for our domain or subdomain so that it points to the IP address of our server.
Log into your domain provider and find your DNS settings for the domain you’re working with. Within your settings create a new A Record that maps your domain or subdomain to your server’s IP address.
Here’s an example of what that looks like in my domain provider’s settings (Namecheap):
Step 2) Create the site folder
Next, let’s set up the site on the server by creating a new directory within /var/www/
that will hold the site’s files. We’ll name this directory after the site’s URL:
> sudo mkdir /var/www/demo.codewithsusan.com
Within this directory, create a simple index.html file to kick things off. You can do that using Nano, or here’s a one line command that will get the job done:
> echo "<h1>Hello from demo.codewithsusan.com</h1>" | sudo tee /var/www/demo.codewithsusan.com/index.html
Step 3) Configure the site
Next, we need to set up a URL for this site by creating a new config file in /etc/apache2/sites-available/
. This file should match the name of your URL with the addition of a .conf
extension.
You can create/edit this new file with Nano:
> sudo nano /etc/apache2/sites-available/demo.codewithsusan.com.conf
Within my new config file, I’ll use the following Apache site config. Replace ServerAdmin
, DocumentRoot
, and Directory
as appropriate for your site.
<VirtualHost *:80>
ServerAdmin mail@codewithsusan.com
DocumentRoot /var/www/demo.codewithsusan.com
<Directory /var/www/demo.codewithsusan.com/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_dir.c>
DirectoryIndex index.html index.php
</IfModule>
</VirtualHost>
To save your changes and exit Nano, Type ctrl + x
, then y
, then hit Enter.
For more details on the above configs check out the following reference: Apache Virtual Host documentation.
Step 4) Activate
In order to activate the above config/site, run the following command:
> sudo a2ensite demo.codewithsusan.com.conf
Additionally, run the following command to deactivate the default config file that came with your server:
> sudo a2dissite 000-default.conf
Next, run the following command to check your configs making sure there are no syntax errors. You can ignore the first warning about ServerName; the important part is the last line that says Syntax OK.
> apache2ctl -t
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK
Assuming everything checks out, restart Apache to make the changes take effect:
> systemctl restart apache2
Step 5) Test
Finally, test it out! Attempt to load your site URL in the browser and confirm you can see the contents of your index file: