DEV Community

Imo-owo Nabuk
Imo-owo Nabuk

Posted on

Simple steps to Deploy Laravel 5/6/7/8 Website on Shared Hosting

Deploying on cPanel seems tricky because you cannot run `php artisan serve` command there and the index.php file resides in the public folder. If you deploy as is, you will have to visit your site as `domain.tld/public` and that's not workable for SEO and convenience.

Using `git` and `composer`, you can easily deploy your site and manage further updates.

Laravel is best hosted on a VPS or dedicated server but you may not want to pay more for hosting a small project and choose to settle for shared hosting.

Step 1 - Visit https://yourdomain.com/cpanel

  • Login with your cPanel credentials.

    Find the terminal icon or search for it and click it.

    If the Terminal Icon does not exist, it means your shared account doesn't have "Shell Access" turned on. Ask your Hosting Provider to turn it on for you.

    It could also mean that your cPanel version doesn't have the terminal option. Download and use an SSH client but you'll have to enable it on the server. Contact your provider on how to use SSH client.

    It will launch the terminal. This is where you will do most of the work.

    • Check PHP version and ensure it is >= 7.1.3
    • Check that composer is installed on the server by running the composer command
    
      composer -V
      // Composer version 1.7.2 2018-08-16 16:57:12
    
    

    Most cPanel comes with Git pre-installed, if it is not, you might need to ask your hosting provider to have it installed.

    Step 2 - Set up the app

    • Clone the repo to a folder named "project", it should be in /home/username/project
    
      git clone git@github.com:username/repo.git project
    
    • Go to the project folder and install the dependencies
    
     cd project
     composer install
    
    • Create a new MySQL database in cPanel in DATABASES -> MySQL Databases section. take note of the database name, username and password.
    • copy the .env.example file to .env
    
     cp .env.example .env
    
    
    • edit the .env file and configure it with your setting
     nano .env 

    few key settings that is important are

    
     DB_HOST=localhost
     DB_CONNECTION=mysql
     DB_PORT=3306
     DB_DATABASE=dbname
     DB_USERNAME=dbuser
     DB_PASSWORD=dbuserpass
     APP_ENV=production
     APP_DEBUG=false
    
    • Generate the application key
     php artisan key:generate 
    • Run the migrations
     php artisan migrate 

    If you received an error of "Specified key was too long; maxkey length is 1000 bytes", you need to check this article for the fix. Apply the fix in the repo, then issue "git pull origin master" so that this copy will receive the fix . Then go to PhpMyAdmin, drop all the tables there and try running the migrate command again.

    • Set the permission of the storage folder so that the web server can write to it
     chmod -R 775 storage 
    • Optimize things up!
     
     php composer dump-autoload
     php artisan config:cache
     php artisan route:cache
    

    Step 2 - Make the app accessible to public

    right now, the app is not accessible outside because we placed it under /home/user/app directory. The only folder that is accessible to public is /home/user/public_html but we don't want to place all of our framework files into public_html folder. So what we gonna do is simply make a Symbolic link to our /home/user/app/public folder.

    to do this, backup first a copy of the public_html folder

     
     mv public_html public_html_old
    

    create the symlink

    
     ln -s /home/user/project/public /home/user/public_html
    

    your folder structure should be similar to this

    
     public_html -> /home/user/project/public public_html_old www -> public_html
    

    Bravo! Your site should be working now.

    • You can deploy further changes by `git pull`ing the latest version or you using Continuous Deployment integration.

    Inspired by Darwin Biler

  • Top comments (0)