Installing Apache On Ubuntu 18.04 With Multiple Domains: Prerequisites
Installing Apache On Ubuntu 18.04 With Multiple Domains: Prerequisites
Installing Apache On Ubuntu 18.04 With Multiple Domains: Prerequisites
04 with Multiple
Domains
Last updated on September 28th, 2018
Apache is the most widely-used web server in the world with approximately 45 percent
of active sites running on it. In this guide we will install and con gure the Apache2
HTTP web server on Ubuntu 18.04 Server (Bionic Beaver). You can use this guide for a
single domain website or multiple domains using Virtual Hosts.
Prerequisites
You should use a non-root user account with sudo privileges. Please see the Initial server setup for
Ubuntu 18.04 guide for more details.
1. Install Apache
DevAnswers.co
Let’s begin by installing Apache from the Ubuntu repository. Press y and ENTER if prompted to install.
Installation may take a few minutes. Once installed, continue to Step 2 to con gure the rewall.
It is highly recommended that you con gure a rewall for added security.
We’ll start by adding a rewall rule for SSH because if you are con guring your server remotely, you don’t
want to get locked out when enabling the rewall! You may have already done this in the our Initial
server setup for Ubuntu 18.04 guide, but it’s no harm adding it again just in case. If the rule already
exists, the command will just skip it.
Press y if you see a message “Command may disrupt existing ssh connections”.
If the rewall was activated correctly, you should see “Firewall is active and enabled on system startup“.
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache Full (v6)
DevAnswers.co ALLOW Anywhere (v6)
Above we can see the rewall is active and has two rules per service. v6 is short for IPv6. This is the new
Internet Protocol, which was introduced to deal with the long-anticipated problem of IPv4 address
exhaustion.
3. Test Apache
To see if Apache installed correctly, we can check the current Apache service status.
If you get the above error about a fully quali ed domain name, you can ignore it.
Now that the Apache service is up and running, you should be able to view the test Apache web page
through your web browser. Enter the IP address of your server in the address bar and hit ENTER .
If you don’t know your IP, you can nd out with the following command.
$ ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.
DevAnswers.co
You’re all set! You can nd this Apache default welcome page in the folder /var/www/html . To edit this
le:
Your Apache web server is ready to go. You can now add your own html les and images the
the /var/www/html directory as you please. However, you should acquaint yourself with and set up at
least one Virtual Host for Apache in the next step as most of our Ubuntu 18.04 guides are written with
Virtual Hosts in mind.
Virtual Hosts allow you to host multiple web sites/domains on one server. Even you only ever intend on
hosting one website or one domain, it’s still a good idea to con gure at least one Virtual Host.
If you wish to instead go straight to setting up PHP on your new Apache web server, please see
guide Installing PHP for Apache on Ubuntu 18.04.
DevAnswers.co
4. Set Up Virtual Hosts
If you wish to host multiple sites/domains on Apache, you should now set up your directory structures
and Virtual Hosts. Even if you only want to host one site/domain, it’s a good idea to set up a directory and
Virtual Host now because if you ever need to add a new domain later, it will makes things a lot easier for
you.
For the purposes of this guide, we will make a virtual host for mytest1.com and another for
mytest2.com. You can substitute these with your own registered domains, or if you don’t have any
domains yet, you can still follow this guide and add mytest1.com and mytest2.com to your hosts le to
trick your OS into resolving these domains in the browser. We explain how to do this at the end of the
guide.
Let’s create two new directories in the /var/www/ directory for our two domains.
If we want our regular non-root user to be able to modify les in these directories, we must change the
ownership.
The $(whoami) variable will take the value of the user you are currently logged in as.
We must also change the permissions for the general web directory /var/www and its contents so that
pages can be served correctly.
We’ll now create a simple index.html web page for each domain using the echo command.
DevAnswers.co
Don’t forget to replace mytest1.com with your own domain if you have one.
$ sudo echo "Welcome to mytest1.com!" > /var/www/mytest1.com/public_html/index.html
The Virtual Host les located in /etc/apache2/sites-available/ are used to tell the Apache web
server how to respond to various domain requests.
In nano, paste in the block below. To paste into nano, press the right mouse button.
/etc/apache2/sites-available/mytest1.com.conf
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName mytest1.com
ServerAlias www.mytest1.com
DocumentRoot /var/www/mytest1.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Be sure to change all instances of mytest1.com to your own domain if you have one.
Save and close nano (Press CTRL + X and then press y and ENTER to save changes)
In nano, paste in the block below. To paste into nano, press the right mouse button.
/etc/apache2/sites-available/mytest2.com.conf
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName mytest2.com
DevAnswers.co
ServerAlias www.mytest2.com
DocumentRoot /var/www/mytest2.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Be sure to change all instances of mytest2.com to your own domain if you have one.
Save and close nano (Press CTRL + X and then press y and ENTER to save changes)
Now that we have our two virtual host les in place, we need to use the a2ensite tool to enable them.
Almost done! You must now reload the Apache service to activate the new con guration.
Assuming you have already con gured DNS on your domain registrar to point your domains to the IP of
your Apache server, you should now be able to view these test webpages in the web browser. If you
don’t have your own domains and just want to test, continue to Step 4.5 to edit your hosts le.
DevAnswers.co
4.5 Edit Hosts le (optional)
If you do not have any domains registered and instead just want to load mytest1.com and
mytest2.com as a test, you can edit the hosts le in your OS to point these domains to your server.
To edit hosts le in Linux or Mac, run sudo nano /etc/hosts . In Windows, follow this guide to edit
hosts. Once hosts les is open, enter two new lines
hosts
x.x.x.x mytest1.com
x.x.x.x mytest2.com
If you don’t know your web server’s IP, you can nd out with:
$ ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.
DevAnswers.co
Once you’ve saved you hosts le, you should be able to access mytest1.com and mytest2.com in your
browser.
Now that you have Apache up and running, there may be some common con guration changes that will
be useful to you.
You will nd that .htaccess will be ignored by default in Apache. If this is something you will need, we
can enable it by altering the Apache con guration le.
/etc/apache2/apache2.conf
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
AllowOverride None means that .htaccess will be ignored. Change it to AllowOverride All .
/etc/apache2/apache2.conf
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Save and exit (press CTRL + X , press Y and then press ENTER )
DevAnswers.co
Restart Apache.
If you want to later con gure some rules in .htaccess, you will most likely need to enable mod_rewrite.
Restart Apache.
If you con gured virtual hosts and visit your server via IP, you may still see the default Apache test page.
This page may generate unnecessary access and error logs due to bots and spiders. To disable:
/etc/apache2/sites-available/000-default.conf
DevAnswers.co
<Location />
Deny from all
Options None
ErrorDocument 403 Forbidden.
</Location>
Save and exit (press CTRL + X , press Y and then press ENTER )
Restart Apache.
Now when you visit your server via IP, you should see a forbidden message.
What Next?
Now that you have your Apache server tested and working, the next step is to install PHP and MySQL,
both required for most popular web applications. You might also want to con gure a free SSL cert.
Let me know in the comments if this helped. Follow me @DevAnswers or read more.
Feedback
6 replies
DevAnswers.co
Aleksandar Gocanin October 2, 2018
Thanks for your help, your solution works very well. : D
Reply
Great
Reply
I said that it worked a treat, and it does, for all of my sites except one. I have no idea why, I have checked
and checked everything, and it should be working, but this ONE site http://www.–.co.uk is not working,
http://www.–.com and http://www.–.com all work ne. Any ideas?
Reply
Hi Vincent. All these domains work ne for me. It could either be a caching issue with your browser
or DNS hasn’t propagated to your ISP yet. Try a di erent browser or proxy.
Reply
Reply
Search …
Recent Posts
Tags
Advanced Custom Fields (ACF) Apache cpanel Craft DigitalOcean FileZilla G-Suite Gmail IPv6
jQuery Kali Let's Encrypt mailgun MySQL namecheap nginx Outlook PHP PHP-FPM
phpMyAdmin PhpStorm Post x PuTTY SFTP SSH SSL Ubuntu Ubuntu 18.04 vsftpd