Laravel artisan migrate deployment production - php

In my deployment strategy I want to do the following:
Get code from git
Install dependencies via composer (dev requirements as well)
Run tests (phpunit etc)
Install dependencies for production (will remove dev requirements)
Zip
Copy to server
Unzip
Change symlink to current release (leave 2 old releases in case of revert)
At this point can I run php artisan migrate to update the database?
Considerations:
The application cache files are purged (actually they are empty like a fresh install).
Will the migrate query check the schema to know if updates are required?
All in all:
Can I run php artisan migrate safely in production with no previous application cache?
How does the migrate task kow the history of the table and what needs to be done?

When you first run your migrations, Laravel creates a migrations table which helps it to know at what point you are with your migrations.
I suggest doing always a backup, anyway you can update your tables without any issue if you test them locally before applying them in production and, most important, you don't edit the old migrations but instead add new ones to migrate, event to edit existing tables (add/remove columns).
PS: Why would you need to symlink if you use git? I'd just tag a working release.
If you are able to get ssh access to your hosting server, even a sandboxed version to just be able to access your site folder, you may directly deploy using git. Best way to avoid any problem caused by a failing copy of files.

Related

What is the best way to deploy changes in Laravel

We already have a Laravel web app hosted in the cloud (AWS EC2 instance). Let's say that changes will be required to be made, such as revising the (blade view) layout or adding new reports.
After I make the changes to the local controller, view and route files, do I simply copy them to the cloud host? Laravel keeps a cache of the blade view files. Will they be updated when the blade view files are updated? What other items do I need to do?
The views caches will be automatically regenerated, but you can force delete the cache if you want. php artisan cache:clear.
For routes, it's parsed from the file, so no worry there.
If you change config at some point, make sure to run php artisan config:clear.
If you touch the services and something is still not working, try php artisan clear-compiled. it will force the regen of the bootstrap.
The best way would be to use a source version control software like git and implement a pipeline on a service like GitLab.
I recently implemented one with this tutorial.
It exploits GitLab pipelines and Larvel's Envoy to automatically run tests and deploy your code that passed each stage you defined. It also allows you to rollback to previous versions at any given time.
For deploying PHP applications in any framework, you can use various tools. The most simple is PHP Deployer, and i recommend you to use it if you're not familiar with automatic deployment. You can set a sequence of commands which will be launched during deployment e.g.
git pull origin master
php artisan cache:clear
php artisan migrate

How to use Composer with git pull so that class not found does not comes

I am using Laravel 5.6.
My Current Code deployment process is as follow
Made some changes to file
Commit and push it to branch.
Once everything is perfect merge it to master and pull master branch to code production server(Where my code is).
My Issue
If I have some dependencies which is maintained by composer. I just add it to current branch and made changes to other files.
When I merge it with master and pull it to production server I need to run composer update command manually.
Server gives errors when we try to access it that it dependencies class or service provider not found(As we need to add it to config/app.php in Laravel) When it is updating composer dependencies.
After updating it is working perfectly.
So I want to avoid that 5-10 sec error which comes while updating composer dependencies.
I have two options
Put Application on maintenance mode while updating code. -> I cannot do that.
Every time whenever there is dependencies upload it using two merge. -> I Don't want to do that.
Is there any other best practices solutions to this problem so that we can avoid such errors.
You should never update on the production server, just composer install. You can hook this to your merge git action, accordingly to how you deploy to production (CI/CD, scripting or all manual?). Here documentation for git hooks: https://git-scm.com/docs/githooks
this is for git-hooks post-merge: https://git-scm.com/docs/githooks#_post_merge
The best way to deploy to avoid this problem is that each time you deploy, you do so to a new folder. Then, once the deployment is complete, you switch the folder.
This could be done using a symlink, or similar:
deploys
1551270000
1551280000
live > deploys/1551280000
Then make a new deployment (1551290000):
deploys
1551270000
1551280000
1551290000
live > deploys/1551280000
Then when it’s finished, change the symlink:
deploys
1551270000
1551280000
1551290000
live > deploys/1551290000
This is what Capistrano and others do. I would recommend using a tool like that rather than trying to write your own – this problem has already been solved.
When I merge it with master and pull it to production server I need to run composer update command manually.
No you dont. You should only run
> composer install
on the live server to make sure composers dependancies are up to date with changes.

Deploying Laravel 5.4 to Shared Hosting

