Article overview

Help article

Installing an Apache webserver in Ubuntu 16.04

In this article, we explain the installation and initial configuration of an Apache web server (with httpd-tools, mod_ssl, and PHP-FPM) in Ubuntu 16.04.

Together with MariaDB and PHP, Apache forms the so-called LAMP stack (Linux Apache Mariadb PHP). If you host a dynamic website (e.g. WordPress) on your VPS, you also need MariaDB (or a comparable alternative depending on your use case).

In this article, we cover the following topics:

  • Installing Apache
  • Allowing Apache in your firewall
  • Installing PHP
  • Using vhosts

In addition, we also recommend setting up an SFTP server and securing the websites that you host with SSL.

  • Execute the steps in this article as a root user or use sudo.
     
  • Would you like to immediately test your Apache server? If so, point the DNS of one of your domains to your VPS (through the A- and AAAA-record).

Installing Apache

 

Step 1

Connect to your VPS via SSH or use the VPS console and update your server first:

apt -y update
apt -y upgrade

First restart your VPS and repeat the commands above, unless your VPS already was up to date. Some packages aren't available until after updating and restarting your server.


 

Step 2

Install Apache with the command:

apt -y install apache2

Apache starts automatically after the installation and with reboots of your server. You don't have to enable and start the service manually.


Allowing Apache in your firewall

 

By default, Ubuntu 16.04 comes with UFW as a firewall and, out-of-the-box, all ports are closed in it. You open the http-(80) and https-(443) ports with the commands:

ufw allow 80/tcp
ufw allow 443/tcp

 

Do you use the VPS firewall in your control panel? If so, don't forget to also open the HTTP (80) and HTTPS (443) ports in your control panel.


 

Testing

You can now test the functioning of your web server directly by going to http://example.com in a browser, replacing example.com with your hostname (checkable with the 'hostname' command), or the IP address of your VPS. You will then see the default test page.


Installing PHP

 

Step 1

Apache comes without a recent PHP version after installation. If you want to host a dynamic PHP website (e.g. WordPress), we'd recommend installing the most recent version of PHP. First add the PHP 7.3 PPA repository with:

add-apt-repository ppa:ondrej/php

Press 'Enter' when prompted for confirmation.


 

Step 2

Update your VPS again so the new repository can be used and then install PHP 7.3 with the most relevant modules:

apt -y update
apt -y install php7.3 php7.3-cli php7.3-common php7.3-curl php7.3-mbstring php7.3-mysql php7.3-xml php7.3-fpm

Your PHP version is now the latest iteration of 7.3. The precise version can be checked with:

php -v

We aim at keeping our documentation up to date, but if you're reading this at a time when a newer version of PHP is available, replace the version number by the newer version.


 

Step 3 

This is a good moment to make changes to php.ini if you'd like, such as changing the memory_limit. The php.ini configuration can be found in:

nano /etc/php/7.3/fpm/php.ini

 

Step 4

Finally, enable the php modules and restart Apache:

a2enmod actions fastcgi alias proxy_fcgi
a2enconf php7.3-fpm
systemctl restart httpd

Your Apache server is now completely ready for PHP. Do not forget to install MariaDB if there is also a database linked to your website and to configure your VHost (see below) if you are hosting multiple sites.


Using a VHost

 

Do you want to host more than one domain? Then, you use a VHost system (also known as VirtualHost). A VHost file contains, among other things, the data on which specific VPS domains are hosted. As a result, visitors to domains that you host on your VPS are automatically sent to the correct folders on your VPS where the websites are located (without them noticing it).

 

Step 1

Create the vhost file with the command:

nano /etc/apache2/sites-available/example.com.conf

 

Step 2

Copy the content below, replacing example.com with the name of the domain you want to host on your VPS. 

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html

    <FilesMatch \.php$>
       SetHandler "proxy:unix:/var/run/php/php7.3-fpm.sock|fcgi://localhost/"
    </FilesMatch>
    ErrorLog /var/log/apache2/example.com/error.log
    CustomLog /var/log/apache2/example.com/access.log combined
</VirtualHost>
Explanation
  • <VirtualHost *:80> </VirtualHost>: Opens and closes the guidelines for a specifc virtual host. Each option (e.g. ServerAdmin) is a guideline / directive for the virtual host.
  • ServerAdmin: Sets the contact address which the server uses for reporting errors.
  • ServerName: This is actually meant to provide the server's hostname. Within a server environment that uses Vhosts to host multiple domains, it's used to add specific domains and to allow your server to identify itself as those specific domains.
  • ServerAlias: Alternative names for the host which is set under 'ServerName'. You can also use a wildcard here using the syntax *.example.com.
  • DocumentRoot: The location on your VPS where the website (ie iets corresponding files) can be found. <FilesMatch \.php$> </FilesMatch>: Defines the SetHandler and enables PHP-FPM. PHP-FPM provides a better performance than FastCGI, especially when hosting larger websites.
  • ErrorLog: The file in which errors are logged.
  • CustomLog: The file used to log requests for your server (e.g. connection requests). The option combined adds additional headers to the log reports to provide more detail. 

 

Step 3

The directories listed in the previous step do not yet exist and you must first create them together with the log files. Replace example.com with the name of the domain you are adding:

mkdir -p /var/www/html/example.com
mkdir -p /var/log/httpd/example.com
touch /var/log/httpd/example.com/error.log
touch /var/log/httpd/example.com/access.log

The -p (-parent) suffix creates all directory structures that stand for /example.com if necessary. In principle, all of these should already exist.


 

Step 4

a2ensite example.com.conf
systemctl reload apache2

Extra domains

Repeat step 1 -4 for each domain you'd like to add.


 

Step 5

You can now immediately get started with your website, by uploading it via SFTP or FTPS, or by creating an html website with a text editor from command-line (for PHP, see PHP installation). For example, you can create an info.php page as follows:

  • Open php.info:
    nano /var/www/html/example.com/info.php
  • Add the following content:
    <?php
    phpinfo();
    ?>

 

 

Your Apache server is now ready! Does your website use a database? Then, take a look at our MariaDB tutorial. To secure your website, you can use our Apache SSL manual.

Should you have any questions left regarding this article, do not hesitate to contact our support department. You can reach them via the ‘ContactUs’ button at the bottom of this page.

If you want to discuss this article with other users, please leave a message under 'Comments'.

Has this article been helpful?

Create an account or log in to leave a rating.

Comments

Create an account or log in to be able to leave a comment.