How to deploy a laravel 8 app on VPS Ubuntu 20.04 - with php 7.4 / mysql / apache - via git

Lets see how to install laravel 8 on a fresh Ubuntu 20.04 TLS  with stack: php 7.4 , mysql / apache.Keep in mind we gonna use git for the deploy, and we wont install Laravel from composer command ( eg: laravel new :name) . I will be using bitbucket, instead of github, but there is no difference in the way we going to install it, i just prefer bitbucket, because of the free private repositories.

 

So lets assume you have a fresh new VPS installed / reinstalled with Ubuntu 20.04. Lets login with Putty via port 22 to the server host with root username and our password

 

Lets update / upgrade first

sudo apt update && sudo apt upgrade

normally , we going to see a confirmation in the terminal if we want to proceed, click yes

 

Now lets install Apache

sudo apt install apache2



After its done, type

systemctl status apache2

You should see something like this, apache is active and running. So far so good.

You can leave the active terminal by typing ctrl+x.

 

Install PHP 7.4

 

Since laravel requires php 7.2 >= , we going to install php 7.4, with some of the libs that are required. Type in the terminal:

sudo apt install libapache2-mod-php php php-common php-xml php-gd php-opcache php-mbstring php-tokenizer php-json php-bcmath php-zip unzip php-mysql

This is what usually you going to see, for a fresh OS , type y and enter:

 

Lets check if its installed or if its the active current version.

php -v

You should see :

php -v

Install Composer

 

Composer is the packet manager Laravel is using , so we need it also, type:

 

curl -sS https://getcomposer.org/installer | php

And

sudo mv composer.phar /usr/local/bin/composer

 

Installing MYSQL

 

sudo apt install mysql-server

 

You should see something like this:

 

type y and enter

 

After its done, type :

sudo mysql_secure_installation

And follow the steps, i suggest you to use validate password, and not allowing to connect remotely.

 

Create database for your project

 

Try to login into the mysql by typing:

sudo mysql -u root -p

You`ll be asked for a password that you created during the mysql_secure_installation process

create database laravelname;

instead of laravelname, use something similar to your domain or your projectname.

You should get :

Query OK, 1 row affected (0.00 sec)

 

Lets create a user now

CREATE USER 'laraveluser'@'localhost' IDENTIFIED BY 'yourpassword';

And grant permissions on that user. You might want to grant permissions on 1 user per table, or you can create superuser to deal with all your installations, its up to you, lets assume you need a super user, that will handle multiple tables.

GRANT ALL PRIVILEGES ON * . * TO 'youruser'@'localhost';

And:

FLUSH PRIVILEGES;

 

So now we have a databaase and username to handle the MYSQL, type exit to leave the mysql.

 

Configurating Apache with Laravel and add a domain

 

cd /etc/apache2/sites-available
ls

You should see something like this:

 

Lets add the domain name  with some config Laravel needs, type:

nano yourdomainname.conf

 

And paste the following

<VirtualHost *:80>   
  ServerAdmin admin@example.com
     DocumentRoot /var/www/yourproject/public
     ServerName yourdomain.com
     ServerAlias www.yourdomain.com

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

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

At DocumentRoot , use your path to our laravel app, same for the directory, and for ServerName - type your domain ( eg: mydomain.com )

After that, leave and save the document by ctrl+x, will ask you are you sure to save it, click y and enter to leave nano ( its a nice and simple editor used by ubuntu)

 

Now type:

sudo a2ensite yourdomain.conf

And

 systemctl reload apache2

 

Deploying Laravel into the VPS

 

go to /var/www , by typing:

cd /var/www

Since we are getting the project from git, make sure the git project name actually matches the name path we used in the .conf file.

 

git clone yourgiturl.git

 

And cd yourgitname

type ls  to make sure you are in the laravel app project directory, and now lets install its dependencies by:

composer install

If it asks do you want to run it as a root, click yes.

 

php artisan key:generate

 

Since i use private repos, and i remove .env file from .gitignore, i always have the .enf file, in case you are not, you need to create  .env file, by typing:

nano .env

And copy paste this into it

APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack
LOG_LEVEL=debug

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

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

click ctrl+x , then yes to close nano and save it. Keep in mind thats just the example .env file and you need to fill some details - your mysql user and password, and other stuff if you need them.

Or if you have .env.example , just type

mv .env.example .env


 

After that, retype php artisan key:generate for your app key

 

Open the domain name url and you should see an error

 

UnexpectedValueException
The stream or file "/var/www/yourproject/storage/logs/laravel.log" could not be opened in append mode: failed to open stream: Permission denied 

 

Thats fine! We didnt setup our permissions yet.

chown -R www-data.www-data /var/www/yourappfoldername
chmod -R 755 /var/www/yourappfoldername
chmod -R 777 /var/www/yourappfoldername/storage

You should see the laravel screen app at the domain URL

 

You can also type in the terminal

php artisan migrate

To make sure php-mysql is installed, and that the mysql details from the .env file matches the real db name / user / password. If you get error for missing driver, just type: "sudo apt-get install php-mysql" and run the migration again.