1. Overview
The Apache web server is one of the most popular and powerful Linux web server among all of the Linux server hosting and web servers available in the world. It is an open-source web server that can be configured to serve a single or multiple websites with ease of administration and flexibility.
In this post will show you how to install and configure Apache Linux web server on Debian 9 Stretch.
2. Prerequisites
In this tutorial how to install and configure Apache Linux web server on Debian 9, it is supposed that:
a. You have already install Debian 9 Stretch Linux server up and running. In case that you don’t, you would probably like to read this link for help. Installing Debian OS With Logical Volume Manager (LVM)
b. You have already done the initial Linux server setup. Please refer to this link for help. Debian 8.x Initial Server Configuration
c. You have already configured a MariaDB database server. Please refer to this link. Installing And Configuring MariaDB Server on Debian 9 (Stretch)
d. You have a self signed SSL certificate generated. Please refer to this link Generating a Self Signed SSL Certificate in RHEL/CentOS 7.
3. System Architecture Diagram
We will set up the Apache Linux web server as show in the following diagram. For security and performance optimization purpose, we will need to separated the Linux Web server and the Database server in two different servers. The Apache Linux web server with hostname “vkcent-web01” and IP address of 10.0.0.1 will connect to database on the remote MariaDB database server with hostname “vkcent-dbs01” and IP address of 10.0.0.2.
4. Install Apache Package
To install Apache Linux web server, execute the following commands.
# apt-get -y install apache2
After finish the installation, we need to check if the Apache web service is started, up and running.
# service apache2 status
To verify Apache Linux web server functionality, we can open a browser on the client computer and enter http://10.0.0.1, a default web page of Apache should appear as the following.
5. Configure Virtual Host
5. 1 Install PHP7 to Support Apache
We need to install the following PHP modules for a basic MariaDB database server support.
# apt-get -y install php7.0 libapache2-mod-php7.0 php7.0-mysql php7.0-gd php7.0-opcache
To change PHP date and timezone, edit /etc/php.ini configuration file, to your physical location as the following and restart Apache service.
# vim /etc/php/7.0/apache2/php.ini date.timezone = Asia/Phnom_Penh # service apache2 restart
If we want to get a full information list about PHP from your browser, we can create a info.php file on Document Root of Apache Linux web server using the following commands, restart Apache Linux web server service, and direct your browser to the http://10.0.0.1/info.php address.
# vim /var/www/html/info.php <?php phpinfo(); ?> # service apache2 restart
5.2 Connect to MariaDB Database Server
To connect the remote MariaDB database server from Apache Linux web server, execute the following command.
# mysql -u webdbuser01 -p -h 10.0.0.2 Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 5 Server version: 10.1.26-MariaDB-0+deb9u1 Debian 9.1 Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | webdb01 | +--------------------+ 2 rows in set (0.00 sec)MariaDB [(none)]> use webdb01; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [webdb01]> show tables; +-------------------+ | Tables_in_webdb01 | +-------------------+ | tbname | +-------------------+ 1 row in set (0.00 sec) MariaDB [webdb01]> select * from tbname; +------+---------+ | id | name | +------+---------+ | 1 | Vannath | | 2 | Solida | +------+---------+ 2 rows in set (0.01 sec)
5.3. Create a Sample Web Page
Now let create a directory to store this sample web page.
# cd /var/www # mkdir web01
To display the values from the remote MariaDB database server (IP 10.0.0.2) on Apache Linux web server (IP 10.0.0.1) virtual host, we need to create a basic PHP file as the following on Apache Linux web server (IP 10.0.0.1).
# cd /var/www/web01 # cd /var/www/web01 # vim index.php <?php echo "<h1>Welcome to Web01</h1>"; echo "<table style='border: solid 1px black;'>"; echo "<tr><th>Id</th><th>Name</th></tr>"; class TableRows extends RecursiveIteratorIterator { function __construct($it) { parent::__construct($it, self::LEAVES_ONLY); } function current() { return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>"; } function beginChildren() { echo "<tr>"; } function endChildren() { echo "</tr>" . "\n"; } } $servername = "10.0.0.2"; $username = "webdbuser01"; $password = "p@ssword123"; $dbname = "webdb01"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $conn->prepare("SELECT * FROM tbname"); $stmt->execute(); // set the resulting array to associative $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { echo $v; } } catch(PDOException $e) { echo "Error: " . $e->getMessage(); } $conn = null; echo "</table>"; ?>
We can run this script manually to check if it is really work.
# php index.php <h1>Welcome to Web01</h1><table style='border: solid 1px black;'><tr><th>Id</th><th>Name</th></tr><tr><td style='width:150px;border:1px solid black;'>1</td><td style='width:150px;border:1px solid black;'>Vannath</td></tr> <tr><td style='width:150px;border:1px solid black;'>2</td><td style='width:150px;border:1px solid black;'>Solida</td></tr>
5.4 Create VHost for Web01
To host a website on a Linux web server, we need to create a virtual host file in the virtual host configurations directory which is /etc/apache2/sites-available The following file file is the virtual host for the website with domain web01.techspacekh.com.
# cd /etc/apache2/sites-available/ # vim web01.techspacekh.com.conf <VirtualHost *:80> ServerAdmin sysadmin@techspacekh.com ServerName web01.techspacekh.com DocumentRoot /var/www/web01 ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Next, we need to enable the new Virtual Host file by using the following command.
# a2ensite web01.techspacekh.com.conf
Now, we need to restart Apache service.
# service apache2 restart
To access to our hosting web site with domain as configured in the VHost above we need DNS record for this domain. If you don’t have a DNS record for this domain, we just edit the host file. The host file for Windows is located in “C:\Windows\System32\drivers\etc\host” and you can edit it with Notepad.
10.0.0.1 web01.techspacekh.com
After editing the host file, we can test by ping to the domain name and make use the reply IP address for this domain name is IP of Apache Linux web server.
>ping web01.techspacekh.com Pinging nextcloud.techspacekh.com [10.0.0.1 ] with 32 bytes of data: Reply from 10.0.0.1 : bytes=32 time<1ms TTL=64 Reply from 10.0.0.1 : bytes=32 time<1ms TTL=64 Reply from 10.0.0.1 : bytes=32 time<1ms TTL=64 Reply from 10.0.0.1 : bytes=32 time<1ms TTL=64 Ping statistics for 10.0.0.1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms
To verify virtual host configuration, we can open a browser on the client computer and enter http://web01.techspacekh.com, and the following web page should be appeared.
5.5. Create VHost for Web02
Since the advantages of virtual host is that can have many web site in a single Linux web server, we can try to create another website with domain web02.techspacekh.com as the following.
# cd /etc/apache2/sites-available/ # vim web02.techspacekh.com.conf <VirtualHost *:80> ServerAdmin sysadmin@techspacekh.com ServerName web02.techspacekh.com DocumentRoot /var/www/web02 ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Create a directory to store web page for virtual host web02 as the following.
# cd /var/www # mkdir web02
We do not need to create new web page, just copy the old web page to directory /var/www/web02 and edit it as the following.
# cp /var/www/web01/index.php /var/www/web02/ # vim /var/www/web02/index.php <?php echo "<h1>Welcome to Web02</h1>";
We need enable the new Virtual Host file by using the following command and restart Apache service as the following.
# a2ensite web02.techspacekh.com.conf # service apache2 restart
Now, we need to verify the virtual host configuration by open a browser on the client computer and enter http://web02.techspacekh.com, and the following web page should be appeared.
9. Configure SSL Certificate for Web01
In order to set up the SSL certificate, first we need to enable the Apache SSL module, This will automatically enabled the Apache SSL module configuration, and Apache will be able to start using an SSL certificate after it is restarted.
# a2enmod ssl
Next, we need to generate a self signed SSL certificate. Please refer to this link for how to create an self signed SSL certificate. Generating a Self Signed SSL Certificate in RHEL/CentOS 7.
Then, we need to edit the virtual host file of web01 as the following.
# vim /etc/apache2/sites-available/web01.techspacekh.com.conf <VirtualHost *:443> ServerAdmin sysadmin@techspacekh.com ServerName web01.techspacekh.com DocumentRoot /var/www/web01 # SSLEngine on SSLCertificateFile /etc/ssl/private/techspacekh.com/techspacekh.com.crt SSLCertificateKeyFile /etc/ssl/private/techspacekh.com//techspacekh.com.key # ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
We need to restart Apache services as the below.
# service apache2 restart
Right now, open a browser on the client computer and enter https://web01.techspacekh.com, and the following web page should be appeared.
10. Redirect HTTP to HTTPS Protocol
Right now the domain name web01.techspacekh.com is accessible via HTTP port 80 and HTTPS port 443. To force user to use HTTPS port 443, need to redirect the port 80 to port 443, First, we need to enable the Rewrite Apache module as the following.
# a2enmod rewrite
Then, we need to edit the virtual host configuration file as the following.
# vim /etc/apache2/sites-available/web01.techspacekh.com.conf <VirtualHost *:80> RewriteEngine on RewriteCond %{SERVER_PORT} !^443$ RewriteRule ^/(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L] </VirtualHost> <VirtualHost *:443> ServerAdmin sysadmin@techspacekh.com ServerName web01.techspacekh.com DocumentRoot /var/www/web01 # SSLEngine on SSLCertificateFile /etc/ssl/private/techspacekh.com/techspacekh.com.crt SSLCertificateKeyFile /etc/ssl/private/techspacekh.com//techspacekh.com.key # ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
We need to restart Apache services as the below.
# service apache2 restart
Now try to access http://web01.techspacekh.com, and it will redirect to https://web01.techspacekh.com automatically.
7. Conclusion
That’s all about how to install and configure Apache Linux web server on Debian 9 Stretch from Tech Space KH. Hopefully, you can find this guide informative. If you have any questions or suggestions you can always leave your comments below. I will try all of my best to review and reply them.