How to Deploy Laravel to Digital Ocean — Step by Step

you can trymysql -u root -p followed by the password you just createdNow you can run sql commands such as this to show databasesSHOW DATABASES;Lets exitexitAlso install nginxsudo apt-get install nginxPress y hit enterTo see whats installed runllIf you scroll up, there you see nginx among a lot of other stuffAlso lets install gitsudo apt-get install gitcheckgit –versionyou see — git version 2.7.4Also install zip and unzip which is required by composer laterConnect Github or BitBucket Repository to Digital Ocean ServerIts time to connect your github or bitbucket repository to this serverI will use bitbucket because there you can host private repository for freeWhereas in github, you get to host only public repository for freeThe process however will be exactly same, I will be covering bothIn your terminal where you are inside the digital ocean server run the create keygenUse the line below — change to github if you will be using githubssh-keygen -t rsa -b 4096 -C "bitbucket"Then you will get this:Enter file in which to save the key (/home/codecontinue/.ssh/id_rsa):This path is in server, so we dont need to make any changesJust hit enter multiple times then runcat ~/.ssh/id_rsa.pubCopy the long key you see in terminalOpen bitbucket or github webpageCreate a new repository — give it a name and hit createThere you get the repository link..lets just wait..keep the window open.Head over to your existing laravel project or create a new one whatever you preferGet inside the project in a new terminal window using cd codecontinue (codecontinue is my laravel project name here) and rungit initgit add .git commit -m "deployment"Now run the command below followed by the url (remove git clone part if its there)You actually see the url on the github or bitbucket page while you created a new repositorygit remote add origin https://[email protected]/kaloraat/codecontinue.gitgit push -u origin masterOk our project is now pushed in bitbucket or githubNow we need to add ssh key to our project in bitbucket or githubFirst copy the key that we still have in terminal where we are logged into the serverThen either in bitbucket or github — go to settingsIn github, its in Settings > Deploy Keys > Add deploy key (on top right corner)Then give it any title and paste the key in the key fieldClick Add key and you are done (No need to click Allow write access)In bitbucket its in setttins > Access keys (left sidebar)Click on Add keyGive it a name and paste the key in the key box hit saveNow lets test the connectionBack in terminal where we are in serverRun the code below — if using github replace bitbucket with githubssh -T [email protected]It will sayThe authenticity of host 'bitbucket.com (18.205.93.4)' can't be established.Are you sure you want to continue connecting (yes/no)?Type yes and hit enterNow you see the following:This deploy key has read access to the following repositories:It means we are doing good!Clone Project Repository from Github or BitbucketNow we can clone this repo to our serverClone into html folder (add html at the end)git clone https://[email protected]/kaloraat/codecontinue.git htmlType password if asked — it will be either github or bitbucket passwordLets get inside the projectcd htmlllHere you see all the files and folders of your projectInstall composer from within this html foldersudo apt install composerTry running composer command — we have it installed nowcomposerTo meet the composer install requirements and to avoid some issues(bugs) with php7.3 lets install them again downgrading to php7.2Frst we need to remove php7.3Run this commandsudo apt-get purge php7.Then runsudo apt-get install php7.2-cli php7.2-fpm php7.2-mbstring php7.2-mysql php7.1-mcrypt php7.2-curlsudo apt install composersudo apt-get updateThen runcomposer install –no-devAwesome..all the packages required to our laravel project are installedTry running llThere you see .env.exampleLets change that to .envcp .env.example .envphp artisan key:generateNow try accessing the app using the IP addressPaste in the url of your browser204.48.20.189There you will get:Welcome to nginx!That means we need to configure nginx nextNginx ConfigurationTime for nginx configurationBack to server inside html folder, thats where we left earlierGo back to rootcdcd /etc/nginxllThere you will see — sites-available/sudo vim sites-availableThere you see defaultWhich means we need to get inside defaultTo exit run::qsudo vim sites-available/defaultNow we are in the nginx configurationThere you see -root /var/www/html;To make changes first press iThen change that by adding public at the end like so:root /var/www/html/public;Then scroll down… and find this line:index index.html index.htm index.nginx-debian.html;Change to the following:index index.html index.htm index.php;Then chang this:try_files $uri $uri/ =404;To this:try_files $uri $uri/ /index.php$is_args$args;Then uncomment and make change from php7.0 to php7.2Here is the complet code# means commented out – uncomment the other lines as shown belowlocation ~ .php$ { include snippets/fastcgi-php.conf; # # # With php7.0-cgi alone: # fastcgi_pass 127.0.0.1:9000; # # With php7.0-fpm: fastcgi_pass unix:/run/php/php7.2-fpm.sock; }To write and quiteThen to get out of the edit mode press ESC key in your keyboard..And then type :wq and hit enter Enter keyThencd /var/wwwllcd htmlllThere we dont have our projectInstead our project is in the root directory’s html foldercdllcd htmlllWe should move this from root to /var/www/htmlcdFirst lets remove the empty html foldercd /var/wwwsudo rm -rf htmlcdsudo mv html /var/www/cd /var/wwwllcd htmlHere you see all the files/folders of laravel projectcdsudo service nginx restartCeck if nginx is working properlysudo nginx -tAllow permision to read and write to certain folderscd /var/www/html sudo chmod -R 777 storageNow go to browser and access your projecthttp://204.48.20.189/When you make any change to your project, you push to your githubThen to pull that change here in digital ocean server (inside html folder where our laravel project is) like so:git pull from within /var/www/htmlMySql Database connectionIn your server runmysql -u root -pSHOW DATABASES;Create a new databaseCREATE DATABASE codecontinue;Then to exit runexitcd /var/www/htmlupdate .envvim .envPress iChange database nameDB_DATABASE=codecontinue DB_USERNAME=root DB_PASSWORD=whateverRunning php artisan commandsNow you can run Laravel php artisan commands as well like form within /var/www/htmlphp artisan migrateAccess live server database to local computerI am using sequel pro for macQuit if sequel pro is already open..Then start sequel proClick on ssh tabGive name — codecontinuemySql host = 204.48.20.189 username: root password: whatever database: codecontinue port: 3306 ssh host: 204.48.20.189 ssh user: codecontinue ssh key: click on key icon locate the key inside .ssh hidden folder – id_codecontinueserver ssh port: 22​Click connect and you are done..Now you can access live server database in your local computer..Thats great!Thanks for reading this..I hope you made it..If you come across any issues, leave your comments below..Cheers!Originally published at kaloraat.com.. More details

Leave a Reply