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!

Image for post
Image for post

Step 0 — update your system, Install prerequisites

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!

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:

Image for post
Image for post

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:

Image for post
Image for post

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

http://127.0.0.1

You will see this:

Image for post
Image for post

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

sudo a2enmod rewritesudo systemctl restart apache2
Image for post
Image for post

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.

Image for post
Image for post

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:

Image for post
Image for post

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:

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:

Image for post
Image for post

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
Image for post

Step 7 — Install Fresh Laravel Project on Ubuntu

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:

http://127.0.0.1:8000
Image for post
Image for post

Attention:

Note 0:

  • 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:

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:

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

Note 3:

php artisan key:generate

Note 4:

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:

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:

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:

Written by

Web geek, Self-taught full-stack web developer, Learning Python, Laravel, Vuejs, UX/UI design, Nuclear Physicist PhD

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store