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.
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-fpmsystemctl 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>
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'.