Install and Configure Laravel with LEMP on Ubuntu Linode

Install and Configure Laravel with LEMP on Ubuntu Linode

Laravel is most popular open-source PHP framework that provides some set of tools that help you build a project faster than native. If you have a project and want to deploy / install and wanna select Ubuntu as your Cloud Server with LEMP this tutorial for you.

In this tutorial, you will know how to install ad configure your Laravel project with Ubuntu server. In this Tutorial we will use git repo to install our laravel project to the server so lets build our server to run Laravel App.

Prerequisites

To install laravel on ubuntu and complete this guide you should know what you need to follow to setup your laravel project with Ubuntu server.

  1. A Ubuntu Server size as per your requirement (In this tutorial we using Linode 1GB Plan).
  2. SSH Client (Putty, bash terminal)
  3. Nginx
  4. MySQL
  5. PHP with FPM, Composer and extra extensions
  6. Project Installation
  7. Configure Nginx
  8. Database Setup
  9. Finish Setup

Step 1 – Create Your Linode

First of all we need a Cloud server where we can install our Ubuntu server. Its so easy to do with Linode.

If you are pure beginner with Linode thats ok. Just Follow Me 😂.

  1. First Click on Create Button on top left side and select Linode.
  2. Now Select Choose a Distribution Ubuntu LTS version.
  3. Now Select Region that is nearest to you.
  4. After that choose your Linode Plan for this tutorial is just select Nanode 1GB of 5$ monthly.
  5. Go down and type your linode label anything you love to call.
  6. Now go to Root Password Input and type your fav password that you can remember.
  7. That’s All, Now just click on Create Button available on Right side (in LinodeSummary)
  8. Still Unclear? just check Picture.

Once to complete these setup you will see something new on your dashboard. Yes you think right you will get a Linode on your dashboard section. Thats will look like below picture. If you not get green dot a right side that you need to wait until your Linode Boot Up and Running.

Linode Analytics

Step2 – SSH Client

Now you have a ip in IP address and for access this server via SSH Client you need to just type below command in your ssh client like Bash Terminal. If you want to access with Putty just add host as your IP and port is 22.

Change Primary Domain Document Root in Cpanel

BashCopy
$ ssh [email protected]

After that you need to type yes and enter than enter your root password that we added while create our Linode. After that you can access and try to follow next step.

Step 3 – NGINX Installation Process

Now We are ready to install our nginx server. BTW its too easy to install and config nginx server. If you new with installation that not need to rocket science explanation. Just Keep follow me.

BashCopy
$ sudo apt update
$ sudo apt install nginx
$ sudo ufw allow "Nginx Full"

Just these 3 commands to install nginx to your ubuntu server. After some more command we will config our Nginx web server as per our requirement. If there any asking just Type Y and press enter.

After Finish Installation you can open your IP in Browser and you will see Welcome To Nginx Message.

Step 4 – MySQL Installation Process

Now its time to install MySQL server and perform some more operation to secure our server.

First of all we install our MySQL server so we need to type a command that written below. and if any question prompted just type Y and press enter to finish installation.

BashCopy
$ sudo apt install mysql-server

After Successfully installation just try to secure your mysql server by typing the command below and it will prompt too many question and just Nod Head 😂. I meant Type Y and Press Enter 😂. For more information check Picture below.

At One Point there you need to pass your MySql password so choose wisely

Our MySql Server setup complete. After a while we will create our database and a new user in Database but before it we should complete our installation process of PHP.

Step 5 – PHP Installation Process

For Installation of php its so easy you can install php-fpm, php-mysql, php-xml and composer + zip and unzip.

BashCopy
$ sudo apt install php-fpm php-mysql php-xml composer
$ sudo apt install zip unzip

you can run both command in one line but i perfer you should use two command to install php and zip unzip.

After you run command your php will installed successfully. To confirm you can use below command to check php version

BashCopy
$ php -v

Step 6 – Project Installation

To clone project from git is easy here. You just run below command and your project will install. Just Keep the command order and run one by one.

BashCopy
# clone project from github or other repo provider and keep dot at last
$ git clone project-repo-url.git .
 
# install all project packages via composer

$ composer install


# change owner 
$ chown -R www-data:root /var/www/html

# change mod
$ chmod 755 /var/www/html/storage

after these command you need to Configure Nginx so we will use vim editor for edit config file.

Step 7 – Nginx Configure

After installation you need to configure your nginx config default file as per laravel project requirement. Follow the below command. In these command we use vim editor.

In VIM editor we should press i to insert new data or to change data and to save that edited file press esc button first than press :wq and then press enter.

BashCopy
$ vim /etc/nginx/sites-available/default

this will open your file than change your path, server_name and some more config that is same as below file.

Plain TextCopy
server {
	listen 80 default_server;
	listen [::]:80 default_server;
	
        root /var/www/html/public;
	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html index.php;
	server_name domainname.com;

	location / {
		try_files $uri $uri/ /index.php$query_string;
	}
	# pass PHP scripts to FastCGI server
	
	location ~ \.php$ {
		include snippets/fastcgi-php.conf;
		fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
	}
}

now you need to check your syntax is OK or not. And you need to restart your nginx if your syntax is OK.

BashCopy
$ sudo nginx -t
$ sudo systemctl restart nginx

Step 8 – Database Setup

Now all is ready we just need to setup our database and use the credential in our .env file of Laravel Project.

Now You need to access your mysql server via command and need to add a database and a user so try below command.

Plain TextCopy
$ mysql -u root -p

# after enter password your sql server will activated and now you need some query to run.

CREATE DATABASE db_name;

CREATE USER 'user_name'@'localhost' IDENTIFIED BY '[email protected]';

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

FLUSH PRIVILEGES;

exit;

# after that you need to restart your mysql
$ sudo service mysql restart

now we have a database a username and a password so we can finish our guide with adding in .env file.

Step 9 – Finish Setup

At this time we cloned our project and by default we don’t have a .env file so lets create that one. By Command you can do it easily.

BashCopy
$ cd /var/www/html

#now we are in our html folder so we need some command to copy .env.example file to .env

$ cp .env.example .env

# now we will edit our .env file and update some content

$ vim .env

# to edit just press i buttton from keyboard

#.env file
APP_NAME=ProjectName
APP_ENV=development
APP_KEY="WE WILL GENERATE IT"
APP_DEBUG=false
APP_URL=http://domainname.com
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_name # created in step 8
DB_USERNAME=user_name # created in step 8
DB_PASSWORD=password # created in step 8

# to save from vim just press esc button then :wq and press enter

# now generate  APP_KEY

$ php artisan key:generate

We finished install of laravel on ubuntu.

Conclusion

Thats all. Its too easy to install and configure laravel project on ubuntu server from github repo. We will soon publish a new article where we can install a laravel project from zip file and custom sql file. Hope this tutorial save your day 😃.

Leave a Comment