You are on page 1of 15
51972020 How to Deploy Laravel Projects to Live Server: The Ultimate Guide -Laravel Dally How to Deploy Laravel Projects to Live Server: The Ultimate Guide There are a lot of courses and articles about coding but much less about putting it in production. With a lot of questions asked, I decided to write this ultimate (hopefully) guide to deploy Laravel projects In this article, I will assume two things: + You already know Laravel and its artisan commands on at least junior level + You want to deploy your project to a dedicated server and not a shared hosting, I have a video-rant about shared hosting for Laravel Notice 1: please take this article as personal advice but not 100% accurate process. Every team have their own way to deploy projects, so you may read different advice elsewhere Notice 2: this article is written in October 2018, check for any updates to software and Laravel/PHP versions at the time you're reading it, IT world is constantly changing. Enough formalities, let’s dive in. In this article, we will cover these steps: Prepare your dedicated server for Laravel Initial launch of the project How to deploy new changes Zero-downtime deployment ‘Teamwork, staging server and branches Brief overview: Automated testing and continuous integration ouaune Step 1. Prepare your dedicated server for Laravel I've recently written a recommendation article for the server needed for Laravel projects. My personal preference is Digital Ocean. They have their own guide for Laravel projects. To be honest, I don’t want to focus on this step too much, cause server preparation is not part of deployment. But these are the parts that you need to prepare, in short: Part 1. Create/purchase your dedicated server. In case of Digital Ocean it’s called Droplet, Amazon AWS call it EC2 instance etc. Part 2. Install LEMP or LAMP stack. LAMP/LEMP stands for Linux (comes with server), web- server (Nginx for “E” and Apache for A”), MySQL database and PHP, A lot of server providers htpstaraveldallycomMhow 3o-doploy-aravel-projcts-to-ve-server-ho-tin 1s. 51972020 How to Deploy Laravel Projects to Live Server: The Ukimate Gulde -Laravel Dally offer this as a one-click install ~ see Digital Ocean example below An Experience Designed for You The LEMP stack is a group of open-source software to get web servers up and running, Available as a One-Click application get NGINX, MySQL, and PHP-FPM installed on your Droplet in less than a minute. G G ™~ php Ubuntu 18.04 NGINX MySQL PHP-FPM Notice: Linux-server is the reason I advise to use Linux environment on local computer, too. This way you get used to configuration. But don’t worry, you don’t need to install Linux or buy specific new computer - it all can be emulated with Laravel Homestead or Laravel Valet. Part 3. Configure SSH access for yourself. You will probably deploy changes by SSHing to the machine and running commands like git pull, php artisan migrate etc. Forget uploading via FTP, if you still do it. Learn more about users/privileges for SSH access in this Digital Ocean article, Part 4. PHP and Dependencies for Laravel. At the time of this article, Laravel latest version is 5.7 with these requirements: + PHP >= 7.1.3 ‘+ OpenSSL PHP Extension = PDO PHP Extension + Mbstring PHP Extension + Tokenizer PHP Extension + XML PHP Extension + Ctype PHP Extension + JSON PHP Extension Part 5. Install/configure composer. You do want to run composer install command on the server, right? Follow the instructions from the official GetComposer.org website, Part 6. Install/configure git. Putting the code to the server will work by pulling it down from git repository. Probably this official instruction will help. Part 7. Configure MySQL. You need to create a database specific for your project, and a user to access it. You may create a separate user, granting only specific privileges. Digital Ocean have article on it, too. hitpsfaraveldaly.comowo-deploy aravel-proects-to-ve-server-he- 216 51972020 How to Deploy Laravel Projects to Live Server: The Ukimate Gulde -Laravel Dally Part 8. Configure Web-server. Prepare a specific folder for your website. Here’s an example Nginx config provided in official Laravel documentation. server { listen 80; server_name example.com; root /example.com/public; add_header X-Frame-Options “SAMEORIGIN" ; add_header X-XSS-Protection “1; mode=block"; add_header X-Content-Type-Options “nosniff"; index index.html index.htm index.php; charset utf-8; location / { try_files $uri $uri/ /index.php?$query_string; location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } error_page 404 / index.php; location ~ \.php$ { fastcgi_split_path_info “(.+\.php)(/.+)83 fastcgi_pass unix: /var/run/php/php7.1-fpm. sock; fastcgi_index index.php; include fastcgi_params; location ~ /\.(?!well-known).* { deny all; } Part 9. Point the Domain to Server. Your new server probably has some IP address given to you by Digital Ocean (or other provider), so now you need to go to the page of your domain configuration (where you've bought the domain) and change its DNS records, specifically A record to point to that new IP address. Here's article on Quora about it. Ok, at this stage, if you've completed all of the above successfully, you have your server ready. Not that simple, huh? To avoid all that manual work, I totally recommend using Laravel Forge ~ it prepares the server for you, with all the correct settings, in just a few clicks. If you work with multiple clients, totally worth $19/month (this is the most popular plan price at the time of writing this article). Also, you support Taylor Otwell and Laravel by using Forge. hitpsfaraveldaly.comowo-deploy aravel-proects-to-ve-server-he- 36 51972020 How to Deploy Laravel Projects to Live Server: The Ukimate Gulde -Laravel Dally Step 2. Initial launch of the project Now, as we have server ready, let’s put the code there. Part 1. Putting the code to the repository. Choose the system you want to use ~ GitHub, Bitbucket or Gitlab. Then create a repository there and push your code from your local computer. Here’s a sample sequence of commands from Github, from the point of initial 19 Git repository: git init git add README.md git commit -m “First commit" git remote add origin https://github.com/LaravelDaily/DeploymentDeno. git git push -u origin master Let's take an example of totally fresh Laravel 5.7 project. Now, if you pushed your code to Github, your repository should look like this. 6 LaravelDaly DeploymentDeme re @uwoe= | 2) [stow [0] [Fre |e No descrotion, web, or topes provided. Part 2. Cloning repository to the server. We need to SSH into our server, navigate to the folder prepared for the project, and launch git clone command. Keep in mind that your server should have access to the repository, if it’s not public. You can do that by specifying your username/password every time you're asked (see below), hitpsfaraveldaly.comowo-deploy aravel-proects-to-ve-server-he- ans. 51972020 How to Deploy Laravel Projects to Live Server: The Ukimate Guide -Laravel Dally {ordedstaging-taraveléailj:~/deplojmentdene. taraveldailj.cons git clone neeps://github.con/Leraveloaily/Deple ae Cloning into *. Userrane fer ‘httpst//github.con’: povitaskorop souerd fer “Http: //pavilaskorepegithub. con? Or you can set up the access following these instructions. After cloning is successful, we should see the files downloaded to the server. 121, dine. eer (171/121), done. to0s. (95/955, reused 121 (aett 286,50 KIB | ack-rvied 9 fecelving ebjets evtes/sy donee Resolving Getto a0 Checking comectivitys done. forgcestasiniarave Gait n/aeptoytentdenataravelaaity.cos 15 2 Srtison comoserstock veaitorconfig, sgitetirtbstes phpuniteest routes tests |ic, tmtstee seta “enwrocaeple) “gittgnore obtie.” " Gerverconp wabpeckias se ibe conmeser. son ie ectogeison,FesourcesStorane.”” Yarnelock {foraeestoging“Laravelandty:n/deptoyeentdeno.taravelaasty-cont Important thing about public folder - make sure that your Web-server configuration points to public folder correctly, and your full repository is cloned in one folder above, not into public. Here’s an example from Nginx config: server { server_nane deploymentdeno.1laraveldaily.com; root /home/forge/deploymentdemo. laraveldaily..com/public; Part 3, .env file, If you've done everything correctly, you should see this: « © notsecus |denomentienazoecion # JOSDS- 823004 @ ‘Warning roy /homeoreepoymeademo veal compu endolo hp: fed wo open sem: Noch le der in melo deplymeniemn ioral zom/pueiex ppc ie 24 Fatal error: reine): Pid opening requis Ybomeoreepoymentemo raved Illuminate\Foundation\ComposerScript > @php artisan package:discover --ansi Discovered Package: beyondcode/laravel—dump-server Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully. forge@staging-laraveldaily:~/deploymentdemo. laraveldaily.com$ fj postAutoloadDump Part 6. Generate application key. We need to run this command php artisan key:generate It generates a random key which is automatically added to .env file APP_KEY variable. iS php artisan key:generate ] set successfully. ‘forgegstaging-taraveldaily:~/deploynentdeno. Laraveldaily.cons jl Part 7. Migrating DB schema. Do you have any database table to migrate? Then let’s launch this: php artisan migrate Please make sure you've set up correct database credentials in -env file. hitpsfaraveldaly.com/howo-deploy-arave-proecte-to-ve-serverthe-ultmate-guie! m6 51972020 How to Deploy Laravel Projects to Live Server: The Ultimate Guide -Laravel Dally Part 8. Seeding data? If you have set up your database/seeds, this is probably the time to run them: php artisan db:seed To speed up the process, you may run two commands above as one: php artisan migrate --seed Part 9. Launch! Finally, let’s try it out in the browser... It works! Omen oan # nesGeuams00-6@ Laravel Congratulations, if you've reached this far, you have deployed Laravel project to your server! But that’s not the end of the article, let’s see how to work with this project and deploy future changes, Step 3. How to deploy new changes Ok so we have the first version of the project deployed. Now, how to deploy the changes in our code? Also, how to do it without breaking anything and with least amount of downtime possible? Here's the plan: Part 1, Artisan down. There is an Artisan command to get all the website “down”, php artisan down If you launch it on the server, the whole website becomes unavailable, and then you can do any deployment actions, and your visitors won't interrupt you. Otherwise you're at risk the someone will change some database data in live mode, which may even break your deployment. htpstaraveldallycomMhow 3o-doploy-aravel-projcts-to-ve-server-ho-tin ans 51972020 How to Deploy Laravel Projects to Live Server: The Ukimate Gulde -Laravel Dally After this command, your visitors will see this (in Laravel 5.7): 503 Sorry, we are doing some maintenance. Please check back To get the site up and running again, after all the deployment actions, run this command: php artisan up Part 2. Git pull. You need to pull the latest version of the code to the folder. It’s simple: git pull You can add more parameters like branch, but we will be talking about branching strategy a little later in the article. Part 3. Composer installl. Next thing is to check if there are any new changes in composer.lock files, so run composer install. Important notice: don’t run “composer update” on a live server. It will take a lot of time (a few minutes) and will break repository consistency. This is how you install a new package to the project: + On local computer run composer require with whatever package you need + Of, add your package to composer.json file and run composer update + During committing to the repository, you will have both composer.json and composer.lock changed. That's ok. + If you do composer install on the server, it will look into composer.lock and will do only the necessary changes, without checking all of the versions for all packages. Part 4. DB migrations. If you have any database changes in database/migrations, run this: hitpsftaraveldaly.comMhow4o-deploy-aravel-projecte-to-ve-serverthe-ulimate-gudel ons 51972020 How to Deploy Laravel Projects to Live Server: The Ultimate Guide -Laravel Dally php artisan migrate Actually, run this anyway, even if you don’t have changes. It won't error, just will say “Nothing to migrate”. But important notice: don’t edit the migration files if they are already in repository, for every change add a new migration file. Part 5 (optional). Restart FPM. This step is not always necessary, but will restart PHP and kill any processes that have been started. Laravel Forge advises this command echo "" | sudo -S service php7.1-fpm reload Change 7.1 version here to your real PHP version. Part 6 (optional). Restart queues. If you have any Queue mechanism running, it is advisable to run this: php artisan queue:restart Part 7 (optional). Clearing cache. Again, this step may be unnecessary, but if you're using Laravel cache, this command will clear it: php artisan cache:clear Part 8, Up and running again! And that's it, let’s get the site working again. php artisan up To sum up, here's an example sequence of commands in one of my projects: cd_/home/Forge/deploymentdemo. laraveldaily.com php artisan down git pull origin master composer install php artisan migrate php artisan cache:clear php artisan queue:restart php artisan up Step 4. Zero-downtime deployment htpstaraveldallycomMhow 3o-doploy-aravel-projcts-to-ve-server-ho-tin 1015 59/2020 How to Deploy Laravel Projects to Live Server: The Utimate Guide Laravl Dally You probably think that it takes a lot of time to (properly) deploy the changes. While you're running various commands, and investigate potential issues, visitors may be waiting for the site to be working again. For minutes. Not cool: To solve this problem, people came up with a solution called zero-downtime deployment. The principle is pretty easy: + You create a new folder on the server + You clone the newest version of the code there from repository + You run all the commands above on that new folder (while website is still running from the old folder) + Now you have two folders, you just need to change Linux so-called symbolic link ~ point it to a new folder This way, your customers experience absolutely zero downtime - symlinks happen almost instantly. ‘There are a few tools that arrange this zero-time deployment for you, with ability to rollback to a different folder, logging the process and report about errors. My favorite is the official tool by Taylor Otwell called Laravel Envoyer. Envoyer oon - wt (QuickAdminPanel Hotel Booking Demo 2 satin Project Oetaie Deployments Post-Deployment Heath ® © oO Recent Deployments It’s not as valuable as Laravel Forge for server provisioning, but if you want to have one-click zero-time deployment, try it out for $10/month. Step 5. Teamwork, Staging Server and Branches htpstaraveldallycomMhow 3o-doploy-aravel-projcts-to-ve-server-ho-tin ns 59/2020 How to Deploy Laravel Projects to Live Server: The Utimate Guide Laravl Dally Let’s take one step even further. How do you deploy the changes, while working in a team? And not necessarily sitting in the same office or even working within the same timezone. How do you not break each other's code? You need to use git branching, here’s an example screenshot from my own SourceTree for one of the projects: =a ‘i Biecies 5) (Show anew Bonin 6) (One Ore @ weinacooy —craor———_esrnion omnes q dea Yorn 28 | rr chr 13 DV master t one 7708 1 Versace 7 oFGInHEAD tymasir Merge pull request #722 rom LarveDaly/svelop sewores Nerge plein 2 rom Laren rome ff me ‘There are quite a few options to organize this process, but here’s the process I would personally recommend. 1, You should have (at least) two branches in repository - master and develop 2. You should have two servers ~ one is LIVE version and would pull the code from master branch (git pull origin master), and testing server (also often called “staging”) which would pull the code from develop (git pull origin develop) 3, No developer is allowed to commit directly to master branch. It may be even restricted on repository settings level. Everyone commits to develop branch ONLY. (or feature branches, more on that below) 4, When you're ready to release some function to live version, it should be done in develop branch and tested (manually and maybe with automated tests) on staging server. Then you do a Pull Request in repository, which merges develop into master. Pull Request may be reviewed by teammates, and then officially approved/merged. Then live server pulls the code from master. 5. If someone is working on a bigger feature, they should create so-called “feature branch" from develop branch, and then at some point do a Pull Request from that branch into develop (not master!). Likely there will be code conflicts to merge manually and talk between teammates. .env File on Staging Server Speaking about staging server, this is the one that probably your client will test on to confirm the feature is ready. So you need to prepare a separate environment for it, specifically .env file. Here are the important variables: APP_ENV=staging APP_DEBUG=true APP_URL=ht tp: //test -something. yourdomain.com Don’t leave APP_ENV as “local”, it should be different (but not “production”). Also, APP_DEBUG=true means it will show errors with all trace, as it should for testing purposes. But don't forget that on live server it should be strictly APP_DEBUG=false, otherwise hitpsfaraveldaly.com/howo-deploy-arave-proecte-to-ve-serverthe-ultmate-guie! 126 51972020 How to Deploy Laravel Projects to Live Server: The Ultimate Guide -Laravel Dally you have security issue. Finally, put your server's URL as APP_URL. Simple. DB_CONNECTION=mysql B_HOST=127.0.0.1 B_PORT=3306 DB_DATABASE=homestead jomestead ecret Have a separate database for staging server. Don’t ever play around with live data for testing. STRIPE_API_KEY=sk_test_N9voB. PUB_STRIPE_API_KEY=pk_test_plaGo Finally, if you have any external services like Stripe, don’t forget to put their sandbox/testing credentials in .env file. Or, for email sending, you can use a separate driver and service like Mailtrap. Bonus: Automated testing and continuous integration Kind of a separate “advanced” topic of deployment is you may want to run automated tests on a separate testing server. You can set it up manually or use tools like Travis CI, CircleCI, or Jenkins. ‘There are many ways how developers set it up and configure, but short version will be this + You have written automated tests ~ unit, feature, behavior or other type of tests. Check awesome course Test-Driven Laravel for this. + You have prepared database seeders to run and create a new fresh database with some dummy data for running your tests. + Every time you commit code to develop branch, testing server pulls it and runs the tests, informing you whether code is “safe” to go live (but don’t trust that “green light” blindly). Here's example automated check by Travis CI in our Pull Request on Github: In ideal scenario, which is called Continuous Integration, everything is automatic - developers just commit their code, and system informs the team if some tests fail htpstaraveldallycomMhow 3o-doploy-aravel-projcts-to-ve-server-ho-tin 1316 51972020 How to Deploy Laravel Projects to Live Server: The Ukimate Gulde -Laravel Dally Small text change for multi-tenancy page LaravelDaly merged 2 commits into master from deslop on Ane 30 Peorwersation @ Commits 2 By Checks @ (Ales changed 1 FER Laravetoaiy commented on Apr 30 [No description provided, Fi Povilas Korop added some commits on Apr 30 Wb nvies-tenancy page text change [ib rerge branch “develo of ety: //93tho.con/LaravelDaity/Qichadan? Ow Sica i esas ae But it can be done in more simple and manual way ~ just have basic automatic tests and run them manually by typing phpunit on your staging server. Just don’t forget to prepare the database for it - with seeders and fake data (recommended) or some testing semi-real data (be REALLY careful about it). Final Notes So here it is, I have described deployment process for Laravel (but most of it is applicable for other frameworks/languages, too). But even in this big article I haven’t touched some topics that may help to make this even smoother, So I’m leaving these for you to explore + Bug tracking and reporting software: we're using Bugsnag for this - here's my own video * Backup your database before every deployment, it’s really easy with Spatie Laravel Backup package + After successful (or not) deployment you may configure notifications to email or Slack, it depends on what repository system you use or what auto-deployment tools like Envoyer. Here’s how Laravel Forge informs us via Slack. arava Forge MP e874 Povlasorep deployed some fresh code! Com: pg cor/LaravelOsty/ Quick Adina ‘Message: Merge pl quest #232 trom LarvelDaly/dveop Uva vechat de oh I guess that’s it. I wish you more success messages like the one above @ Again, please take this article as advice for “typical scenario” but keep in mind there are dozens of other ways to deploy projects, and you may here different tips and processes from other hitpsfaraveldaly.com/howo-deploy-arave-proecte-to-ve-serverthe-ultmate-guie! 1416 51972020 How to Deploy Laravel Projects to Live Server: The Ukimate Gulde -Laravel Dally teams. Come up with your own process that is suitable for you! Like our articles? Check out our Laravel online courses! Povilas Korop PHP web-developer with 15 years experience, S years with Laravel. Now leading a small team of developers, growing Laravel adminpanel ‘generator QuickAcminPanel and publishing Laravel courses on Teachadl, in hitpsfaraveldaly.comowo-deploy aravel-proects-to-ve-server-he- 19115

You might also like