If you want to work on the Linux environment, you will have to be familiar with the command line. If you are a PHP Developer and prefer to go with Linux OS, you will have to opt for LAMP Stack. But, If you are an absolute beginner, then there might be questions like What is LAMP Stack and Why will use it? Here, nothing to worry about these terminologies. I will explain you step by step and here in this post. Today, I will guide you in the installation of the LAMP stack. For the Apache server, it is required to install apache2 service. LAMP stands for Linux, Apache, MySQL, and PHP. These are the group of software that is used for web development for Linux OS. Also, these are open-source software packages. So, you can install it easily.
For the LAMP Stack setup, I am going with Ubuntu 20.04.2 LTS. Here, I will show you how to install PHP, apache2, and MySQL.
- 1 What is LAMP Stack?
- 2 Prerequisites
- 3 Installing LAMP Stack in Ubuntu
- 4 Update Package Repository in Ubuntu
- 5 Install Apache in LAMP Stack Ubuntu
- 6 Check Apache Service in Ubuntu
- 7 Check Firewall in Ubuntu
- 8 Install MySQL in Ubuntu For LAMP Stack
- 9 Install PHP in LAMP Stack Ubuntu
- 10 Install phpMyAdmin in LAMP Stack Ubuntu
- 11 Restart Apache2 Server and MySQL Database
- 12 Conclusion
What is LAMP Stack?
The LAMP is a collection of open-source software that is used to develop web applications. It stands for Linux, Apache, MySQL, and PHP. Here, Apache is a server that creates a local server for us. It allows us to run the web application in our local system. That is called the localhost. MySQL is the database management tool and the last one is the PHP that is a scripting language. So, all these tools will be under the LAMP stack. Hence, you can easily install it through the command line.
However, it is most important to install every software in a order to work properly. But, don’t worry, I will be guiding you step by step here.
Before moving to this post, I am assuming you are ready with the below requirements-
- Ubuntu 20.04.2 LTS (As an Operating System)
- An Ubuntu user with Sudo Privileges
- A terminal and command line access
Installing LAMP Stack in Ubuntu
So, in the very first step, you will have to open the terminal. Press the window key and in the search option, just type the terminal. Also, you can open the terminal using the short key using the CTRL + ALT + T.
Here, I opened the terminal.
Before start installation of any software tool in Ubuntu, it is must to check the updates of the software repository cache. It will ensure that every packages are updated.
Update Package Repository in Ubuntu
The Ubuntu package repository will be updated using the APT tool. Here, APT stands for Advanced Package Tool. It is a command-line utility that is used to install, update, and remove the package in Linux. So, for updating the package cache hit the below command in the terminal. For the LAMP installation in Ubuntu, it is necessary to update the package.
sudo apt update
The above command will list out the available upgrades for the pre-installed packages.
If the upgrde packages are available then you will have to upgrade the required packages. Therefore, for upgrading the package hit the below command there.
sudo apt upgrade
The above command will start upgrading the available packages. Also, it will prompt for confirmation. Press y (yes) to proceed with the instllation process. Hence, just allow it. On other hand, you may pass the default permission of Yes with the command. So, that it won’t ask again.
sudo apt upgrade -y
So, after upgrading the packages, we can proceed the LAMP installation in Ubuntu.
Install Apache in LAMP Stack Ubuntu
For the LAMP Installation in Ubuntu, the very first package is the Apache server. Here, we will install apache2 service. So, just hit the below command in the terminal.
sudo apt install apache2
Here, you might be wondering about apache2. Here, apache2 is the package available for the Apache service. The above command will start installing the apache service.
Press y (yes) to permit the installation.
After the installation of Apache service, it is necessary to check the service is installed and running.
Check Apache Service in Ubuntu
sudo service apache2 status
If everything has done correctly, you will get the apache2 service status as running as showing below.
Check Firewall in Ubuntu
After the installation of the Apache service, you have to check the firewall in ubuntu. Here, all the HTTP requests should go through the Apache server. So, it is important to check this.
sudo ufw app list
Here, ufw stands for Ubuntu Firewall.
Here, you have to allow the Apache Full for the firewall. So, that it will able to check the HTTP requests.
sudo ufw allow in "Apache Full"
If by default the Firewall is disabled, then you can check it and enable it. For checking the firewall status, you can hit the below command.
sudo ufw status
If the firewall status is disabled, then just enable it by giving the flag of enable.
sudo ufw enable
After enabling the firewall, you can re-check the firewall status. You can see here, the firewall status is running.
After completing the above steps, you will be able to run the localhost in the browser. Just hit the http://localhost in the URL and you will have the default page of Apache2 as showing below.
In the next step, we will install the MySQL database management. So, just start it.
Install MySQL in Ubuntu For LAMP Stack
In the LAMP stack, after installing the Apache server, the next is the MySQL database. Here, in Ubuntu, we will install the MySQL server package. It will allow database management. Just open the terminal and hit the below command.
sudo apt install mysql-server
It will install the latest version of MySQL.
It is not mandatory to create the root user password. Here, if you will create the password of MySQL root user, then keep remind the default user in Linux is root. And the default user of MySQL database is also root.
After installing the MySQL, you may enter into the MySQL database by using the below command.
If you have set the password for the MySQL user then it will ask for the password.
sudo mysql -p
In case, if you have multiple user in MySQL database then you will have to put the specific username and password to enter into that.
sudo mysql -u root -p
It will ask for the database user password. Then, you may enter into the database.
So, MySQL is installed successfully. In the upcoming Linux post, I will show you to create new user and password in MySQL.
Install PHP in LAMP Stack Ubuntu
For creating a web application using PHP it will require to install PHP. So, with PHP it is necessary to install some other libraries which will be required. Therefore, hit the below command in the terminal.
sudo apt install php libapache2-mod-php php-mysql
In the above command php-mysql will install the library for connecting PHP with MySQL.
After finishing the installation, you can create any file with .php extension and run it in the browser.
I have created a file inside the var/www/html folder.
Inside this directory, I created a file named phpinfo.php.
After creating the file, let’s open it through the default editor.
Here, I opened the file inside the default editor that is gedit. Gedit is the default text editor of the GNOME desktop environment and part of the GNOME Core Applications.
<?php echo phpinfo(); ?>
Now, open the browser and run this file. Here, in the result, you can see PHP is installed and working properly.
So, we have completed the LAMP stack installation. In addition, we can install phpMyAdmin to access the MySQL in GUI mode.
Install phpMyAdmin in LAMP Stack Ubuntu
If you want to access MySQL in the GUI mode then you will have to install phpMyAdmin in Ubuntu. This is optional, you can install it if you required it. Otherwise, you may access the command line.
Before installing the phpMyAdmin, it will require some libraries. So, firstly, install the below libraries.
sudo apt install php-mbstring php-zip php-gd php-curl php-json
After installing the libraries, you can proceed to install phpMyAdmin in Ubuntu.
sudo apt install phpmyadmin
It will ask to add the server for the phpMyAdmin. So, just select apache2 (httpd) service and proceed the installation.
Once the phpMyAdmin installated, just restart the apache and mysql server.
Restart Apache2 Server and MySQL Database
To apply the new changes, here you will have to restart the apache server. So, hit the below command.
sudo systemctl restart apache2
The above command will restart the apache server. Similarly, restart MySQL server.
sudo systemctl restart mysql
After restarting the apache2 and mysql, just open the browser and hit the http://localhost/phpmyadmin.
Finally, we have completed the steps of LAMP installation in Ubuntu. In the LAMP Stack, we have to install the Apache server first. Then it is required to install PHP and MySQL. But, firstly, you can install MySQL then PHP. So, steps are clearly mentioned in this post. Also, I have shown you to install phpMyAdmin in Ubuntu for the LAMP stack. So, I hope you will love this post. Thank you.