
Linux, Apache, MySQL, and PHP (LAMP Stack) is a collection of open-source software applications that manage the runtime and development of dynamic web applications on a server. Apache works as a web server to deliver web application files, MySQL as the database backend, and PHP processes dynamic web application contents to deliver web applications on the server.
This article explains how to install the LAMP stack on Rocky Linux 9 to enable the development and delivery of web applications on your server.
Before you begin:
app.example.com.Apache is available in the default repositories on Rocky Linux 9 and uses the httpd web server package to run. Follow the steps below to update the DNF package index and install the Apache on your server.
Update the server's package index.
Install the httpd Apache web server package.
Enable the httpd Apache service to automatically start at system boot.
Output:
Start the Apache web server.
View the Apache service status and verify that it's running.
Output:
The Apache service is active and running on your server based on the active (running) value in the above output.
Temporarily stop the default firewalld utility to enable access to the Apache HTTP port 80.
Access your server IP address using a web browser such as Chrome to verify that Apache is actively delivering web applications.
MySQL is available in the default package repositories on Rocky Linux 9. Follow the steps below to install the latest MySQL version on your server using the default DNF package manager.
Install the MySQL database server package.
View the installed MySQL version on your server.
Your output should be similar to the one below:
Enable MySQL to automatically start at system boot.
Output:
Start the MySQL service.
View the MySQL service status and verify that it's running.
Output:
The MySQL service is active and running on your server based on the above output.
Run the MySQL secure installation script to enable authentication on your database server.
Reply to the following prompts to set up the root database user password, remove anonymous users, disallow remote root login, and remove the test database on your server.
root database user password.root database user password.root database user.Restart the MySQL service to apply changes.
Log in to the MySQL console as the root user.
Enter the root user password you set earlier when prompted to access the console.
Create a new sample database example_vultr.
List all available databases to verify that the new database is created.
Output:
Exit the MySQL console.
PHP enables the processing of dynamic web contents and PHP-FPM manages PHP connections to process requests using pools with specific resource configurations. Follow the steps below to install PHP and configure PHP-FPM on the server.
Install PHP and PHP-FPM.
Install common PHP extensions required by most dynamic web applications.
View the installed PHP version on your server.
Your output should be similar to the one below:
View the installed PHP-FPM version and verify that it matches your PHP version information.
Your output should be similar to the one below:
Enable the PHP-FPM service to automatically at system boot.
Output:
Start the PHP-FPM service.
View the PHP-FPM service status and verify that it's running.
Output:
PHP-FPM is active and running on your server based on the above service output.
Open the default PHP-FPM pool configuration using a text editor such as nano.
apache as the default value to enable PHP-FPM connections to the Apache web server.listen directive and verify the PHP-FPM UNIX socket path to use when connecting to the service.Save and close the file.
Restart PHP-FPM to apply your configuration changes.
Apache connects to the PHP-FPM service using the UNIX socket path defined in your pool configuration. PHP-FPM uses a virtual host configuration in the /etc/httpd/conf.d/ directory by default to handle connections to the Apache web server. Follow the steps below to modify the virtual host file and configure Apache with PHP-FPM to process PHP requests on your server.
Open the default PHP-FPM virtual host configuration php.conf using a text editor such as nano.
Find the following mod_php block and verify that SetHandler points to your PHP-FPM UNIX socket path /run/php-fpm/www.sock.
Save and close the file.
The above configuration enables Apache to process and forward PHP requests to your PHP-FPM UNIX socket path. Replace the default path /run/php-fpm/www.sock with the value in your PHP-FPM pool configuration.
Test the Apache configuration for syntax errors.
Output:
Restart Apache to apply your configuration changes.
Create a new sample PHP application file phptest.php in the default web root directory /var/www/html/.
Add the following contents to the file.
Save and close the file.
The above PHP application code displays your installed PHP version information when accessed in a web browser.
Access your server IP address in a new web browser window using the /phptest.php URL path.
Verify that the Apache web server delivers your PHP application and displays the version information in your browser.
Apache delivers web applications using virtual host configurations on your server. PHP-FPM processes dynamic web application requests while the MySQL database server interacts with PHP using the php-mysqlnd module. Follow the steps below to create a new sample PHP application that queries the MySQL database to output a Greetings from Vultr message when accessed in a web browser.
Log in to the MySQL database server as the root user.
Create a new user dbadmin to use with the example_vultr database you created earlier. Replace Strong@@password123 with your desired password.
Grant the user full privileges to the example_vultr database.
Switch to the database.
Create a new sample table greetings with two columns.
The above SQL query creates a new greetings table with the following columns:
id: Stores numeric data that is auto-incremented whenever a new record is created.message: Stores mixed character data (VARCHAR) such as strings.Add a new Greetings from Vultr! string in the message column within the table.
Exit the MySQL console.
Create a new Apache virtual host configuration in the /etc/httpd/conf.d/ directory. For example, app.example.com.conf.
Add the following contents to the file.
Save and close the file.
The above configuration creates a new Apache virtual host that listens for incoming connections on the HTTP port 80 using your domain app.example.com to deliver web application files from the /var/www/app.example.com directory. The default PHP-FPM virtual host configuration /etc/httpd/conf.d/php.conf processes all dynamic PHP requests to the Apache web server.
Test the Apache configuration for errors.
Output:
Create the new virtual host's web root directory /var/www/app.example.com.
Create a new sample PHP application file index.php in the directory.
Add the following contents to the file.
Save and close the file.
The above PHP application connects to the MySQL database server using your provided user details to select and display all records in the message column within the greetings table.
Enable the Apache SELinux web server context on the virtual host's web root directory and all files.
Apply the SELinux context.
Restart the Apache web server to apply changes.
Access your domain in a new web browser window.
Verify that Apache serves your PHP application and displays the message Greetings from Vultr when the database connection is successful.
Firewalld is the default firewall management utility that is available and active on Vultr Rocky Linux 9 servers by default. Use the firewall to allow network connections on the Apache HTTP port 80 to deliver web applications on a server. In addition, generate trusted SSL certificates using a trusted authority such as Let's Encrypt to secure connections to the Apache web server with HTTPS traffic. Follow the steps below to set up new firewall rules and secure your Apache virtual host domain with SSL certificates.
Start the firewalld utility.
Allow network connections to the Apache HTTP port 80.
Allow network connections to the Apache HTTPS port 443.
Reload the firewall configuration to apply changes.
View all services allowed through your firewall configuration.
Output:
Install the Certbot Let's Encrypt client plugin for Apache.
Generate a new SSL certificate for your virtual host domain. Replace app.example.com with your actual domain and admin@example.com with your email address.
Output:
Test the Certbot automatic SSL renewal process.
Restart Apache to apply your configuration changes.
Access your domain in a new web browser window using HTTPS and verify that Apache serves your web application files.
You have installed Apache, MySQL, and PHP (LAMP stack) on your Rocky Linux 9 server to deliver dynamic web applications. You can create multiple virtual host configurations and deliver web applications on your server using different domains that point to your server's IP address. For more information and configuration options on each application within the stack, visit the following resources:
0 Comments
Be the first to comment and share your perspective with the community.