I've got a real head-scratcher for you! I've been working on a Laravel 5.4 application for quite some time now and up until yesterday I had been able to:
develop on my local machine [still works flawlessly],
push my changes to my BitBucket repo [still okay here],
and would subsequently pull those changes to my shared hosting server (RedHat) [still running smoothly],
I then run my dependency managers (npm and composer) to get the project in place and functional
there is some matter with clearing various caches:
php artisan view:clear
php artisan route:clear
php artisan cache:clear
composer dump-autoload
and finally move my '/public' folder to the web root and update index.php to point back to the 'bootstrap/autoload.php' in main project structure,
I am aware there is likely another or several steps I am missing, but I am unsure what they are...
All that being said, I've attempted to deploy a number of applications using Laravel lately and I always seem to run into the same issue come time to deploy an application to production. I've read 30+ tutorials on the matter and nothing seems to explain the issue why my site isn't working any more.
I've checked the error log file maintained by Apache, it's empty.
Was wondering if it's a permissions issue, doesn't seem to be the case (all folders set to 775 and files set to 664 as specified by various sources and owned by serverName:userName)
Browser console simply shows a 500 server error.
All I see if "Whoops, looks like something went wrong." twice.
There must be some way to show better error details (config debug setting already set to true)
Any suggestions at this point would be beneficial to send me looking in the right direction!
======= UPDATES =======
For the sake of thoroughness, and that this save others from severe headaches, I'll be posting actions taken here.
Following tutorial mentioned by #user123456 (permissions applies)
Generate new key for application
Run php artisan config:clear
Off to the races, answer to come!
You need to ensure you have a working .env file.
Once done, run php artisan key:generate to create a key for your application after which you should clear your application's cache as follows php artisan config:clear
I would never recommend using shared hosting for Laravel application. You will face lots of issues for permissions, composer and external dependencies. Instead, you can use cloud servers like DigitalOcean, Linode, vultr and deploy laravel application on them. If you don't know about linux and creating Stacks you can use Cloudways to deploy laravel.
https://dev.to/rizwan_saquib/deploy-laravel-application-on-cloud-easily-with-cloudways

How can I organize my laravel project's github repository so that users can easily install and run it?

Five months ago I created a pretty extensive Laravel Blog Management system. I am now at a point where I am looking for a job and would like for potential employers to be able to easily install and run my project to check it out. I want to include instructions in the readme.md on how to get it started but I've just realized that I can't figure out how to run it myself!
Here is the repository: https://github.com/colesam/Laravel-Blog
Here is what I've tried:
git clone git#github.com:colesam/Laravel-Blog.git
composer install
php artisan serve
This copied the repo into my xampp/htdocs directory and ran it. XAMPP is currently running with MySQL and Apache running.
The console responds by telling me it's being served on localhost:8000. Unfortunately I receive an error message on the actual html doc:
What is going wrong with my project and how can I make this as easy as possible for my potential employers / anyone who would like to download and check out my project?
It's really easy actually. Takes about ~5 minutes. Here are the steps:
Clone the project
Create a database
Copy .env.example to .env and set the correct database credentials
Run php artisan key:generate to generate the app key
Run php artisan migrate to create the tables
Run php artisan serve
And you're done.

How I can install and configure an existing Laravel Project? (Laravel 4)

I need to install and configure an existing Laravel 4 project.
I tried to do, but when I ran composer update or composer install a lot of issues appear.
I have the database too (with data) so I ran the migration but doesn't work because the console show me an issue about the "table doesn't exist".
Can anyone tell me the complete process to configure the App?.
I mean, what its first, second and so and so because maybe in some step I made a mistake
To install and configure an existing project, you'd typically
Check out its source code
Run composer install
Run php artisan migrate
Check the README for specific instructions on installing 3rd party assets, or any additional steps you'd need to take
If the above creates errors, its either because of something in your environment (installing over an old project?) or some problem with the way the Laravel developer created their project.
To install and configure an existing project, you'd typically check those things first :
You should goto app/config/database.php check file and verify username and password.
After check in Project Folder vendor folder and composer.json file exist then remove it (that remove old configuration now we going to fresh configuration).
Then after back to command prompt and fire command composer update and that download some dependent file download.
Now Run php artisan serve
that tricks work for me last time when I migrate another host.
Carlos, when using an existing DB you'll need to check the migrations table to see which migrations have run successfully. Typically when taking over a laravel project setting up a new db and running the migrations against it is a good idea because you never know what hacks were made to the database outside the migration system. One small change can throw the entire system into a useless state because it's looking for a table or column that doesn't exist or has been modified. If you run the migrations against a bare db you can also figure out which tables were manually created (which could very well be your issue) outside the system and add them in as necessary. Cobbling things together after a previous developer is tedious, hopefully there is decent documentation.
Note that if you are using Git with .gitignore, don't forget to copy .env variables in new location too.

Categories