How to set up a Secure LAMP Stack on a Linux Server

Configurare noua (How To)

Situatie

A LAMP stack (Linux, Apache, MySQL/MariaDB, PHP) is one of the most popular setups for hosting websites and web applications. Setting it up securely is essential to prevent unauthorized access, SQL injections, and other vulnerabilities.

Solutie

  • Step 1: Install Apache Web Server

Apache is the most widely used web server. Install it with:

sudo apt update

sudo apt install apache2

Start and enable the service: sudo systemctl enable –now apache2

To allow HTTP and HTTPS traffic, configure the firewall:

sudo ufw allow ‘Apache Full’

Check if Apache is running:

sudo systemctl status apache2

  • Step 2: Install MySQL/MariaDB and Secure It

Install MySQL or MariaDB (an alternative database system):

sudo apt install mysql-server

Secure the database installation:

sudo mysql_secure_installation

This will:

✔ Remove anonymous users

✔ Disable remote root login

✔ Set a strong root password

Login to the database: sudo mysql -u root -p

  • Step 3: Install PHP and Necessary Modules

PHP processes server-side scripts. Install it with:

sudo apt install php libapache2-mod-php php-mysql

To verify PHP installation, create a test file:

echo “<?php phpinfo(); ?>” | sudo tee /var/www/html/info.php

Now, open http://your-server-ip/info.php in a browser. If you see PHP details, it’s working.

Remove this file afterward for security:

sudo rm /var/www/html/info.php

  • Step 4: Secure Apache and PHP Configuration

Modify the Apache configuration file for security:

sudo nano /etc/apache2/apache2.conf

Set the ServerTokens and ServerSignature values to Prod:

Disable directory listing:

Restart Apache:

sudo systemctl restart apache2

Secure PHP settings by editing php.ini:

sudo nano /etc/php/*/apache2/php.ini

Find and modify these settings:

Restart Apache: sudo systemctl restart apache2

  • Step 5: Enable SSL with Let’s Encrypt (HTTPS)

To encrypt traffic, install Let’s Encrypt SSL:

sudo apt install certbot python3-certbot-apache

Run the SSL setup:

sudo certbot –apache

This automatically configures SSL and enables HTTPS. Verify renewal:

sudo certbot renew –dry-run

Tip solutie

Permanent

Voteaza

(0 din 3 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?