Easy Steps to Setup Web Server on Debian 12 or Ubuntu 24.04 for Beginners

Apache, MariaDB, PHP

This guide will walk you through the steps to set up a basic web server on Debian 12 or Ubuntu 24.04 to host PHP applications like WordPress, Joomla, Drupal, OpenCart, Prestashop, osCommerce, etc. We will install and configure Apache, MariaDB, PHP and SSL and prepare the environment for deploying a PHP application.

Step 1: Install Required Packages

First, install the necessary packages for Apache, MariaDB, PHP, and Certbot (for SSL):

sudo apt update
sudo apt install apache2 mariadb-server mariadb-client php php-fpm php-intl php-mbstring php-gd php-xml php-curl php-mysql php-zip php-xmlrpc python3-certbot-apache ssl-cert


  • Apache2: The web server.
  • MariaDB: The database server.
  • PHP: The scripting language for your application.
  • Certbot: For obtaining and managing SSL certificates.

Step 2: Enable PHP-FPM with Apache

PHP-FPM (FastCGI Process Manager) is a modern way to handle PHP requests. Enable it with Apache:

Debian 12:

sudo a2enconf php8.2-fpm

Ubuntu 24.04:

sudo a2enconf php8.3-fpm

This command creates a symlink in /etc/apache2/conf-enabled/ to enable PHP-FPM configuration.

Step 3: Enable SSL and Configure Apache

Enable the default SSL site and configure Apache to allow .htaccess overrides for PHP applications like WordPress:

1. Enable the default SSL site:

sudo a2ensite default-ssl

2. Edit the SSL and default configuration files to allow .htaccess overrides:

Open /etc/apache2/sites-enabled/000-default.conf and /etc/apache2/sites-enabled/default-ssl.conf in a text editor (e.g., nano or vim):

sudo nano /etc/apache2/sites-enabled/000-default.conf
sudo nano /etc/apache2/sites-enabled/default-ssl.conf

Add the following block inside the <VirtualHost> section:

<Directory "/var/www/html">
    Options FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

This configuration allows .htaccess files to override Apache settings, which is required for WordPress and many other PHP applications.

3. Restart Apache to apply the changes:

sudo systemctl restart apache2

Step 4: Restart PHP-FPM

Restart the PHP-FPM service to ensure it’s running with the latest configuration:

Debian 12:

sudo systemctl restart php8.2-fpm

Ubuntu 24.04:

sudo systemctl restart php8.3-fpm

Step 5: Set Up a Database for Your Application

Most PHP applications, like WordPress, require a database. Use MariaDB to create a database and user:

1. Log in to MariaDB as the root user:

sudo mysql -u root

2. Create a database and user, and grant privileges:
Replace databasenamedatabaseuser, and Se6uReP@55w0rD with your desired values.

CREATE DATABASE databasename;

GRANT ALL PRIVILEGES ON databasename.* TO databaseuser@'localhost' IDENTIFIED BY 'Se6uReP@55w0rD';

FLUSH PRIVILEGES;

EXIT;

Step 6: Deploy Your PHP Application

 

Now that the server is configured, you can deploy your PHP application. We will use WordPress in this example.

1. Download and extract WordPress:

cd /var/www/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xvzf latest.tar.gz
sudo mv wordpress/* .
sudo rm -rf wordpress latest.tar.gz

2. Set the correct permissions:

sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

3. Access your application via a web browser:

Open https://your-server-ip or https://yourdomain.tld in your browser. Follow the WordPress installation wizard, and use the database details you created earlier.

Step 7: Secure Your Server with SSL

If you haven’t already, use Certbot to obtain a free SSL certificate from Let’s Encrypt:

sudo certbot --apache

Follow the prompts to configure SSL for your domain. Certbot will automatically configure Apache to use the SSL certificate.

Step 8: Test Your Setup

1. Visit your domain (e.g., https://yourdomain.tld) to ensure the application loads correctly.

2. Verify that SSL is working by checking for the padlock icon in your browser’s address bar.

Optional Step: Adjust PHP options

You may need to adjust some PHP options in accordance with your PHP application requirements.

Create and edit .user.ini file:

sudo nano /var/www/html/.user.ini

And insert, as example:

expose_php = Off
max_execution_time = 600
max_input_time = 600
max_input_vars = 10000
memory_limit = 256M
post_max_size = 64M
upload_max_filesize = 64M
max_file_uploads = 50
session.gc_probability = 1
session.gc_maxlifetime = 14400

It may require 300 seconds for PHP to re-read these options.

Conclusion

You’ve successfully set up a basic web server on Debian and Ubuntu to host PHP applications like WordPress. This guide covered installing Apache, MariaDB, PHP, and SSL, as well as configuring the environment for a PHP application. You can now deploy and manage your PHP applications with ease.


About Olvy ( www.olvy.net / www.olvy.eu ) :

Olvy is a private and independent Limited Liability Company based in Bratislava, Slovakia, in the heart of Europe. We combined our invaluable 20+ years experience to develop innovative and reliable, lightning-fast and affordable Managed Cloud Hosting services for Everyone. From a small blog to a growing eCommerce – Olvy takes care of your website 24/7.

Leave a Reply