
Setting up a robust web server environment on Ubuntu is a fundamental step for hosting web applications and dynamic websites. This guide provides a definitive method for installing the popular LAMP stack, consisting of Apache, the widely-used web server, MariaDB, a powerful relational database system, and PHP 7.2, a versatile scripting language, on the Ubuntu 18.04 LTS platform.
The process begins by ensuring your system’s package list is up-to-date. Execute the command sudo apt update
followed by sudo apt upgrade
to fetch the latest information about software packages and apply any necessary updates.
Next, install the Apache web server. Use the command sudo apt install apache2
. Once installation is complete, verify that Apache is running with systemctl status apache2
. For security, it’s crucial to configure your firewall. If using UFW (Uncomplicated Firewall), allow incoming traffic on the standard web ports by running sudo ufw allow 'Apache Full'
. Confirm the firewall status with sudo ufw status
.
Proceed to install the database server. MariaDB is a community-developed fork of MySQL and is an excellent choice. Install it using sudo apt install mariadb-server mariadb-client
. Immediately after installation, secure your MariaDB instance by running the sudo mysql_secure_installation
script. This script will guide you through setting a root password, removing anonymous users, disallowing remote root login, and removing the test database. Follow the prompts carefully to harden your database security.
Now, install PHP, specifically PHP 7.2 along with the necessary module to connect to MariaDB. Use the command sudo apt install php7.2 libapache2-mod-php7.2 php7.2-mysql php7.2-cli
. The libapache2-mod-php7.2
package integrates PHP with Apache, php7.2-mysql
allows PHP to communicate with MariaDB, and php7.2-cli
provides the command-line interpreter for PHP.
To ensure that Apache recognizes and processes PHP files, it’s often beneficial to adjust the directory index configuration so that index.php
is checked before index.html
. Edit the Apache directory index file, typically located at /etc/apache2/mods-enabled/dir.conf
, and move index.php
to the beginning of the list within the <IfModule mod_dir.c>
block.
After installing PHP and making any configuration changes, restart the Apache service for the changes to take effect: sudo systemctl restart apache2
.
Finally, test your installation. Create a simple PHP file, often named info.php
, in the Apache web root directory (/var/www/html/
). Add the following line to this file: <?php phpinfo(); ?>
. Save the file and then access it through your web browser by navigating to http://your_server_ip/info.php
. You should see a page displaying detailed information about your PHP configuration, confirming that PHP is correctly installed and working with Apache and the necessary modules. Remember to remove the info.php
file afterwards for security reasons.
This completes the installation of a stable and functional LAMP stack on Ubuntu 18.04, providing a solid foundation for your web development projects.
Source: https://kifarunix.com/how-to-install-lamp-stack-apachemariadb-php-7-2-on-ubuntu-18-04-lts/