Setting up a LAMP stack on your Ubuntu server or desktop provides a robust environment for developing and hosting web applications. LAMP stands for Linux, Apache, MySQL, and PHP, which together create a powerful platform. This guide walks you through each step of the installation process.
Prerequisites
- A system running Ubuntu (18.04, 20.04, or newer).
- A user with sudo privileges.
- Basic knowledge of using the terminal.
Step 1: Update Your Package Index
Before installing any new software, it’s important to update your package index to ensure you have the latest information about the available packages.
sudo apt update
Step 2: Install Apache
Apache is a widely-used web server. To install Apache, run the following command:
sudo apt install apache2
After the installation is complete, start and enable Apache to ensure it runs on startup:
sudo systemctl start apache2
sudo systemctl enable apache2
To verify that Apache is running, open your web browser and navigate to http://your_server_ip or http://localhost. You should see the Apache2 default page.
Step 3: Install MySQL
MySQL is a popular relational database management system. Install it by running the below command:
sudo apt install mysql-server
Once the installation is complete, secure your MySQL installation by running the following command:
sudo mysql_secure_installation
This script will guide you through setting a root password and securing your MySQL installation.
Step 4: Install PHP
PHP is a server-side scripting language used to develop dynamic web applications. Install PHP along with some common modules. run the below command.:
sudo apt install php libapache2-mod-php php-mysql
Step 5: Adjust Apache to Prefer PHP Files
To ensure that Apache prioritizes PHP files over others, modify the dir.conf file:
sudo nano /etc/apache2/mods-enabled/dir.conf
Make sure index.php comes before index.html as shown below:
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
Save the file and exit (Ctrl+X, Y, Enter).
Step 6: Restart Apache
To apply the changes, restart Apache, use below command:
sudo systemctl restart apache2
Step 7: Test PHP
To ensure PHP is working correctly with Apache, create a info.php file in the Apache root directory, run the fowlloing command:
sudo nano /var/www/html/info.php
Add the following PHP code:
<?php
phpinfo();
?>
Save the file and exit. Open your web browser and navigate to http://your_server_ip/info.php or http://localhost/info.php. You should see a page displaying information about your PHP configuration.
Step 8: Install phpMyAdmin (Optional)
phpMyAdmin is a web-based interface for managing MySQL databases. To install phpMyAdmin, run the fowling command:
sudo apt install phpmyadmin
During the installation, you’ll be prompted to choose a web server (select Apache) and configure a database for phpMyAdmin.
After the installation, include phpMyAdmin configuration in Apache, use the below command:
sudo nano /etc/apache2/apache2.conf
Add the following line at the end of the file:
Include /etc/phpmyadmin/apache.conf
Save and exit the file, then restart Apache:
sudo systemctl restart apache2
You can now access phpMyAdmin by navigating to http://your_server_ip/phpmyadmin or http://localhost/phpmyadmin.
Conclusion
Congratulations! You have successfully installed and configured a LAMP stack on your Ubuntu system. With Apache serving your web pages, MySQL managing your databases, and PHP processing your dynamic content, you’re ready to start developing and hosting web applications. Optionally, phpMyAdmin provides a user-friendly interface for database management.
If you encounter any issues or need further customization, don’t hesitate to contact us on [email protected]. Happy coding!