Skip to content

Install Web Server di Linux

Pengertian

Web server adalah komputer yang terhubung ke internet dan memiliki beberapa jenis perangkat lunak khusus untuk server web yang diinstal di dalamnya.

Jenis perangkat lunak web server yang paling umum adalah yang dapat melayani halaman web statis dan dinamis ke browser di seluruh dunia. Misalnya, Apache dan Nginx keduanya adalah server HTTP yang dapat melayani halaman web.

Prerequisites

A server running Debian 12.

A non-root user with sudo privileges.

A fully qualified domain name (FQDN) like example.com pointing to the server.

The Uncomplicated Firewall(UFW) is enabled and running.

Everything is updated.

Terminal window
sudo apt update && sudo apt upgrade

Few packages that your system needs.

Terminal window
sudo apt install wget curl nano ufw software-properties-common dirmngr apt-transport-https gnupg2 ca-certificates lsb-release debian-archive-keyring unzip -y

Some of these packages may already be installed on your system.

Step 1 - Configure Firewall

The first step before installing any packages is to configure the firewall to allow HTTP and HTTPS connections.

Check the status of the firewall.

Terminal window
sudo ufw status

You should see something like the following.

Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)

Allow HTTP and HTTPs ports.

Terminal window
sudo ufw allow http
Terminal window
sudo ufw allow https

Check the status again to confirm.

Terminal window
sudo ufw status

Output:

Status: active
To Action From
---
OpenSSH ALLOW Anywhere
80/tcp ALLOW Anywhere
443/tcp ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
80/tcp (v6) ALLOW Anywhere (v6)
443/tcp (v6) ALLOW Anywhere (v6)

Step 2 - Install Web Engine

Install Nginx

Debian 12 ships with an older version of Nginx. To install the latest version, you need to download the official Nginx repository.

Import Nginx’s signing key.

Terminal window
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \ | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

Add the repository for Nginx’s stable version.

Terminal window
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \ http://nginx.org/packages/debian `lsb_release -cs` nginx" \ | sudo tee /etc/apt/sources.list.d/nginx.list

Update the system repositories.

Terminal window
sudo apt update

Install Nginx.

Terminal window
sudo apt install nginx

Verify the installation. On Debian systems, the following command will only work with sudo.

Terminal window
sudo nginx -v

Output:

nginx version: nginx/1.24.0

Start Nginx.

Terminal window
sudo systemctl start nginx

Check the service status.

Terminal window
sudo systemctl status nginx

Output:

nginx.service - nginx - high performance web server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; preset: enabled)
Active: active (running) since Thu 2023-06-15 16:33:46 UTC; 1s ago
Docs: https://nginx.org/en/docs/
Process: 2257 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 2258 (nginx)
Tasks: 2 (limit: 1108)
Memory: 1.8M
CPU: 6ms
CGroup: /system.slice/nginx.service
??2258 "nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf"
??2259 "nginx: worker process"

Install Apache

Terminal window
sudo apt update
Terminal window
sudo apt install apache2

Check the service status.

Terminal window
sudo systemctl status apache2

Output

apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor prese>
Active: active (running) since Thu 2024-01-25 15:37:08 WIB; 5h 16min ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 920 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUC>
Main PID: 1067 (apache2)
Tasks: 7 (limit: 16536)
Memory: 35.8M
CPU: 638ms
CGroup: /system.slice/apache2.service
├─1067 /usr/sbin/apache2 -k start
├─1071 /usr/sbin/apache2 -k start
├─1072 /usr/sbin/apache2 -k start
├─1073 /usr/sbin/apache2 -k start
├─1074 /usr/sbin/apache2 -k start
├─1075 /usr/sbin/apache2 -k start
└─7902 /usr/sbin/apache2 -k start
Jan 25 15:37:08 cidara-MS-7A38 systemd[1]: Starting The Apache HTTP Server...
Jan 25 15:37:08 cidara-MS-7A38 apachectl[953]: AH00558: apache2: Could not reli>
Jan 25 15:37:08 cidara-MS-7A38 systemd[1]: Started The Apache HTTP Server.

Step 3 - Install PHP

Debian 12/ Ubuntu 22.04 ships with PHP 8.3 by default. You can install it by running the following command.

Terminal window
sudo apt-get install ca-certificates apt-transport-https software-properties-common curl lsb-release -y
Terminal window
sudo apt update
Terminal window
sudo apt-get -y install lsb-release ca-certificates curl
Terminal window
sudo apt install php php-fpm php-cli php-mysql php-mbstring php-xml php-gd libapache2-mod-php

Install Multi PHP

Terminal window
sudo apt install apt-transport-https lsb-release ca-certificates wget -y
Terminal window
wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
Terminal window
echo "deb https://packages.sury.org/php/ (lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
Terminal window
sudo apt update

Install PHP 7.4

Terminal window
sudo apt install php7.4 -y

Install PHP 7.4 Extension

Terminal window
sudo apt install php7.4-{common,mysql,xml,xmlrpc,curl,gd,imagick,cli,dev,imap,mbstring,opcache,soap,zip,intl} -y

Install PHP 8.3

