A LAMP stack in Linux is a bundle of software that includes Linux as the operating system, Apache as the web server, MySQL (or MariaDB) as the database system, and PHP (or Perl/Python) as the scripting language. It is used for building and deploying dynamic websites and web applications by combining these components. LAMP stands for “Linux, Apache, MySQL, and PHP.” Together, these technologies can be used to create a fully-functional web server. Linux is the most popular, secure and open source operating system used in web servers. Apache HTTP Server is a free and open-source web server that delivers web content through the internet. MySQL is a relational database engine that allows you to store and manage data. PHP is a widely used open source and general-purpose server-side scripting language used mainly in web development to create dynamic websites and applications. In this tutorial, we’ll be using MariaDB, which is a drop-in replacement of the MySQL Database Server. It includes all major open-source storage engines, and allows you to manage relational databases for storing and organizing data.
Before proceeding with installing LAMP on Ubuntu 22.04, make sure you have the following prerequisites:
Root access to your server or a sudo user.
To install the LAMP stack on Ubuntu 22.04, you can use the apt package manager to install Apache, MySQL, and PHP. Once these components are installed, you need to ensure that they are properly configured and integrated for web development. Follow the steps below to accomplish this.
It is important to update the Ubuntu packages in the package manager cache to ensure that you have the latest available versions. To do this, use the following command:
After updating our Ubuntu package cache, we will now install the MariaDB database server, the M in the LAMP acronym. We’ll use MariaDB instead of MySQL because it includes more features and supports new storage engines, in addition to its high performance.
To install MariaDB, execute the following command:
Tap the y key then Enter to continue the installation.
In the preceding command, you install two packages:
mariadb-server: The MariaDB database server which stores data.
mariadb-client: The MariaDB database client which allows you to interact with and manage the database server via the command line.
Once the installation is finished, verify that the MariaDB database server is running properly by executing the following command to check out the MariaDB service status:
The output should show that the service is enabled and running:
Here, you can see that the service is active and running in the line
Ensure That MariaDB Starts at Boot
To make sure that the MariaDB database server starts with the system at boot, use the enable subcommand of the systemctl command. To do so, execute the following command:
You should receive the following output:
Securing The MariaDB Database Server
MariaDB comes with some unsafe default settings which may expose your database server to dangerous security vulnerabilities. It’s recommended that you run a security script that comes with MariaDB to strengthen your database server and minimize the risk of database intrusions or breaches.
To secure your MariaDB database server, execute the following command, where you will be presented with seven prompts:
You’ll be asked for your current root password:
Enter current password for root (enter for none):
Next, you’ll be asked whether you want to use the unix_socket authentication method:
Tap Y to enable unix_socket authentication for better security.
The unix_socket authentication method allows users to connect to their MariaDB account without entering a password. Instead, it verifies the user’s Unix credentials. This makes it a passwordless security mechanism. However, keep in mind that it cannot be used to grant multiple Unix users access to a single MariaDB user account. Therefore, access to your MariaDB database server is restricted to the Unix user who has the corresponding MariaDB account.
The unix_socket authentication method is particularly strong due to the default sturdiness of Unix user security in preventing remote access. However, unskilled administration of your Unix user system may expose dangerous vulnerabilities. So, keep a wide and open eye on potential Unix user security issues such as weak passwords or accidental password exposure, excessive sudo permissions that allow users to execute commands of a different Unix user, scripts of other users executed by your MariaDB Unix user, running unsafe scripts, or weak remote access security.
Next, you’ll be asked to change the root password:
Tap Y to set a new password for root, and re-enter it for validation.
Next, you’ll be asked to remove anonymous users:
Tap Y to remove the anonymous users that come with your MariaDB installation.
Next, you’ll be asked whether you want to disallow remote root logins:
Tap Y to disable remote root login.
Next, you’ll be asked whether you want to remove the test database that comes with your MariaDB installation:
Tap Y to remove your MariaDB test database and disable access to it.
For the changes you’ve made to take effect, you’ll be asked to reload your MariaDB privilege tables:
Tap Y to reload your MariaDB privilege tables. This will ensure that your changes will take effect immediately.
With this, your MariaDB installation is more secure than before, provided that you follow security best practices.
You now have the MariaDB database server installed, and you’re ready to store and manage your data. Next, you’ll need to serve the data you store using an HTTP server, which is the Apache HTTP server in the LAMP stack.
After installing and securing the MariaDB database server, the next step in our LAMP stack installation is installing the Apache HTTP server, which serves web content.
Use apt to install Apache using the following command:
You’ll be asked to confirm the installation. Tap the y key then Enter to continue.
Once the installation is finished, check Apache’s version to confirm that it was properly installed using the following command:
You should receive an output that looks as follows:
Then check Apache’s status:
The output should show that the Apache service is enabled and running:
You now have Apache installed. Next, you’ll install the PHP language.
With MariaDB you can store and manage data, and the Apache HTTP server allows you to serve it. You now need to install PHP to dynamically display data, and allow the final user to interact with your web service via easy-to-use web forms or API calls.
Use the following command to install the main PHP package, along with basic PHP packages that allow you to interact with your database and HTTP server:
When prompted, tap the y key then Enter to continue the installation.
In the preceding command, you install the following packages:
php: The main PHP language package.
php-mysql: A package that allows PHP to communicate with MySQL-based databases such as MariaDB.
php-xml: A package that provides a DOM, SimpleXML, WDDX, XML, and XSL module for PHP.
php-mbstring: A package that provides the MBSTRING module for PHP, which is used to manage non-ASCII strings.
libapache2-mod-php: A package that allows Apache to handle PHP files.
To ensure PHP was successfully installed, check its version using the following command:
You should receive an output similar to the following:
This means that you’ve successfully installed PHP.
After we’ve finished installing all the components of the LAMP stack, we will now test our Apache server by creating a small HTML file in an Apache Document Root folder –a folder that contains your website files.
To create a document root folder to serve our test file, create a new directory called mysite inside the /var/www directory, which is the default document root for Apache. Note that you can use your site’s name instead of mysite:
Next, create a file called index.html inside your mysite directory:
As you can see, this is straightforward HTML code.
For Apache to know about your mysite document root directory and serve its index.html on your server’s IP address or domain, you will need to modify the default Apache configuration file. To do this, first open it using the following command:
Then make sure to set the correct server name and document root path:
Run the following command to ensure that your new configuration file does not contain any errors:
You might receive a AH00558: apache2: Could not reliably determine the server’s fully qualified domain name message. You can ignore this message because it’s just an informational note that does not affect the purposes of this tutorial, and Apache will run as expected.
You should receive an output that ends with the following:
This means you can safely reload Apache. Otherwise, you will get a specific description pointing out the error you have to fix.
Restart Apache for these modifications to take effect:
After you do so, use your browser and navigate to your server’s domain name or IP address:
You should see that the index.html file you created earlier is properly served.