Setup LAMP on ubuntu

Many times when we need to setup a sever at cloud or somewhere, we need some good and step wise document for install PHP, apache and mysql if this requirement comes sometimes. Before setup LAMP on Ubuntu I would like to provide some small explanation . A LAMP stack is a group of some open source software Apache, MySQL, PHP for Linux distribution.

Here I have created an easy and useful document for setup a working environment of LAMP on Ubuntu. In this article we assumed that you have an Ubuntu system already and now need to setup PHP, Apache and MySQL.

Step1 – Apache installation

The most popular web servers in the world is Apache so it is good choice for hosting websites :).
sudo apt update
sudo apt install apache2
This command will ask you about required space. Enter Y and hit Enter to proceed.

Step2 – Updating Firewall

UFW firewall should be enabled and allows HTTP and HTTPS traffic. To check by this command
sudo ufw app list
This command should return below output
Available applications:
Apache Full
Apache Secure

You can check Apache Full profile by below command. It should display that traffic enabled to ports 80 and 443. These both ports are for http and https.
sudo ufw app info "Apache Full"
Output of this command will be like this
Profile: Apache Full
Title: Web Server (HTTP,HTTPS)
Description: Apache v2 is the next generation of the omnipresent Apache web server.

Now this command will allow incoming traffic for http and https
sudo ufw allow in "Apache Full"
That’s it your Apache installation has been done. You can check it on browser by type ip address. It will show apache default page.

Step3 – Install CURL

CURL is mostly required in various application and websites. Although it is not required in setup process but you can install for future requirement. It is just by typing a single line and enter
sudo apt install curl

Step4 – MySQL Installation

Install MySQL
To install mySQL please run below command
sudo apt install mysql-server
For verification that mysql running or not use below command
sudo systemctl status mysql

Setup DB admin (root) password
By default root password is blank. For secure MySQL need to run below command
sudo mysql_secure_installation
After hit you will see various setting options and change password. You can set root user password here.

Change Authentication for root user
By default, auth_socket is used to authenticate MySQL root user. We can change authentication method from auth_socket to mysql_native_password by login into mysql.
sudo mysql
SELECT user,authentication_string,plugin,host FROM mysql.user;

You will see list of users with authentication method. Now you can change it by below query
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Step5 – PHP Installation

We can install PHP by below command
sudo apt install php libapache2-mod-php php-mysql
Mostly apache will first get index.html. You can change order of first look extension by this command
sudo nano /etc/apache2/mods-enabled/dir.conf
This command will display something like below
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.php index.xhtml index.htm
Now move the PHP index file to the first position, like below
DirectoryIndex index.php index.html index.cgi index.xhtml index.htm
Save by CTRL+X then Y and ENTER. After this restart the server by below command.
sudo systemctl restart apache2

Step6 – Install other packages (optional)

This is optional. You can install many packages and extension as per your requirement. Some command are below
sudo apt-cache search php- | less
sudo apt-cache show php-gd
sudo apt install php-gd

To install all modules, although it is not necessary
sudo apt-get install php*

I hope you really feel good after successful setup your LAMP stack on ubuntu. We can improve more and In my next articles I will share about virtual host setup so that you can manage various domains/subdomains at same server.
Till then enjoy and happy coding…

Join the Conversation

1 Comment

Leave a comment

Your email address will not be published. Required fields are marked *