Install Laravel on Ubuntu 18.04 with Apache, MySQL, PHP7 (LAMP) stack

How to install Laravel, phpmyadmin, php 7.2 and LAMP on Ubuntu less than 10 minute!

Panjeh
8 min readJun 23, 2019

Step 0 — update your system, Install prerequisites

You should first update your system by running :

sudo apt update

Before anything you should install git, curl, wget, … in a fresh ubuntu 18.04:

sudo apt install -y git curl wget zip unzip

I put -y flag in the above command to answer all the question “yes” by default!

Before you continue with the rest of this tutorial, I would like to introduce two packages for Laravel that I have recently developed: Laravel Pay Pocket, a modern multi-wallet package, and Laravel Failed Jobs, a UI for the Laravel Failed Jobs Table. I hope they may be of help to you.

https://github.com/HPWebdeveloper/laravel-pay-pocket
https://github.com/HPWebdeveloper/laravel-failed-jobs

Step 1 — Install Apache:

sudo apt install apache2

To make sure that the server is running check the apache2 status:

sudo systemctl status apache2

You will see this:

and then press “q” to quit this session.

Step 2 — Adjust the Firewall to Allow Web Traffic

sudo ufw allow in "Apache Full"

The response will be:

Then check this address in the browser : http://your_server_ip . In my case:

http://127.0.0.1

You will see this:

As you see above, the service appears to have started successfully, you can also access to your server through the http://localhost address and you will see the same Apache2 default home page.

Attention: Enabling mod_rewrite

We need to activate mod_rewrite. It's available but not enabled with a clean Apache 2 installation.

sudo a2enmod rewritesudo systemctl restart apache2

Read Note 0 at the end.

Step 3— Install MySQL:

sudo apt install mysql-server

Optional:

Then remove some dangerous defaults after installing mysql:

sudo mysql_secure_installation

Think about a strong password for user root in Mysql ( in level 1=Medium, it must have sing, digit, lowercase and uppercase letters). Don’t forget to save it.

Type your selected root password when it asks. Read more about improving MySQL installation security here.

And then answer the other questions arise with Yes!

  • Remove anonymous users? y
  • Disallow root login remotely? y
  • Remove test database and access to it? y
  • Reload privilege tables now? y

Then you should try to connect mysql with root password

sudo mysql -u root -p

and when you see mysql> you can type exit to quit from mysql environment.

Step 4— Install PHP:

sudo apt install php libapache2-mod-php php-mysql

For Laravel installation and also phpmyadmin you will need some important php modules, so do this:

sudo apt install php7.2-common php7.2-cli php7.2-gd php7.2-mysql php7.2-curl php7.2-intl php7.2-mbstring php7.2-bcmath php7.2-imap php7.2-xml php7.2-zip

Why these modules? Read more here.

Step 5— Tell the web server to prefer PHP files over others, so make Apache look for an index.php file first.

sudo nano /etc/apache2/mods-enabled/dir.conf

Before editing:

Then edit the dir.conf file in a way that index.php has the priority over the others, as like as:

<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

Then Ctrl+x and answer yes to override the file. Then

sudo systemctl restart apache2

Test:

Check the correctness of installation by:

sudo nano /var/www/html/info.php

And put this:

<?php
phpinfo();
?>

Then check in the browser this: http://your_server_ip/info.php

In my case is :

http://127.0.0.1/info.php

I will see this:

Do not forget to delete info.php file!

Step 6— Install composer on Ubuntu

curl -sS https://getcomposer.org/installer | phpsudo mv composer.phar /usr/local/bin/composer# this make the composer executable -> sudo chmod +x /usr/local/bin/composer# check versioncomposer --version

Step 7 — Install Fresh Laravel Project on Ubuntu

Change your directory to the place you want like:

cd ~composer create-project --prefer-dist laravel/laravel my_linux_app

Then go to the project directory :

cd my_linux_app

and then start the project:

php artisan serve

Read Note 5 at bellow.

Step 8 — Verify Laravel Installation:

I will see the project in this address:

http://127.0.0.1:8000

Before continue reading the reset of this tutorial, I want to introduce Mastering PhpStorm course which is provided by my friend Christoph Rumpel. I believe this course will save a lot of golden time for you! and make you be a professional in PhpStorm in a short period of time. Click here to see more details.

https://masteringphpstorm.com/

Attention:

This installation guide is suggested for local environment. For production you should consider more security cases!

Note 0:

To config Appche2 you need to know these commands:

  • a2enmod : (apache2 enable mode) — enable an Apache2 mod.
  • a2dismod : (apache2 disable mode) — disable an Apache2 mod.
  • a2enconf : (apache2 enable Config) — enable a specific config.
  • a2disconf : (apache2 disable config) — disable a specific config.
  • a2ensite : (apache2 enable Site) — enable a specific app.
  • a2dissite : (apache2 disable Site) — disable a specific app.

example:

sudo a2enmod rewrite

Read more about Apache config and Linux in this article.

Note 1:

You can also clone the Laravel from github repository:

git clone https://github.com/laravel/laravel.git

So you should change your directory to the project you clone and then don’t forget to do the following command to install all dependencies required for Laravel framework.

sudo composer install

Note 2:

Your project and also its storage directory should be accessible. So do this:

chmod -R 755 ~/my_linux_app
chmod -R 777 ~/my_linux_app/storage

Note 3:

For each fresh installation don’t forget to generate base64 random number encryption key.

php artisan key:generate

Note 4:

To create database for Laravel project first:

sudo mysql -u root -p

It will ask you first the sudo password and then the root mysql password. Then do this:

mysql> CREATE DATABASE laravel;mysql> GRANT ALL ON laravel.* to 'laravel'@'localhost' IDENTIFIED BY 'secret';mysql> FLUSH PRIVILEGES;mysql> quit

Then in the project directory do this:

mv .env.example .env

and edit the .env file as follow:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=secret

don’t forget to do:

php artisan config:cache

Then

for old versions of Laravel do:

php artisan make:auth
php artisan migrate

For Laravel 6 read:

and also:

and

Note 5:

In local environment I put the project files in the /home ~ directory. and with the command php artisan serve I launch the website. While in production environment it is important to know that all your web content must be under the /var/www/html directory.

You can create a Laravel application under Apache2 root directory.

cd /var/www/html
composer create-project --prefer-dist laravel/laravel my_linux_app

Open the browser and access to laravel app by :

http://localhost/my_linux_app/public

Note 6:

For installation phpmyadmin you need to do:

sudo apt install phpmyadmin php-gettext

and then restart apache2

sudo systemctl restart apache2

phpmyadmin with php 7 in Ubuntu 18.04 has some issues that I fixed and explained them in this article.

Also, for windows environment I have found Laragon.

Thank you for reading! If you enjoyed this article:

Clap it ! Share it! Follow Me in Medium!

Also I’d like to hear your opinion on this article. If you have any doubt, question or suggestion please leave a comment below.

Have a very wonderful day!

Previous Tips You will Love:

--

--