Setup LAMP Stack Environment (Linux, Apache, MySQL, PHP) on Ubuntu 18.04 - Laravel
When you love to work in open source development, LAMP (Linux, Apache, MYSQL, PHP) is one of the best choices. If you are a PHP developer then most of the time your client asks you set up a complete LAMP server on AWS or any other VPS. Apache handles all web requests with PHP and MYSQL help to store the database. In our example, I will use AWS for a complete setup.
For complete setup, you need to install Ubuntu 18.04 and port 80 should be open for the public on your VPS.
Advanced Package Tool is a package management system for Linux to install and remove the application (packages). We discussed the Homebrew package management system for MAC in another discussion.
Before starting anything we need to update the package information in fresh installed Ubuntu. First, you need to go into
sudo mode and update package information.
$ sudo su
$ apt-get update
Install Apache2 and enable Firewall:
To install the apache run the following command.
$ sudo apt install apache2
When you run the command Ubuntu will start to download packages and prompt to continue it will look like, you need to just type Y and press ENTER.
Do you want to continue? [Y/n]
Run the following command to allow incoming HTTP and HTTPS traffic
$ sudo ufw allow in "Apache Full"
You can verify apache service is running.
$ sudo systemctl status apache2.service
If the service is not running you can run the following command
$ sudo systemctl start apache2.service
Now you can see your see default server page just browse in the browser
If you see the page then the apache2 server is working perfectly otherwise you can see the error log in
Good, now your web browser is running perfectly its time to install one database to store the data. MYSQL is an open-source relational (Database Management System) is built with C and C++. To install the MYSQL run the following command.
$ sudo apt install mysql-server
It will start to download the packages and prompt to continue you can type Y and press ENTER
Do you want to continue? [Y/n]
It will take some time to install depending upon the internet connection. Like apache2 you check the status of the MYSQL service for this run the following command.
$ sudo systemctl status mysql.service
You can start and stop the MYSQL service with the following command
$ sudo systemctl start mysql.service $ sudo systemctl stop mysql.service
To complete the MYSQL installation you need to run MYSQL setup
It will show you several steps to follow. Just keep reading and follow the instruction.
- First, it will ask you to set up the password just type Y and press ENTER
VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin?
- After that, it will ask you to set the password strength, type 0, 1, or 2 for complexity respectively and press ENTER.
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:Now it will ask you to enter the password and Re-enter
New password: Re-enter new password:
- Now it will ask you are you agree with a provided password just type Y and press ENTER
Do you wish to continue with the password provided? (Press y|Y for Yes, any other key for No) :
- It prompts about to remove the user, now it depends on your choice in our example just type N and press ENTER
Remove anonymous users? (Press y|Y for Yes, any other key for No) :
- Now it will ask you to access the database remotely it depends on your choice.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :
- Again another step to remove the database depends on your choice.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :
- At last step, it asks you to reload the services just type Y and press ENTER
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :
Oh, tired? But MYSQL installation is not finished yet. After this, all steps just run the following command.
$ sudo mysql
This command help to enter into MySQL mode and show you a terminal (shell/command line) like this.
When MySQL is installed it carries the one default database MySQL with user table. This user table allows or disallows to connect to the database. To see the users run the following command, It will list some users.
mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
+------------------+-------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+-------------------------------------------+-----------------------+-----------+ | root | | auth_socket | localhost | | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | debian-sys-maint | *DAD72C2A4FCB9D5CBA024E0F692BA8F1E588AD31 | mysql_native_password | localhost | +------------------+-------------------------------------------+-----------------------+-----------+ 4 rows in set (0.00 sec)
In our example, I use the root user and going to update the password. Just use any password in place of NEW_PASSWORD to update the password just run
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'NEW_PASSWORD';
Every time you change the MySQL root database you need to flush existing privileges for this just run
mysql> FLUSH PRIVILEGES;
Finally, MySQL is installed you can exist from MySQL mode just run
Install PHP 7.2
PHP is the last component in LAMP stack development to install. PHP is a very popular high-level programming language. It came with different versions. PHP will connect with MySQL to do dictionary operations (insert, update, delete) on data. To install PHP just runt the following commands
$ sudo apt install php libapache2-mod-php php-mysql
While installing PHP shell will prompt you to continue it will look like, you need to just type Y and press ENTER.
Do you want to continue? [Y/n]
Hopefully, it will install the PHP without any error. You can verify the PHP version by just run
$ php -v
PHP 7.2.24-0ubuntu0.18.04.7 (cli) (built: Oct 7 2020 15:24:25) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.24-0ubuntu0.18.04.7, Copyright (c) 1999-2018, by Zend Technologies
Note:Before modifying a file in Linux you need to keep the backup for backup you can run the following command.
cp / etc/apache2/mods-enabled/dir.conf / etc/apache2/mods-enabled/dir.conf.bakYou can now replace the existing file from backup by running
cp / etc/apache2/mods-enabled/dir.conf.bak / etc/apache2/mods-enabled/dir.conf
After installing the PHP you need to modify the Apache default web server extension. Apache installed with some specific sequence start with HMTL. To update it to PHP as the default page just edit the file
/ etc/apache2/mods-enabled/dir.conf. You can use any editor to modify like
vim in my example, I will use
nano. To modify just run
$ sudo nano / etc/apache2/mods-enabled/dir.conf
The file will look like
<ifmodule mod_dir.c=""> DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm </ifmodule>
Just modify the above file and move the index.php at the start, the output file will look like
<ifmodule mod_dir.c=""> DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm </ifmodule>
After modifying the file just restart apache2
sudo systemctl restart apache2
With this PHP installation, it will install PHP 7.2 by default, but it may your work required more recent work like 7.3 or 7.4. For this specific objective. I will use
ppa a Personal Package Archive to install the PHP 7.3 or 7.4 version. Personal Package Archives (PPAs) are packages archive to install third-party repositories mostly use for pre-release packages.
Install PHP 7.3 on Ubuntu 18.04
To install the PHP 7.3 just run a couple of commands and verify with
$ sudo apt-get install software-properties-common $ sudo add-apt-repository ppa:ondrej/php $ sudo apt-get update $ sudo apt-get install -y php7.3
Install PHP 7.4 on Ubuntu 18.04
For PHP 7.4 run a couple of commands
$ sudo apt-get install software-properties-common $ sudo add-apt-repository ppa:ondrej/php $ sudo apt-get update $ sudo apt-get install -y php7.4
Bonus - Install Composer and Laravel
Composer is a PHP package manager to add or remove packages. To install the composer is very easy just run the below command.
$ sudo apt install composer
Before installing Laravel you need to install Laravel required PHP extensions. You need to consider Laravel 7 requirements here are some requirements. To install these extensions run the following command
$ sudo apt install openssl php-common php-curl php-json php-mbstring php-mysql php-xml php-zip
After installing some extension you need to run the following command with your respect PHP version, I will use 7.3
$ sudo apt install php7.3-common php7.3-bcmath openssl php7.3-json php7.3-mbstring
Finally to install Laravel just run the command
$ composer create-project --prefer-dist laravel/laravel:^7.0 blog