Terminal window
sudo apt install php8.3 -y

Install PHP 8.3 Extension

Terminal window
sudo apt install php8.3-{common,mysql,xml,xmlrpc,curl,gd,imagick,cli,dev,imap,mbstring,opcache,soap,zip,intl} -y

Change PHP version to php7.4

Terminal window
update-alternatives --set php /usr/bin/php7.4

Change PHP version to php8.3

Terminal window
update-alternatives --set php /usr/bin/php8.3

Check the version of PHP installed

check php version

Terminal window
php -v
Output:
Terminal window
PHP 8.3.2-1+ubuntu22.04.1+deb.sury.org+1 (cli) (built: Jan 20 2024 14:16:40) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.2, Copyright (c) Zend Technologies
with Zend OPcache v8.3.2-1+ubuntu22.04.1+deb.sury.org+1, Copyright (c), by Zend Technologies

Step 4 - Install MariaDB

Debian 12 does not ship with MySQL by default and they haven’t released an official package for it yet. Therefore, we will be using MariaDB for it. MariaDB doesn’t have an official package for Debian 12 as well but Debian ships with it. Therefore, install it using the following command.

Terminal window
sudo apt install mariadb-server -y

Check the version of MySQL.

Terminal window
mysql --version

Run the MariaDB secure install script.

Terminal window
sudo mysql_secure_installation

You will be asked for the root password. Press Enter because we haven’t set any password for it.

You can enter the MariaDB shell by typing sudo mysql or sudo mariadb on the command line.

Step 5 - Configure MariaDB

Log in to the MariaDB shell.

Terminal window
sudo mysql -u root -p

Create a sample database.

Terminal window
CREATE DATABASE exampledb;

show database

Terminal window
show databases;

Create a test table.

Terminal window
CREATE TABLE exampledb.name_list ( sno INT AUTO_INCREMENT, content VARCHAR(255), PRIMARY KEY(sno) );

Insert test data.

Terminal window
INSERT INTO exampledb.name_list (content) VALUES ("Navjot");

Repeat the above command multiple times to add more entries. Run the following command to check the contents of the table.

Terminal window
SELECT \* FROM exampledb.name_list;

You will receive the following output.

Exit the MySQL shell.

Terminal window
exit

ADD USER MariaDB

Open your Terminal and type:

Terminal window
mysql -u root -p

input your root password (press ENTER if you not configure it)

Then

Terminal window
CREATE USER 'user1'@localhost IDENTIFIED BY 'password1';
Change 'user1' on 'user1'@localhost with your user name
Change 'password1' with your secure password
Terminal window
GRANT ALL PRIVILEGES ON *.* TO 'user1'@localhost IDENTIFIED BY 'password1';
Change 'user1' on 'user1'@localhost with your user name
Change 'password1' with your secure password
Terminal window
FLUSH PRIVILEGES;
Terminal window
exit

Step 6 - Configure PHP-FPM

Open php.ini for editing.

Terminal window
sudo nano /etc/php/8.2/fpm/php.ini

To set file upload sizes, change the values of the upload_max_filesize and post_max_size variables.

Terminal window
upload_max_filesize = 50M
...
post_max_size = 50M

Configure PHP’s memory limit depending on your server resources and requirements.

Terminal window
memory_limit = 256M
Save the file by pressing Ctrl + X and entering Y when prompted.

Open the file /etc/php/8.0/fpm/pool.d/www.conf.

Terminal window
sudo nano /etc/php/8.2/fpm/pool.d/www.conf
We need to set the Unix user/group of PHP processes to nginx. Find the user=www-data and group=www-data lines in the file and change them to nginx.
Also, find the lines listen.owner=www-data and listen.group=www-data in the file and change them to nginx.
Terminal window
listen.owner = nginx
listen.group = nginx
Save the file by pressing Ctrl + X and entering Y when prompted.

Restart the PHP-fpm process.

Terminal window
sudo systemctl restart php8.2-fpm

Step 7 - Install phpMyAdmin

Terminal window
sudo wget -P Downloads https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz
Download Keyring
Terminal window
sudo wget -P Downloads https://files.phpmyadmin.net/phpmyadmin.keyring
Mount Folder Downloads
Terminal window
cd Downloads
Import keyring
Terminal window
sudo gpg --import phpmyadmin.keyring
Terminal window
sudo wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz.asc
Terminal window
sudo gpg --verify phpMyAdmin-latest-all-languages.tar.gz.asc
Create a directory for the site.
Terminal window
sudo mkdir /var/www/html/phpMyAdmin
Extract the archive to the public directory.
Terminal window
sudo tar xvf phpMyAdmin-latest-all-languages.tar.gz --strip-components=1 -C /var/www/html/phpMyAdmin
Copy config file
Terminal window
sudo cp /var/www/html/phpMyAdmin/config.sample.inc.php /var/www/html/phpMyAdmin/config.inc.php
Edit config.ini.php
Terminal window
sudo nano /var/www/html/phpMyAdmin/config.inc.php

find

cfg[‘blowfish_secret’] = ”; ---> change inside with youre secure password,

then press ctrl+x, y , enter

