In this tutorial, we explain the installation and initial configuration of an Apache web server (with httpd-tools, mod_ssl, and PHP-FPM) in Ubuntu 18.04 or 20.04.
Together with MariaDB and PHP, Apache forms the so-called LAMP stack (Linux Apache Mariadb PHP) which is used for hosting dynamic websites. 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, Debian 9 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:
apt -y install lsb-release apt-transport-https ca-certificates
wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/php7.3.list
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 in step 1 and 2.
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
Step 3
This is also a good moment to make any adjustments to your php.ini settings 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 so all changes can take effect:
a2enmod proxy_fcgi setenvif
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.
Do you host a single website on your server? Then, you can now upload it (e.g. using FTP) into the folder /var/www/html (an example file called index.html already exists in this folder).
DNS
In order to host a website it's not only important to have your website's files stored on a server. You also need to be able to find them through the internet. For this, DNS records are used globally; DNS records indicate on which servers services such as webhosting or email can be found for a domain.
Below, you'll find an example of what DNS records may look like for a domain registered at TransIP and hosted on a VPS. A more detailed explanation on configuring DNS records can be found in this tutorial.
Please note that for a SandboxVPS you'd ony use an IPv6 record.
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
Replace example.com by your actual domain name.
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/example.com/public_html
mkdir -p /var/log/apache2/example.com
touch /var/log/apache2/example.com/error.log
touch /var/log/apache2/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.
Stap 4
Enable the virtual host with the commands:
a2ensite example.com.conf
systemctl reload apache2
Extra domains
Repeat step 1 - 4 for each additional domain you're adding.
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();
?>
You can then visit your page at example.com/info.php in your browser to test the output.
Your Apache server is now ready! Does your website use a database? Then, take a look at our MariaDB manual. 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'.