Article overview

Help article

Installing an Apache webserver in Debian 9

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

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, 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 

After the installation you'll see a notification such as below, stating that PHP-FPM is not enabled yet.

NOTICE: Not enabling PHP 7.3 FPM by default.
NOTICE: To enable PHP 7.3 FPM in Apache2 do:
NOTICE: a2enmod proxy_fcgi setenvif
NOTICE: a2enconf php7.3-fpm

Execute the last two commands above to configure and start PHP-FPM, so:

a2enmod proxy_fcgi setenvif
a2enconf php7.3-fpm

 

Step 4

Finally, restart Apache:

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
    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 using 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();
    ?>

 

 

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.