A Guide to Setting up Laravel in Ubuntu

Written by Alex Golub | Published 2020/09/27
Tech Story Tags: laravel | ubuntu | coding | bunnyshell | programming | cloud | cloud-computing | coding-skills

TLDR A Guide to Setting up Laravel in Ubuntu will show you how to install and run Laravel on an Ubuntu 18.04 LTS based Apache server. This tutorial works for Ubuntu 17.x as well as the current Ubuntu LTS release. We will assume that you have a basic server based on Ubuntu running. The next step is to install PHP along with several extra packages that would prove useful if you are going to work with Laravel. We also need Git version control to be installed. If you have it installed, you can skip the following step.via the TL;DR App

Laravel is a very popular open-source PHP framework aimed at easy development of applications. If you are looking for a new PHP framework, you should give Laravel a try.

Summary

The following steps will show you how to install and run Laravel on an Ubuntu 18.04 LTS based Apache server. This tutorial works for Ubuntu 17.x as well. But for servers, you should prefer to use an Ubuntu LTS release like the current Ubuntu 18.04 LTS. Learn how to set up Laravel in Ubuntu. 
After showing you how to set up this PHP framework in Ubuntu, we’ll be showing you what it takes to set up your Laravel Application using the Bunnyshell Platform.

How to set up Laravel in Ubuntu

Pre-Requisites
We will assume that you have a basic server based on Ubuntu running.
Before proceeding with the installation, it's always a good idea to make sure your sources and existing software are updated.
<i><span style="font-weight: 400;"># sudo apt-get update&nbsp;</span></i>

<i><span style="font-weight: 400;"># sudo apt-get upgrade</span></i>
Before Laravel, we need to install other components that are essential.

Installing Apache and PHP 7.2

Next step is to install PHP along with several extra packages that would prove useful if you are going to work with Laravel.
<i><span style="font-weight: 400;"># sudo add-apt-repository ppa:ondrej/php</span></i>

<i><span style="font-weight: 400;"># sudo apt-get update</span></i>

<i><span style="font-weight: 400;"># sudo apt-get install apache2 libapache2-mod-php7.2 php7.2 php7.2-xml php7.2-gd</span></i>

<i><span style="font-weight: 400;"># php7.2-opcache php7.2-mbstring</span></i>
Even though Ubuntu's own repository has PHP, it's better to add a 3rd party repository here because it gets more frequently updated. You can skip that step and stick to Ubuntu's version if that's what you prefer.

Installing Laravel

Before we finally delve into it, we also need Git version control to be installed. If you have it installed, you can skip the following step.
Download and install Git for Linux:
<i><span style="font-weight: 400;"># sudo apt-get install git</span></i>
Once the installation has successfully completed, the next thing to do is to set up the configuration details of the GitHub user.
<i><span style="font-weight: 400;"># git config --global user.name "user_name"</span></i>

<i><span style="font-weight: 400;"># git config --global user.email "email_id"</span></i>
To install Laravel, we need to install Composer first. It is a tool for dependency management in PHP that allows you to package all the required libraries associated with a package as one. To install Laravel and all its dependencies, Composer is required. 
<i><span style="font-weight: 400;"># cd /tmp</span></i>

<i><span style="font-weight: 400;"># curl -sS https://getcomposer.org/installer | php</span></i>

<i><span style="font-weight: 400;"># sudo mv composer.phar /usr/local/bin/composer</span></i>
The curl command downloads composer.phar package to your /tmp directory. But we would want composer to run globally hence we need to move it to /usr/local/bin/ directory under the name 'composer'. Now we can run composer from anywhere.
To install Laravel, move to the public html directory on your system. Since we are on Ubuntu and using Apache, we will install it in the /var/www/html directory.
<i><span style="font-weight: 400;"># cd /var/www/html</span></i>