Change Permission
Terminal window
sudo chmod 660 /var/www/html/phpMyAdmin/config.inc.php
Terminal window
sudo chown -R www-data:www-data /var/www/html/phpMyAdmin
Restart web server

Apache:

Terminal window
sudo systemctl restart apache2

Nginx:

Terminal window
sudo systemctl restart nginx
Then you can enter in your web Browser like Chrome http://localhost/phpMyAdmin

Step 9 - Configure Opcache

Opcache is PHP’s caching system. It works by saving precompiled script bytecode in the memory, so every time a user visits a page, it loads faster. Opcache is installed by default. To verify, check the PHP version.

Terminal window
php --version
PHP 8.2.7 (cli) (built: Jun 9 2023 19:37:27) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.7, Copyright (c) Zend Technologies
with Zend OPcache v8.2.7, Copyright (c), by Zend Technologies

This tells us that Opcache is installed and available. In case, it doesn’t show up here, you can install it manually by running the following command.

Terminal window
sudo apt install php-opcache

To change Opcache settings, open the file /etc/php/8.2/fpm/conf.d/10-opcache.ini for editing.

Terminal window
sudo nano /etc/php/8.2/fpm/conf.d/10-opcache.ini
Terminal window
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
Save the file by pressing Ctrl + X and entering Y when prompted.
Restart PHP-FPM.
Terminal window
sudo systemctl restart php8.2-fpm

Step 10 - Install Certbot for SSL

We need to install Certbot to generate free SSL certificates offered by Let’s Encrypt.

You can either install Certbot using Debian’s repository or grab the latest version using the Snapd tool. We will be using the Snapd version.

Debian 12 comes doesn’t come with Snapd installed. Install Snapd package.

Terminal window
sudo apt install snapd

Run the following commands to ensure that your version of Snapd is up to date.

Terminal window
sudo snap install core
sudo snap refresh core
Install Certbot.
Terminal window
sudo snap install --classic certbot

Use the following command to ensure that the Certbot command can be run by creating a symbolic link to the /usr/bin directory.

Terminal window
sudo ln -s /snap/bin/certbot /usr/bin/certbot

Verify if Certbot is functioning properly.

Terminal window
certbot --version

Output:

certbot 2.6.0

Step 11 - Test a demo site

Create the site

Create and open a test page for editing.

Terminal window
sudo nano /var/www/html/example.com/index.php

Paste the following code in it.

Terminal window
<?php
user = "exampleuser";
password = "YourPassword2!";
database = "exampledb";
table = "name_list";
try {
db = new PDO("mysql:host=localhost;dbname=database", user, password);
echo "<h2>Members List</h2><ol>";
foreach(db->query("SELECT content FROM table") as row) {
echo "<li>" . row['content'] . "</li>";
}
echo "</ol>";
} catch (PDOException e) {
print "Error!: " . e->getMessage() . "<br/>";
die();
}
Save the file by pressing Ctrl + X and entering Y when prompted.
Create an SSL Certificate

Run the following command to generate an SSL Certificate.

Terminal window
sudo certbot certonly --nginx --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m name@example.com -d example.com

The above command will download a certificate to the /etc/letsencrypt/live/example.com directory on your server.

Generate a Diffie-Hellman group certificate.

Terminal window
sudo openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096

Check the Certbot renewal scheduler service.

Terminal window
sudo systemctl list-timers

You will find snap.certbot.renew.service as one of the services scheduled to run.

Terminal window
NEXT LEFT LAST PASSED UNIT ACTIVATES
.....
Sun 2023-02-26 06:32:00 UTC 9h left Sat 2023-02-25 18:04:05 UTC 2h 59min ago snap.certbot.renew.timer snap.certbot.renew.service
Sun 2023-02-26 06:43:20 UTC 9h left Sat 2023-02-25 10:49:23 UTC 10h ago apt-daily-upgrade.timer apt-daily-upgrade.service
Sun 2023-02-26 09:00:06 UTC 11h left Sat 2023-02-25 20:58:06 UTC 5min ago apt-daily.timer apt-daily.service

Do a dry run of the process to check whether the SSL renewal is working fine.

Terminal window
sudo certbot renew --dry-run

If you see no errors, you are all set. Your certificate will renew automatically. Configure Nginx

Create and open the file /etc/nginx/conf.d/example.conf for editing.
Terminal window
sudo nano /etc/nginx/conf.d/example.conf

enforce HTTPS

Terminal window
server {
listen 80;
listen [::]:80;
server_name example.com;
return 301 https://hostrequest_uri;
}
Save the file by pressing Ctrl + X and entering Y when prompted.

Open the file /etc/nginx/nginx.conf for editing.

Terminal window
sudo nano /etc/nginx/nginx.conf

Add the following line before the line include /etc/nginx/conf.d/*.conf;.

server_names_hash_bucket_size 64;
Save the file by pressing Ctrl + X and entering Y when prompted.

Verify your Nginx configuration.

Terminal window
sudo nginx -t

If you see no errors, it means you are good to go. Start the Nginx server.

Terminal window
sudo systemctl start nginx

Load your website by visiting https://example.com in your browser and you will see the following page.