
Apache, MySQL, and PHP (LAMP stack) is a collection of open-source applications that enable the development and delivery of dynamic web applications on a server. Linux runs as the operating system to deploy the applications, Apache works as the web server, MySQL as the database backend, and PHP processes dynamic web application contents on the server.
This article explains how to install the LAMP stack on Ubuntu 24.04 and configure the server to deliver web applications using the stack.
Before you begin:
Deploy an Ubuntu 24.04 instance on Vultr.
Access the server using SSH as a non-root user with sudo privileges.
Set up a new A record for your domain that points to the server IP address.
The latest Apache version is available in the default APT repositories on Ubuntu 24.04. Follow the steps below to update the default package index and install the latest Apache web server package on your server.
Update the server's package index.
Install Apache.
Start the Apache service.
Enable the Apache service to start automatically at boot time.
Verify that the Apache server service is running.
Output:
Allow connections to the HTTP port 80 through the default firewall configuration.
Access your domain or server IP using a web browser such as Chrome and verify that the default Apache web page displays.
MySQL works as the database backend within the LAMP stack and you can substitute it with MariaDB depending on your needs. The latest MySQL package is available in the default repositories on Ubuntu 24.04. Follow the steps below to install the latest MySQL version on your server using the default APT package manager.
Install the MySQL database server package.
Enable the MySQL service to start automatically at boot time.
Start the MySQL service.
View the MySQL service status and verify that it's running.
Output:
Based on the active (running) result in the above output, the MySQL database server is active and running on your server.
Run the MySQL secure installation script to disable insecure defaults and enable authentication on your database server.
Reply to the following MySQL database server options when prompted:
VALIDATE PASSWORD: Enter Y to enable password strength checks on the database server.Password strength policy: Enter 2 to enable multi-character password usage on the server.Remove anonymous users: Enter Y to remove anonymous users from the database server.Disallow root login remotely: Enter Y to disable remote access to the root database user.Remove test database: Enter Y to delete the default MySQL test database on your server.Reload privileges tables now: Enter Y to reload the MySQL privilege tables and apply your configuration changes.Your output should look like the one below when successful:
Log in to the MySQL console as the root user.
Alter the root database user to use a new strong password. Replace Strong@@password123 with your desired password.
Replace password with a strong password.
Flush the MySQL privileges table to apply the new user changes.
Exit the MySQL console.
Log in to the MySQL console again as the root user and enter the password you set earlier when prompted.
Create a new sample database content_database.
View all databases and verify that the new database is available.
Output:
Create a new MySQL user such as dbadmin with a strong password. Replace Strong@@password123 with your desired password.
Grant the user full privileges to your sample database content_database.
Flush the MySQL privileges table to apply changes.
Exit the MySQL shell.
PHP is a core LAMP stack component that processes dynamic content and integrates with your MySQL database server to read or create database records. PHP-FPM (FastCGI Process Manager) manages the connection to the PHP service and optimizes the application performance using pools of worker processes. Follow the steps below to install the latest PHP version and configure PHP-FPM to enable dynamic web application processes on your server.
Install PHP and the PHP-FPM module.
Install common PHP extensions on your server.
The above command installs the following PHP modules:
php-mysql: Enables PHP to connect and interact with the MySQL database server.libapache2-mod-php: Enables the Apache web server to process and run PHP scripts.php-opcache: Enables caching of precompiled PHP scripts in memory for faster execution.php-cli: Enables access to PHP in your server terminal.View the installed PHP version on your server.
Output:
Start the PHP-FPM service based on the installed PHP version on your server. For example, PHP 8.3.
Enable PHP-FPM to start at boot time.
View the PHP-FPM service status and verify that it's running.
Output:
PHP-FPM ensures optimized performance for PHP applications on your server using pools depending on your server memory. Follow the steps below to configure PHP-FPM to work with the Apache web server and modify the default pool configurations to enable efficient resource management.
Enable the required Apache modules.
The above command enables the following modules on your web server:
proxy_fcgi: Enables Apache to work as a proxy with PHP-FPM.setenvif: Sets the necessary environment variables to enable connections between Apache and PHP-FPM.Enable the default PHP-FPM configuration.
Restart the Apache web server to apply the changes.
Switch to the PHP-FPM pool configurations directory.
Open the default www.conf PHP-FPM pool configuration.
Verify that the default PHP-FPM pool name www.
Find and verify the following directives are set to www-data to enable PHP-FPM to use the default web server user profile.
Find the following pool configurations and modify them to suite your desired server needs:
pm: Sets the default process manager. The value dynamic enables PHP child processes to dynamically adjust on your server.pm.start_servers: Defines the number of PHP child processes to create at startup. The default value is 2.pm.max_children: Sets the maximum number of PHP child processes that can be active simultaneously. The default value is 5.pm.min_spare_servers: Setsthe minimum number of idle PHP child processes. The default value is 1.pm.max_spare_servers: Specifies the maximum number of idle PHP child processes. The default value is 3.pm.max_requests: Limits the number of requests a PHP child process can handle before it's recycled.Save and close the file.
Restart the PHP-FPM service to apply your configuration changes.
The Apache web server uses the mod_proxy_fcgi module to communicate with PHP-FPM using the service UNIX socket or the default TCP port 9000 depending on your pool configuration. In the following steps, set up a new Apache virtual host configuration and connect to the PHP-FPM service using the UNIX socket.
Remove the default Apache virtual host configuration files.
Create a new Apache virtual host configuration file. For example, app.example.com.conf.
Add the following configurations to the file. Replace app.example.com with your actual domain.
Save and close the file.
The above configuration creates a new Apache virtual host that listens for incoming connections on the default HTTP port 80 and serves web contents using your app.example.com domain. Then, all PHP file requests are forwarded to the PHP-FPM process using the /var/run/php/php8.3-fpm.sock UNIX socket. Within the configuration:
<VirtualHost *:80>: Enables the virtual host profile to listen for connections on port 80.<Directory /var/www/app.example.com>: Sets the web root directory to deliver web application files.<FilesMatch \.php$>: Forwards all PHP file requests to the PHP-FPM socket /var/run/php/php8.3-fpm.sock using the FastCGI protocol.ErrorLog, CustomLog: Enable custom paths to store the virtual host error and access logs respectively.Enable the new Apache virtual host configuration.
Test the Apache configuration for syntax errors.
Output:
Create the virtual host web root directory /var/www/app.example.com defined in your configuration.
Create a new sample PHP file info.php.
Add the following contents to the file.
Save and close the file.
The above application code displays information about the PHP version and installed modules on your server when accessed in a web browser.
Restart Apache to apply your configuration changes.
Access your domain using a web browser such as Chrome and append the /info.php path to verify that your PHP application information displays.
Uncomplicated Firewall (UFW) is available and active on Vultr Ubuntu 24.04 servers by default. The Apache web server delivers dynamic web applications on your server using the default HTTP port 80 while other LAMP stack components use internal TCP ports such as the MySQL port 3306 and the PHP-FPM port 9000. Follow the sections below to configure the default firewall to allow connections to the default web server port 80 and set up trusted SSL certificates to enable HTTPS connections on port 443.
View the default firewall status and verify that it's active.
Output:
View the available UFW application profiles and verify that the Apache profile is available.
Output:
Allow the Apache Full profile to enable HTTP and HTTPS connections on the server.
Reload the firewall rules to apply changes.
View the UFW status and verify that the Apache connection rules are available in the firewall table.
Output:
Install the Certbot Let's Encrypt client tool using Snap.
Request a new SSL certificate for your domain. Replace app.example.com with your actual domain and admin@example.com with your email.
Output:
Test the Certbot automatic SSL certificate renewal process.
Restart the Apache web server to apply your SSL configuration changes.
Follow the steps below to set up a new sample table in your existing content_database MySQL database to connect with your PHP application and display the message Hello World! Greetings from Vultr when accessed in a web browser.
Log in to the MySQL console using the sample database user dbadmin you created earlier.
Enter the dbadmin user password when prompted to access the MySQL console.
Switch to the sample database content_database.
Create a new sample table messages with two columns, content_id and content to store your data.
The above SQL query creates a new table with the following specifications:
content_id: Contains numeric values and automatically increments unique data on each new row.content: Holds mixed content data with up to 255 characters.Insert new data to the messages table. For example, add a new Hello World! Greetings from Vultr string to the content column.
View all table data to verify that the new string is added to the column.
Output:
Exit the MySQL console.
Create a new sample PHP application file setup.php in your web root directory /var/www/html/app.example.com.
Add the following contents to the file.
Save and close the file.
The above PHP application code connects to the MySQL database content_database and displays data from the content column in the messages table when accessed. When the MySQL database does not contain any records, a No records found message displays or Connection Failed. when the connection to the database server fails.
Grant the Apache user www-data full privileges to your web root directory.
Access your domain using the /setup.php path in your web browser to verify that your PHP application displays the Hello World! Greetings from Vultr string content from your MySQL database.
You have installed and configured Apache, MySQL, and PHP (LAMP stack) on your Ubuntu 24.04 server. In addition, you created sample dynamic applications to test access between all LAMP stack components and securely run the applications on your server. For more information and configuration options about each component, visit the following official documentation resources:
0 Comments
Be the first to comment and share your perspective with the community.