<i><span style="font-weight: 400;"># sudo composer create-project laravel/laravel your-project --prefer-dist</span></i>
The above command will create a "your-project" directory with Laravel installation in it. Composer uses Git to download and install all the packages and modules that Laravel requires for functioning.
Configuring Apache
Now that we have installed Laravel, we move onto the step of configuring Apache webserver.
Give proper permissions to the project directory:
<i><span style="font-weight: 400;"># sudo chgrp -R www-data /var/www/html/your-project</span></i>

<i><span style="font-weight: 400;"># sudo chmod -R 775 /var/www/html/your-project/storage</span></i>
Go to the /etc/apache2/sites-available directory and use the following command to create a configuration file for our Laravel install:
<i><span style="font-weight: 400;"># cd /etc/apache2/sites-available</span></i>

<i><span style="font-weight: 400;"># sudo nano laravel.conf</span></i>
Add the following content to the file and close it after saving. Replace yourdomain.tld with the domain name of your website inside the file:
<i><span style="font-weight: 400;">&lt;VirtualHost *:80&gt;</span></i>

<i><span style="font-weight: 400;">&nbsp;&nbsp;&nbsp;&nbsp;ServerName yourdomain.tld</span></i>

<i><span style="font-weight: 400;">&nbsp;&nbsp;&nbsp;&nbsp;ServerAdmin webmaster@localhost</span></i>

<i><span style="font-weight: 400;">&nbsp;&nbsp;&nbsp;&nbsp;DocumentRoot /var/www/html/your-project/public</span></i>

<i><span style="font-weight: 400;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;Directory /var/www/html/your-project&gt;</span></i>

<i><span style="font-weight: 400;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AllowOverride All</span></i>

<i><span style="font-weight: 400;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;/Directory&gt;</span></i>

<i><span style="font-weight: 400;">&nbsp;&nbsp;&nbsp;&nbsp;ErrorLog ${APACHE_LOG_DIR}/error.log</span></i>

<i><span style="font-weight: 400;">&nbsp;&nbsp;&nbsp;&nbsp;CustomLog ${APACHE_LOG_DIR}/access.log combined</span></i>

<i><span style="font-weight: 400;">&lt;/VirtualHost&gt;</span></i>
We have to enable this newly created .conf file and disable the default .conf file that is installed with the default Apache install. Also, we need to enable mod_rewrite so that permalinks can function properly:
<i><span style="font-weight: 400;"># sudo a2dissite 000-default.conf</span></i>

<i><span style="font-weight: 400;"># sudo a2ensite laravel.conf</span></i>

<i><span style="font-weight: 400;"># sudo a2enmod rewrite</span></i>

<i><span style="font-weight: 400;"># sudo service apache2 restart</span></i>
Your Laravel installation is now complete. Visit the IP address or domain name of your server with a web browser.

Setting up Laravel using Bunnyshell

  1. Go to the application section and select the Frameworks tab
  2. Select Laravel
  3. Select an existing virtual machine or leave it blank to create a new one
  4. Select your desired cloud provider and your machine location and size
  5. Give your application a name and create it
These are the steps required to get a virtual machine that is set up to run your Laravel application without actually having to go through all the steps detailed above.
After the virtual machine is created, you’re all set to start deploying your application, and no, you won’t need to use an FTP client to upload your files manually, all you have to do is go to Applications and open the application you just created, select Deployments and click enable deployment.
This will allow you to configure a repository to use as a codebase and allow you to select the branch you want to have deployed. Needless to say, this can be helpful for configuring staging and production environments based on repository branches.
From here you can also manage your backups and restores for the application, you can simply create a backup or restore your application with a few simple clicks, no more logging into the server and running commands.
Conclusion
As you can see, in order to set up Laravel in Ubuntu, you need to complete a lot of steps. However, if you choose to set up Laravel in the cloud with Bunnyshell, it’s a simpler process with lots of automation that makes your life easier and helps take your mind off the DevOps side of development.
Also published on: https://www.bunnyshell.com/blog/how-to-set-up-laravel-in-ubuntu/

Published by HackerNoon on 2020/09/27