Install and setup Laravel 7 on Ubuntu 16.04 and 18.04

In this tutorial, we will learn how to install and configure Laravel 7 on Ubuntu 16.04 and 18.04 from scratch with example.

Before we start installing Laravel 7, Make sure the LAMP is already installed in your machine. If not You can following the below tutorial to install and configure the LAMP stack.

Install apache2, Mysql and PHP in Ubuntu 16.04 and 18.04

To install Laravel 6 you can follow the below tutorial.

Install Laravel 6 from scratch with example

Install Composer

In this post, we will install Laravel 7 via composer. If composer is not installed in your system. Enter the following command.

sudo apt install composer

The above command will install the composer in your system.

Install Laravel 7

For this tutorial, My installation path is /var/www/html To install Laravel 7 enter the following command. 

cd /var/www/html
composer create-project laravel/laravel blog --prefer-dist "7.*"

The above command will install the fresh Laravel 7 project. Now Before we start working on the Laravel 7 Installed project we will need to setup the correct permission else you will end up with 500 error. Enter the following command to setup correct Laravel 7 Permission

sudo chown -R www-data:www-data /var/www/html/blog/
sudo chmod -R 755 /var/www/html/blog/

Setup Database connection

Next, we will setup the database connection. Before we setup a database connection we will need to create a new database. To do so enter the following command to login to MySQL and create a database.

mysql -u root -p

Enter the password to login. Once you are login enter the following command to create a database.

mysql > create database blog;

Next, Rename the .env.example file to .env . To do so navigate to /var/www/html/blog and enter the following command

cp .env.example .env

The above command will copy .env.example to .env. Enter the following command to generate the Laravel 7 Key

php artisan key:generate

Now open your installed project in your code editor and update the database connection with the following code.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=root

Configure Apache2

Next we will setup the virtual host to run the Laravel 7 project. To do so enter the following command to create an apache2 site configuration file call laravel-blog.conf

sudo nano /etc/apache2/sites-available/laravel-blog.conf

Now copy and paste the below code and save it.

<VirtualHost *:80>   
  ServerAdmin admin@example.com
     DocumentRoot /var/www/html/blog/public
     ServerName laravelblog.local

     <Directory /var/www/html/blog/public>
        Options +FollowSymlinks
        AllowOverride All
        Require all granted
     </Directory>

     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Now save the file and exit.

Enable Laravel virtual host site and Apache2 Rewrite Module

Once the configuration is done. We will then need to enable the newly created site. To do so enter the following command

sudo a2ensite laravel.conf
sudo a2enmod rewrite

Restart Apache2 server

Now all the setup is done, the final step is to restart the apache2 and run the application. To do so enter the below command.

sudo service apache2 restart

Now open your browser and type blog.local you will get the following screen.

Laravel 7 Freshly installed screen