Host secured laravel 5.x appliaction on ubuntu 16.04 vps with apache

Aim 



The aim of this tutorial is to show how to migrate the local laravel 5.x project to the Ubuntu 16.04 vps. We will see how to setup the vps for hosting from scratch. We also see the ways you can copy your laravel project to the vps and at last we will see how to secure your project with ssl using the lets encrypt. we will also add a cron job to auto update the ssl certificate. 

Prerequisites 

To follow this guide along you will require this 
  • Ubuntu vps with 16.04 laravel 
  • project you want to host
  • ssh client
    • For windows you can use putty
    • For Mac you can use terminal
  • sftp client
    • For windows you can use winscp
    • For Mac you can use cyberduck
  • Git
  • Remote Git repository ( optional )

Steps

Now after gathering the required elements you can follow these steps to host your laravel 5.x project.
  • Prepare the vps for the web hosting
    • Install Php
    • Install Mysql 
    • Install Apache
    • Install Git
  • Copy and setup the local laravel project on vps
    • Copying the project
      • Using sftp
      • Using git
    • Install the dependencies
    • Setup the project and run migrations
  • Setup the virtual host for your laravel project
  • Setup lets encrypt to secure your project with ssl




Prepare the vps for the web hosting

After you have created a brand new Ubunut 16.04 vps first step for us is to update the Ubuntu itself. We will use apt to update the ubuntu and preinstalled packages.


$ sudo apt-get update


It is essential to update the packages because it fixes issues with the software that has released by the package vendors.
Now we are ready to setup the vps for hosting.

1. Install Php1. Install Php 


$ sudo apt-get install php


You will get php 7.0 because now default php version for ubuntu 16.4 is php 7.0After installation you can check if php installed successfully or not and which version is installed by running $  php –vNext is to install the php dependncies required by the larave


$ apt install php-mcrypt php-gd php-mbstring 



2. Install Mysql

 $ sudo apt-get install mysql-server 


After installation to check if your mysql is running or not ( it should be running automaticlly after installation) you should run this command $ systemctl status mysql.serviceIt will display status of mysql.service 
If you mysql dose not start automatically than run this command
$ sudo service mysql start

3. Install Apache


 $ sudo apt-get install apache2 


This will install apache2 to your vps.You can verify the installation of apache on your vps. Just goto web browser and visit the url http://<your-vps-ip-address>It will show the default apache2 page.

4. Install Git

Install git on your vps


 $ sudo apt-get install git


Run command  $ git --help To check if git is installed properly or not.

Copy the local laravel project to vpsCopy the local laravel project to vps


Copying the project

You can copy your local laravel project to the vps in 2 ways 

  • Using the sftp 
  • Using the Git

Using the sftpIn this way you have to copy the project files from the local machine to vps using the sftp client. for exmple on windows you can use the winscp. They are drag and drop clients. 

If you are using sftp then follow this steps and skip the setp to in this section.

  • Open your sftp client 
  • Copy your local project folder to the /var/www/ directory 

That's it you have done

Using the gitYou can alos use git to copy ( sync ) your local prject on the vps. for this you should have a remote git repository. you can directly clone the project from your remote repository to your vps using git commands and than after you can run the pull commands on the vps to pull the changes from the remote repository push from the local machine.  
clone your project from the remote repository

$ git clone http://<url-to-your-remote-repository.git>


Now in future if you do any updation in your project. You just have to push it to your remote repository from local machine and have to pull it your remote repository to reflect the changes.


2. Install the dependencies

For this first you have to connect  to the vps using the ssh client like putty Laravel depends on the composer to pull the packages from packegist. So first step is to install the composer on the vps


$ curl -sS https://getcomposer.org/installer |
php -- --install-dir=/usr/local/bin --filename=composer


this will install the composer to your bin directory. So you can run composer command from anywhere in your termianl. Now go to /var/www/<your-project-folder>


 $ cd /var/www/<your-project-folder>


Run ls command to list out the files. /var/www/<your-project-folder> $  ls -l
your will get the list of files and folder. In those files one will be composer.json file. It is the file that contains the list of packages on which your laravel project depends. run composer install the pull the dependcies  /var/www/<your-project-folder

 $ composer install


3. Setup the project and run migrations


After running compose install its time to setup the .env file of laravel projetmake copy of .env.example to .env




 /var/www/<your-project-folder > $ cp .env.exmple .env


Create a key for your laravel project /var/www/<your-project-folder> $  php artisan key:generateOpen the .env file in the nano or vim and update the database credentials to connect your project to mysql database, /var/www/<your-project-folder> $  nano .envAfter connecting the database run the migration to setup the tables in database

/var/www<your-project-folder> $ php artisan migrate --seed 



You have change folder permission for certain folders in order to make laravel running

Change  group of the storage folder with command :


/var/www/<your-project-folder> $ chgrp -R storage www-data






Change  owner of the storage folder with command : 

/var/www/<your-project-folder> $ chown storage www-data






Change folder mode with

/var/www/<your-project-folde> $ chmod 755 storage



Setup the virtual host for your laravel project Setup the virtual host for your laravel project

You might have purchase your domain name project. To run your laravel project on that domain name you have to create a virtual host. You should follow this step to create virtual host for your laravel project on the apache server. Make a copy of default.conf file 


$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/<site-name>.conf


Open the .conf file in nano or vim and update it like this

<VirtualHost *:80>
ServerAdmin admin@<your-domain>.com
ServerName <your-domain>.com
// ServerAlias the alternate name for site like www.<your-domain>.com
//and <your-domain>.com. points to same page
// If you not add <your-domain> as alias you will not be able to access site with // www subdomain
// It will only displayed with url http://<your-domain>.com
ServerAlias www.<your-domain>.com
DocumentRoot /var/www/<your-project-folder>/public
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>



After Creating the conf Virtual host file you have to enable.

$ sudo a2ensite <site-name>.conf



By running upper command you are enable the virtual host defined in file sit1.conf By running upper command you are enable the virtual host defined in file sit1.conf  a2ensite = apache + enable  + siteNext step is edit your host file. Host file can be found at /etc/hosts 127.0.0.1 localhost 149.142.xxx.xxx  <your-domain> 

Replace 149.142.xxx.xxx with your vps ip addressThe last step is check you site in browser


Setup lets encrypt to secure your project with ssl

As we have installed git in previous stesp we can use git to pull the letsencrypt to our vps

$ git clone https://github.com/letsencrypt/letsencrypt /opt/letsencrypt


Now we have to generate the ssl certificate for our domain using the letsencrypt

$ ./letsencrypt-auto --apache -d <your-domain>.com  -d www.<your-domain>.com


we created a certifacte that work our domain as well as www subdomain of our domain. we created a certifacte that work our domain as well as www subdomain of our domain. In letsencrypt certifcate have expiry date of 90 days. So after 90 days certifacte will expiry. Here we have to ways , first we can update those certifacte every 90 days manually or we can create a cron job for itTo make the cron job for updating the certificate update the cronjob file

$ crontab –e


This will open the crontab file of the user in vim , append this line to that file  and close the cronjob file.

15 5 * * 5 /opt/letsencrypt/letsencrypt-auto renew >> /var/log/le-renew.logsave


Conclusion

In this tutorial we see how to setup the ubuntu vps from scratch to make it ready for hosting the laravel project. We also see the way to migrate the laravel project from the local machine to the ubunut vps on its own domain name. We also see how to setup and use lets encrypt to make your site secured with autoupdating ssl certifactes. For more details about letsnecrypt visit the site letsencrypt.org

Comments

Popular Posts