Developer

[Laravel] Deploying to production. How we do it at icrewsystems!

admin Published on June 28, 2021

Deploying software is always considered as a head ache, because developers like the phrase "it works well on my machine" way too much. The reason for that phrase is, the developer machines are well configured with all the required tools, tweaks and quirks.

"It works on my machine"

But in all fairness, deployment is a phase that eventually comes while building any software. For novice developers, it might be a nightmare of a thought to deploy their code to the world-wide-web, but it does not have to be. In this blog, I'll show you a glance at how we push software to production and what are the precautions we take to make sure everything is under control.

Step 0: The "Production Branch"
I'm going to assume you already know what version control systems are and how they work. For deploying, it's always recommended that you checkout the latest, tested code to a branch called "production". At icrew, only the Lead Engineers are authorized to merge / commit into the production branch. By that convention, we can ensure that the code that's being added onto the production branch, is reviewed by an experienced developer.

Step 1: Preparing the server

You need to make sure you have a few things sorted out before you begin the deployment. They are

  1. Database - Create a database, user & password and store the credentials on your computer, which will be required when you're installing your application. (How to create a database in cPanel)

  2. Emails - Create an email address called "no-reply@YOURDOMAIN.com". This will serve as your email ID for all outgoing emails from your Laravel application. Once you have created, copy the (How to setup E-mails in cPanel).

  3. HTTPS / SSL - It's very important that you have an SSL certificate installed on your domain. This makes sure that the interactions between your application, users & the server is encrypted. Certain hosting providers provide Life time FREE SSL (Self Signed) for all plans, like GemHosting, UK.

  4. Subdomains? - If your application makes use of subdomains, you should create them too and point it to the public_html folder (where our application will live).

For Laravel applications, you need to make sure you have SSH access into your server. If you use shared hosting, most of the providers don't enable it by default for you, therefore you have to specifically request it to them.

If you're unfamiliar with how to create an SSH Keypair, go to this article

Step 2: "The actual deploying"

Now that you have the server fully configured and ready to accomodate our application, we can go ahead and clone the code onto the serve, for which we'll use the following commands

$ cd public_html
$ git clone REPOSITORY_URL -b BRANCH_NAME .

Example:

$ git clone https://github.com/icrewsystemsofficial/covid19resources.git -b production .

The "." will make sure the application is deployed onto the public_html folder. For this to work, you need to also make sure that the public_html folder is empty.


(From this screenshot, you can see that the code is cloned into the public_html folder)

Now, we have the application cloned, we need to install the dependencies by running
 
$ composer install
$ npm install && npm run prod

You might as well grab a coffee while it takes to compile.

Once that's done, you should be running a few more commands

$ cp .env.example .env (this copies the .env.example file into a new file called .env)
$ php artisan key:generate
$ nano .env (this opens the nano text editor on your terminal)
 
Now, paste the database & mailing credentials which we created and stored in Step 1 in their respective places, and then set your application mode to "production", by editing the APP_ENV=production and APP_DEBUG=false. This will make sure your application does not reveal any confidential information on the live server.



And Voila! Your application is live.

Here's a secret tip!

I use Terminus SSH app on my phone. This is a lifesaver when you're on the go. The app allows me to SSH into my servers anytime I want. The best part is, it's synced to my computer, so I don't need to do any additional setup on my computer.

Credits: Terminus website


It's a paid subscription with a trial period, I was very skeptical in the beginning, but now I don't see a life without the app. https://termius.com

admin Published on 2021-06-28 01:13:23