Step 1 — Installing Apache
Apache is available within Ubuntu’s default software repositories, so you can install it using conventional package management tools.
Update your local package index:
- sudo apt update
Install the apache2
package:
- sudo apt install apache2
Step 2 — Adjusting the Firewall
Check the available ufw
application profiles:
- sudo ufw app list
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH
Let’s enable the most restrictive profile that will still allow the traffic you’ve configured, permitting traffic on port 80
(normal, unencrypted web traffic):
- sudo ufw allow 'Apache'
Verify the change:
- sudo ufw status
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache (v6) ALLOW Anywhere (v6)
Step 3 — Checking your Web Server
Check with the systemd
init system to make sure the service is running by typing:
- sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Drop-In: /lib/systemd/system/apache2.service.d
└─apache2-systemd.conf
Active: active (running) since Tue 2018-04-24 20:14:39 UTC; 9min ago
Main PID: 2583 (apache2)
Tasks: 55 (limit: 1153)
CGroup: /system.slice/apache2.service
├─2583 /usr/sbin/apache2 -k start
├─2585 /usr/sbin/apache2 -k start
└─2586 /usr/sbin/apache2 -k start
Access the default Apache landing page to confirm that the software is running properly through your IP address:
http://your_server_ip
You should see the default Ubuntu 18.04 Apache web page.
Step 4 — Setting Up Virtual Hosts (Recommended)
When using the Apache web server, you can use virtual hosts (similar to server blocks in Nginx) to encapsulate configuration details and host more than one domain from a single server. We will set up a domain called your_domain, but you should replace this with your own domain name. To learn more about setting up a domain name with DigitalOcean, see our introduction to DigitalOcean DNS.
Create the directory for your_domain
:
sudo mkdir /var/www/your_domain
Assign ownership of the directory:
- sudo chown -R $USER:$USER /var/www/your_domain
The permissions of your web roots should be correct if you haven’t modified your unmask
value, but you can make sure by typing:
- sudo chmod -R 755 /var/www/your_domain
Create a sample index.html
page using nano
or your favorite editor:
- nano /var/www/your_domain/index.html
Inside, add the following sample HTML:
<html>
<head>
<title>Welcome to Your_domain!</title>
</head>
<body>
<h1>Success! The your_domain virtual host is working!</h1>
</body>
</html>
Save and close the file when you are finished.
Make a new virtual host file at /etc/apache2/sites-available/your_domain.conf
:
- sudo nano /etc/apache2/sites-available/your_domain.conf
Paste in the following configuration block, updated for our new directory and domain name:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName your_domain
ServerAlias your_domain
DocumentRoot /var/www/your_domain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and close the file when you are finished.
Enable the file with a2ensite
:
- sudo a2ensite your_domain.conf
Disable the default site defined in 000-default.conf
:
- sudo a2dissite 000-default.conf
Test for configuration errors:
- sudo apache2ctl configtest
You should see the following output:
Syntax OK
Restart Apache to implement your changes:
- sudo systemctl restart apache2
Apache should now be serving your domain name