Hosting a Laravel Application on Shared and Own Server.

Laravel is currently the top web framework for PHP. It’s based on Symphony and follows the Model-View-Controller (MVC) architecture. If you are reading this, however, you probably don’t need to be lectured about what Laravel is. You probably have used, are using it and I bet you love it. If you probably worried about how to put your awesome application on the internet. This blog is made for you.

Hosting mainly comes in two tastes: own server or shared hosting. Own server is whereby you have an on-premise server or a cloud server e.g. Amazon Web Services, Google Cloud Platform, Microsoft Azure etc. With an own server, you have control of everything. On the other hand, on a shared hosting you share the server with other users and you have little or no control of the server running your application. An example of shared host is GoDaddy and HostGator. In this tutorial, we will be using Laravel version 5.6 but should work for other versions of Laravel from 5.0

Own Server

For this tutorial we will be using a server running Ubuntu 16.04. It’s worth noting that Laravel 5.6 requires PHP version 7.2 or greater. Our server is using Apache as the webserver; however, Nginx should work as well. By default, in Ubuntu, the public web files are stored in /var/www/html directory. In this tutorial, our server is hosting only one application(website). If you need to host several applications on the same server, you will need to create virtual hosts. To host your application on the server, follow the following steps:

  1. Put all the files in Laravel Public folder to /var/www/html
  2. Create a directory in /var/www and name it your application name i.e /var/www/<my_application>
  3. Move all other files (except public directory) of your application to the directory you created in the previous step
  4. Using a text editor e.g. vim or nano, open the index.php file in /var/www/html
  5. Edit this line require __DIR__.’/../vendor/autoload.php’; to require __DIR__.’/../<my_application>/vendor/autoload.php’; Make to replace my_application with the application name you created in step (b)
  6. Edit line $app = require_once __DIR__.’/../bootstrap/app.php’; to $app = require_once __DIR__.’/../my_application/bootstrap/app.php’; Again, replace my_application to your application name.
  7. Navigate to your application (step b) and run php artisan serve. Visit your IP address or domain address and see your awesome application.

 

Shared Server.

For a shared server, you normally do not have control of the server. However, most hosts like GoDaddy have their servers ready to run Laravel applications. Also make sure the shared host you are using is running PHP >= 7.2 and has composer installed, otherwise Laravel 5.6 will not work. Most shared hosts have public website files in public_html directory. You might want to use an ftp client like FileZilla to transfer files to the server. Follow the below steps to host your application on a shared host:

  1. Fire up your ftp client (e.g. FileZilla) and connect to your shared host. Your server is normally on the right while your local environment is on the left
  2. Navigate to public_html directory on your server.
  3. Transfer all the files in public directory of your local Laravel application to public_html in the server
  4. On the server web root, create a directory and name it the name of your application. This directory should be on the same level with public_html directory.
  5. Upload all the Laravel files and directories except Public to the directory you created in the previous step.
  6. Navigate to public_html and open index.php for editing.
  7. Edit this line: require __DIR__.’/../vendor/autoload.php’; to require __DIR__.’/../<my_application>/vendor/autoload.php’; Make to replace my_application with the application name you created in step (d)
  8. Edit line: $app = require_once __DIR__.’/../bootstrap/app.php’; to $app = require_once __DIR__.’/../my_application/bootstrap/app.php’; Again, replace my_application to your application name.
  9. Your application is now ready for public view.

That’s for now, happy Laravel hosting.