
The Linux, Apache, MySQL, and PHP (LAMP) stack is a collection of open-source applications that enable the development and deployment of dynamic web applications on a server. Linux runs as the base operating system while Apache works as a webserver to handle web requests. MySQL functions as a backend database and PHP processes dynamic web application contents.
This article explains how to install the LAMP stack on a Debian 12 server.
Before you begin:
app.example.com.Apache is available in the default package repositories on Debian 12. Follow the steps below to install the Apache on your server.
Install the Apache webserver.
View the installed Apache version on your server..
Your output should be similar to the one below.
Start the Apache service.
Enable the Apache service to start automatically at boot.
View the Apache service status and verify that it's running on your server.
Your output should be similar to the one below.
MySQL is not available in the Debian 12 package repositories by default. Follow the steps below to add the MySQL APT repository information to your server and install the latest database server package.
Download the latest MySQL package repository setup file..
Visit the MySQL APT repository page to verify the latest Debian package file to download on your server.
Run the file to install the latest MySQL repository information on your server.
Select MySQL Server & Cluster option and press Enter when prompted.
Press Up and Down to select your preferred MySQL Server version and press Enter to proceed. For example, select mysql-8.4-lts version.
Press Down, select Ok, and press Enter to save changes.
Update the server's package information index to apply the new MySQL package repository information.
Install the MySQL database server package.
Enter a new root database user password when prompted and press Enter.
Enter the password again to validate it and press Enter to update the root database user.
View the MySQL version on your server.
Your output should be similar to the one below.
Start the MySQL system service.
Enable MySQL to start at boot.
View the MySQL service status and verify that it's running on your server.
Your output should be similar to the one below.
Run the MySQL secure installation script to secure the database server.
Enter the root database user password you set earlier and press Enter.
Enter Y when prompted to setup the VALIDATE PASSWORD component.
Set your preferred password strength policy for your MySQL database server. For example, enter 2 to enable strong password validation.
Enter N when prompted to change the root database user password, or enter Y to change the password.
Enter Y when prompted to remove anonymous users from MySQL.
Enter Y when prompted to disallow remote login for the root user.
Enter Y when prompted to remove the test database from your MySQL database server.
Enter Y to reload the MySQL privileges table and apply all configuration changes.
Output:
PHP is available in the default package repositories on Debian 12. Follow the steps below to install PHP, PHP-FPM, and additional web application modules on your server.
Install PHP and PHP-FPM.
Install PHP modules.
The above command installs the following PHP modules:
php-mysql: Connects PHP to the MySQL database server to perform SQL operations.php-cli: Allows you to run PHP scripts from the Command Line Interface (CLI).libapache2-mod-php: Integrates PHP with the Apache webserver to enable processing and forwarding of PHP requests.View the PHP version installed on your server.
Your output should be similar to the one below.
Start the PHP-FPM service depending on your PHP version such as PHP 8.2.
Enable PHP-FPM to start automatically at boot.
View the PHP-FPM service status and verify that it's running.
Output:
PHP-FPM (FastCGI Process Manager) is a process manager for PHP designed to handle webserver requests for dynamic PHP applications. PHP-FPM offers several enhancements, including process management, server-specific configuration, and improved performance. Follow the steps below to configure PHP-FPM on your server.
Enable the Apache FastCGI module proxy_fcgi to allow the webserver to communicate with PHP-FPM.
Use the ss utility to verify the active PHP-FPM Unix socket path.
Your output should be similar to the one below.
Based on the above output, /run/php/php8.2-fpm.sock is the active PHP-FPM Unix socket path.
Enable Apache's default PHP-FPM configuration for PHP version 8.2.
Restart the Apache webserver to apply the new configuration changes.
Open the default PHP-FPM pool configuration www.conf using a text editor such as nano.
View the PHP-FPM pool name and verify that it is set to www.
Find the following directives and verify that the values are set to www-data.
Find the listen directive and verify that it is set to the PHP-FPM Unix socket path /run/php/php8.2-fpm.sock.
Find the following pool configurations and modify them depending on your server specifications and available resources:
pm: Specifies the process manager type. The dynamic value allows PHP child processes to adjust automatically based on server needs.pm.max_children: Defines the maximum number of PHP child processes that can run simultaneously.pm.start_servers: Sets the number of child processes created on startup, providing an initial pool to handle requests.pm.min_spare_servers: Sets the minimum number of idle child processes. If the number of idle processes is less than the specified value, then some child processes will be created.pm.max_spare_servers: Sets the maximum number of idle child processes allowed. If the number of idle child processes exceeds the specified value, then the excess idle child processes are stopped.Save and close the file.
Restart the PHP-FPM service to apply the new changes.
PHP-FPM (FastCGI Process Manager) connects with Apache using the proxy_fcgi module and the PHP-FPM Unix socket. The proxy_fcgi module forwards requests to the PHP-FPM service which processes PHP scripts and returns the output to the webserver. Follow the steps below to configure Apache with PHP-FPM to serve dynamic web application files on the server.
Create a new Apache virtual host configuration file using a text editor like nano. For example, website.conf.
Add the following configurations to the file. Replace app.example.com with your actual domain name and webmaster@app.example.com with your administrative email.
The above Apache configuration creates a new virtual host that serves web application files from the web root directory /var/www/html/ using your app.example.com domain. Within the configuration:
<VirtualHost *:80>: Defines the virtual host to listen for connections on the HTTP port 80.ServerAdmin webmaster@app.example.com: Sets the webserver administrator's email for receiving critical error notifications and alerts.ServerName app.example.com: Specifies the virtual host domain to deliver web applications..DocumentRoot /var/www/html/: Sets the virtual host's root directory.DirectoryIndex index.html index.php: Sets a list of files to look for when an HTTP client requests an index for the virtual host.<Directory /var/www/html/>: Sets the directory configurations for requests made to the /var/www/html directory.Options Indexes FollowSymLinks: Allows directory listing and the use of symbolic links within the directory.AllowOverride All: Permits .htaccess files to override the settings in the virtual host configuration.Require all granted: Grants all users access to files in the web root directory.<FilesMatch \.php$>: Instructs Apache how to handle PHP files. The SetHandler directive specifies that PHP files should be processed by PHP-FPM using the specified Unix socket.SetHandler "proxy:unix:/var/run/php/php8.2-fpm.sock|fcgi://localhost/": Enables Apache to connect to PHP-FPM and process PHP files using the Unix socket path /var/run/php/php8.2-fpm.sock.ErrorLog ${APACHE_LOG_DIR}/error.log: Specifies the virtual host error log filename and location.CustomLog ${APACHE_LOG_DIR}/access.log combined: Specifies the virtual host's access log file location and writes data in the combined log format.Disable the default Apache virtual host configuration file.
Enable the new Apache virtual host configuration file.
Test the new Apache configuration for errors.
Output:
Restart the Apache webserver to apply your configuration changes.
Allow the HTTP port 80 through the firewall.
Reload UFW to apply the firewall changes.
Create a new index.php file in your web root directory /var/www/html.
Run the following command to add PHP information contents to the file.
Access the /index.php path in your web browser and verify that your PHP information displays.
Apache delivers dynamic web applications on the HTTP port 80 by default which enables unencrypted traffic to the server. Generate SSL certificates to enable HTTPS connections on port 443 and allow all Apache network ports through the default firewall configuration as described in the following sections.
Uncomplicated Firewall (UFW) is available and active on Vultr Debian 12 servers by default. Follow the steps below to configure UFW and allow connections to the Apache webserver.
Allow HTTP connections on port 80.
Allow HTTPS connections on port 443.
Reload UFW to apply the firewall changes.
View the firewall status and verify the new rules are available.
Output:
Install the Certbot Let's Encrypt client plugin for Apache.
Request a new Let's Encrypt SSL certificate on your server. Replace app.example.com with your domain and email@example.com with your actual email address.
Test the Certbot SSL certificate renewal process.
Restart Apache to apply the SSL configuration changes.
Follow the steps below to set up sample web applications, create a database table, and insert a new Greetings from Vultr record into the table. Then, connect to the database and fetch the Greetings from Vultr message from the MySQL database on your server.
Log in to MySQL as root.
Enter the root user password you set earlier when prompted to access the MySQL console.
Create a new sample database. For example, VultrDB.
Switch to the VultrDB database.
Create a new sample database user db_user with a strong password. Replace Str0ng_P@ssw0rd! with your desired password.
Grant db_user full privileges to the VultrDB database.
Reload the MySQL privileges table to apply changes.
Exit the MySQL console.
Create a new insert.php PHP file in your web root directory.
Add the following contents to the file.
Save and close the file.
The above PHP application connects to the MySQL database server as db_user to create a new VultrTable table in the VultrDB database if it does not exist. The application then inserts a new Greetings from Vultr record into the table when the connection is successful.
Create a new display.php application file in your web root directory.
Add the following contents to the file.
Save and close the file.
The above PHP application fetches all records from the VultrTable table in the VultrDB sample database and displays the result when the connection is successful.
Access the /insert.php path using your domain in a new browser window to create a new table and insert data.
Access the /display.php path using your domain in a new browser window to fetch all table records from the database.
You have installed the Apache, MySQL, and PHP (LAMP) stack on your Debian 12 server. You can create and securely deploy multiple web applications using virtual host configurations and PHP-FPM pools on your server. For more information and configuration options, visit the following documentation resources.
0 Comments
Be the first to comment and share your perspective with the community.