
The LAMP stack (Linux, Apache, MySQL, PHP) is a set of open-source tools used to build and deliver dynamic web applications. Linux serves as the operating system, Apache handles web requests, MySQL manages databases, and PHP processes dynamic content.
In this article, you are to install the LAMP stack on 22.04.
Before you begin:
Have an Ubuntu 22.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.
Follow the steps below to update the package index and install the latest Apache web server 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 service is running.
Output:
Allow incoming connections to the HTTP port 80.
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 replace it with MariaDB if needed. Follow the steps below to install the latest MySQL version using the default APT package manager.
Install the MySQL database server package.
Enable the to start automatically at boot time.
Start the MySQL service.
View the MySQL service status and verify that it's running.
Output:
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 validation on the database server.Password strength policy: Enter 2 to enforce multi-character password requirements.Remove anonymous users: Enter y to delete anonymous users from the server.Disallow root login remotely: Enter y to block remote access for the root user.Remove test database: Enter y to remove the default MySQL test database.Reload privilege tables now: Enter y to apply the changes by reloading privilege tables.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.
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, processing dynamic content and interacting with the MySQL database. PHP-FPM (FastCGI Process Manager) handles PHP connections and improves performance through worker process pools. Follow the steps below to install the latest PHP version and configure PHP-FPM for dynamic web application processing 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: Allows PHP to connect and interact with the MySQL database.libapache2-mod-php: Enables Apache to process and run PHP scripts.php-opcache: Caches precompiled PHP scripts in memory for faster execution.php-cli: Provides access to PHP via the 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.1.
Enable the service to start at boot time.
View the service status and verify that it's running.
Output:
PHP-FPM optimizes PHP application performance on your server by managing pools based on available memory. Follow the steps below to configure PHP-FPM with Apache 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.
Locate the following pool configurations and adjust them according to your server's needs:
pm: Sets the process manager. The dynamic value allows PHP child processes to scale dynamically.pm.start_servers: Defines the number of PHP child processes to start. Default is 2.pm.max_children: Sets the maximum number of simultaneous active PHP child processes. Default is 5.pm.min_spare_servers: Specifies the minimum number of idle PHP child processes. Default is 1.pm.max_spare_servers: Sets the maximum number of idle PHP child processes. Default is 3.pm.max_requests: Limits the number of requests a PHP child process can handle before being 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 via the UNIX socket or the default TCP port 9000, depending on your pool configuration. In the below steps, you’ll configure a new Apache virtual host and connect it 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 configuration above creates an Apache virtual host that listens on port 80 and serves content for the app.example.com domain. All PHP requests are forwarded to PHP-FPM via the /var/run/php/php8.1-fpm.sock UNIX socket. Key points in the configuration:
<VirtualHost *:80>: Listens for connections on port 80.<Directory /var/www/html/app.example.com>: Sets the web root directory.<FilesMatch \.php$>: Forwards PHP requests to the PHP-FPM socket /var/run/php/php8.1-fpm.sock using FastCGI.ErrorLog, CustomLog: Specify custom paths for 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.
Follow the steps below to configure the default firewall to allow connections on port 80 for HTTP 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.
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 content_database MySQL database, connect it to your PHP application, and display the message Hello World! Greetings from Vultr 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 PHP code connects to the content_database MySQL database and displays data from the content column in the messages table. If no records are found, it shows No records found, or Connection Failed if the database connection 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’ve set up Apache, MySQL, and PHP (LAMP stack) on your Ubuntu 22.04 server, and created sample applications to test the connections between all components. To explore more configuration options, refer to the official documentation for each component:
0 Comments
Be the first to comment and share your perspective with the community.