Uploading Laravel 5 App to VPS or Shared Hosting with SSH Access
This post will serve as a memo to myself as I keep googling the same over and over again.
Requirements
This post is for you if you have the following:
- A VPS or
- A shared hosting account that provides limited SSH access.
- You are familiar with terminal, ssh and linux
- Can use Git (Alternatively, Just zip your laravel project and upload)
- A Laravel 5 project (yes, this post is specific to laravel 5).
Steps
Now that you have all the requirements, let's dive into the detailed step by step guide.
1. Share your code to Github.
Yes, share your project as a private or public repo on github. To do that, create a github repo and configure it as remote for your local repo.
If you are using PHPStorm you can use the VCS integration instead.
2. Clone the Git Repo to Your VPS or Shared Server.
SSH to the root directory for your domain/site, then run the following command to clone your laravel project:
git clone https://github.com/your-project.git
TIP: Replace https://github.com/your-project.git with the actual URL of your github repo.
3. Move/Copy public folder to public_html
Cd into the repo you just cloned and copy the content of your laravel public folder to your servers public_html directory. To do that, enter the following command:
cp -R public/.* ../public_html
4. Edit index.php
cd to public_html and modify the content of index.php as follows:
change
require __DIR__.'/../vendor/autoload.php';
to
require __DIR__.'/../repo-name/vendor/autoload.php';
Also, change
$app = require_once __DIR__.'/../bootstrap/app.php';
to
$app = require_once __DIR__.'/../repo-name/bootstrap/app.php';
TIP: change repo-name to the name of the cloned repo on your server.
5. Install dependencies
Run the following code in your ssh terminal to install all necessary laravel dependencies:
composer install
6. Config .ENV
If there is no .env file in the root of the repo you cloned, create one by running the following code from the root of the repo:
nano .env
Copy the content of the example .env file on your local repo then paste it in nano. Press control + x to exit and save.
If your application uses a database, you might want to add database details before saving the file.
After that running the following command to generate app key:
php artisan key:generate
You can also run this command to migrate your database tables:
php artisan migrate
Now try opening your website. Everything should work. Else, you should get laravel specific error if something went wrong.
Extra
If your site allows users to upload pictures and other files consider adding the following line of code to app/Providers/AppServiceProvider.php under the register method.
$this->app->bind('path.public', function() {
return realpath(base_path().'/../public_html');
});
Conclusion
That’s all for now. Hopefully, the post was helpful. You can speak to me on Twitter via @ea_pius. Cheers!!!