
The LAMP stack—Linux, Apache, MySQL, and PHP—is a collection of open-source tools used to develop and serve dynamic web applications. In this setup:
This article explains how to install and configure the LAMP stack on Ubuntu 20.04 to host web applications on your server.
Before you begin:
Have an Ubuntu 20.04 server.
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 version of Apache is available in Ubuntu 20.04’s default APT repositories. Follow the steps below to update the package index and install Apache.
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 serves as the database backend in the LAMP stack, but you can also substitute it with MariaDB if needed. The latest MySQL package is available in Ubuntu 20.04’s default repositories. Follow the steps below to install MySQL using the 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.
During the MySQL installation, you’ll be prompted with several options. Respond as follows to configure your server:
y to enable password strength checks.2 to enforce multi-character passwords.y to remove anonymous users.y to disable remote access for the root user.y to delete the default MySQL test database.y to reload the MySQL privilege tables and apply your 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 key component of the LAMP stack, responsible for processing dynamic content and interacting with the MySQL database. PHP-FPM (FastCGI Process Manager) optimizes performance by managing connections to PHP using pools of worker processes.
Install PHP and the PHP-FPM module.
Install common PHP extensions on your server.
The above command installs the following PHP modules:
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 7.4.
Enable PHP-FPM to start at boot time.
View the PHP-FPM service status and verify that it's running.
Output:
PHP-FPM optimizes PHP application performance by managing pools of worker processes, depending on your server’s memory. Follow the steps below to configure PHP-FPM to work with the Apache web server and adjust the default pool settings for better 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.
pm: Defines the process manager. Set to dynamic to allow PHP child processes to adjust based on server demand.pm.start_servers: Sets the number of PHP child processes to create at startup. Default value: 2.pm.max_children: Defines the maximum number of PHP child processes that can run simultaneously. Default value: 5.pm.min_spare_servers: Specifies the minimum number of idle PHP child processes. Default value: 1.pm.max_spare_servers: Specifies the maximum number of idle PHP child processes. Default value: 3.pm.max_requests: Limits the number of requests a PHP child process can handle before being recycled to ensure optimal performance.Save and close the file.
Restart the PHP-FPM service to apply your configuration changes.
Apache communicates with PHP-FPM through the mod_proxy_fcgi module, either using a UNIX socket or the default TCP port 9000, depending on your PHP-FPM pool configuration. Follow these steps to set up a new Apache virtual host and connect it to PHP-FPM 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 sets up a new Apache virtual host that listens for incoming traffic on port 80 and serves content for the app.example.com domain. PHP file requests are forwarded to the PHP-FPM process via the /var/run/php/php7.4-fpm.sock UNIX socket. The key components of this configuration include:
<VirtualHost *:80>: Configures the virtual host to listen on port 80.<Directory /var/www/html/app.example.com>: Sets the root directory for web application files.<FilesMatch \.php$>: Forwards PHP file requests to the PHP-FPM socket using the FastCGI protocol.ErrorLog, CustomLog: Defines custom paths for the virtual host's error and access logs.Enable the new Apache virtual host configuration.
Test the Apache configuration for syntax errors.
Output:
Create the virtual host web root directory /var/www/html/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 enabled by default on Vultr Ubuntu 20.04 servers. The Apache web server uses the default HTTP port 80 to serve dynamic web applications, while other LAMP stack components, such as MySQL (3306) and PHP-FPM (9000), use internal TCP ports. Follow the steps below to configure UFW to allow connections on port 80 for the web server and set up trusted SSL certificates to enable HTTPS 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 create a new sample table in your existing content_database MySQL database. This table will connect with your PHP application and display the message Hello World! Greetings from Vultr when accessed through 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 PHP application code above connects to the content_database MySQL database and retrieves data from the content column in the messages table. If no records are found in the database, it will display a No records found message. If the connection to the database server fails, it will show a Connection Failed message.
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 successfully installed and configured Apache, MySQL, and PHP (LAMP stack) on your Ubuntu 20.04 server. Additionally, you created sample dynamic applications to test the interaction between all LAMP stack components and ensured secure execution on your server.
0 Comments
Be the first to comment and share your perspective with the community.