How To Install the Laravel PHP Framework on Digital Ocean

If you instead want to install Laravel on your local computer, here my previous article on how to install on a Mac:
worldorder.wiki/2016/11/26/coding-with-laravel-installing-homestead-on-a-mac/


Create Your SSH Key, If You Haven’t Already

From the macOS Terminal (Applications -> Utilities) — or alternatives like iTerm — run this command

ssh-keygen -t rsa

Press the ENTER key to accept the default location, then enter a passphrase twice. This creates new files in your Mac User’s folder at “~/.ssh/id_rsa.pub” and “~/.ssh/id_rsa”. Run the following command to copy your key to your clipboard, for the next step:

pbcopy < ~/.ssh/id_rsa.pub

Log Into Your Digital Ocean Account

Create a new Droplet, with a recent version of Ubuntu.

The minimal memory size of 1GB is sufficient to get off the ground.

If you have already setup your SSH Key with Digital Ocean before, select it. Otherwise, click the “New SSH Key” button and paste the contents of your key which was copied to your clipboard above.

After it is created, open up your Droplet in the website, go to the “Access” tab, and click the button to “Reset Root Password”.This will quickly reboot the server while resetting the root password, then send it to your account’s email address.

It sometimes take around 10 minutes to receive, in my limited experience.

Link The Domain (optional)

Go to your domain registrar and setup Custom DNS Servers to…

  • ns1.digitalocean.com
  • ns2.digitalocean.com
  • ns3.digitalocean.com

Then return to Digital Ocean and go to the Networking area, where you can “Add a domain”:

This will bring you to the domain’s details, where you should add DNS Records. Add two “A Records” pointing to your newly created Droplet, one for hostname “@” and one for hostname “www”…

Setup Your Server

Once you receive your server’s reset root password via email, you can log into your server via SSH. You can use Digital Ocean’s button to “Launch Console”, but other command line terminals (e.g. iTerm) will allow you to copy and paste things like your most secure passwords.

If your domain linkage has already propagated, you can SSH connect using your domain name instead of your server’s IP address in this command:

ssh root@12.34.56.78

This will prompt you for the temporary password you were emailed as “(current) UNIX password.” Then you should enter a new, strong password twice when the server prompts you. DON’T FORGET TO COPY AND SAVE YOUR PASSWORD SOMEWHERE SECURE.

Install Core Web Packages (Apache version)

Run this series of commands, accepting defaults, and confirmations by pressing the Enter key. When you create a MYSQL password, be sure to copy and securely save it.

$ apt-get update
$ apt-get install tasksel
$ tasksel install lamp-server
$ apt-get install php-mbstring
$ apt-get install php-zip
$ apt-get install phpunit

Now edit this Apache file:

nano /etc/apache2/mods-enabled/dir.conf

…and move index.php to the front of the list there:

DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm

Save a file in the ‘nano’ editor by clicking Control-X, “Save modified buffer?” type “Y” for Yes, then press enter to confirm overwriting the file with the same filename. Then restart Apache:

service apache2 restart

Test That PHP Is Running (optional)

Create a new file in the default public root:

nano /var/www/vhosts/worldorder.wiki/httpdocs/index.php

Type this, “<?php phpinfo(); ?>”, into the empty file, and save it. If things are working, you should be able to type your IP address (or propagated domain name) into your browser, and see a bunch of details about your PHP installation.

Install Pear & Composer

Run this series of commands, accepting defaults, and confirmations by pressing the Enter key:

$ wget http://pear.php.net/go-pear.phar
$ php go-pear.phar
$ apt-get install curl
$ curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
$ service apache2 restart

Create A Database

Down the line, you might want a GUI to play with your databases. I’ve enjoyed Sequel Pro.

Give your new SSH connection an appropriate name, and use your Droplet’s IP address for the hosts. Copy the MYSQL root password you created while installing the core packages above.

 

Finally, link your SSH Key in your operating system, “~/.ssh/id_rsa.pub”. This can sometimes be challenging since this is a hidden folder which you may need to work to reveal.

Once you are connected, use the drop-down in the top-left corner to “Add Database…”. Give your database an appropriate name, and write it down for later.

Install Laravel

Run this series of commands, accepting defaults, and confirmations by pressing the Enter key:

$ rm /var/www/vhosts/worldorder.wiki/httpdocs/index.html
$ composer create-project laravel/laravel /var/www/laravel 5.3.*
$ rm /var/www/laravel/vendor/compiled.php
$ cd /var/www/laravel
$ composer update
$ php artisan key:generate
$ chown -R www-data:33 /var/www/laravel/storage
$ chmod -R gu+w /var/www/laravel/storage
$ nano .env

While editing Laravel’s .env file, enter your domain name for the APP_URL, and database connection info:

APP_URL=https://example.com

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=newdatabasename
DB_USERNAME=root
DB_PASSWORD=strongpassword

Finally just clear the cache…

php artisan cache:clear

Setup Server’s Domain

$ cd /etc/apache2/sites-available
$ cp 000-default.conf example.com.conf
$ nano /etc/apache2/sites-available/example.com.conf

Edit this file to replace “DocumentRoot /var/www/vhosts/worldorder.wiki/httpdocs” with:

DocumentRoot /var/www/laravel/public

Open up one more file to edit:

nano /etc/apache2/apache2.conf

Edit the bottom of this new file, to replace these two default lines for the public directory settings (/var/www) with…

Directory /var/www/laravel/public
        AllowOverride All

Run a few more commands to finish this step:

$ a2dissite 000-default && sudo a2ensite example.com
$ a2enmod rewrite
$ /etc/init.d/apache2 restart

Install SSL Certificate

Thanks to the EFF‘s Certbot, you can now get this step down quickly and easily… for free!! Just run these commands:

$ apt-get install software-properties-common
$ add-apt-repository ppa:certbot/certbot
$ apt-get update
$ apt-get install python-certbot-apache
$ certbot --apache
$ certbot renew

Start Using Laravel or Install SurvLoop

You should now have a functioning server with basic, common security measures installed! From here, you can install SurvLoop, any other Laravel-based projects, or start building your own. You should now see the basic Laravel page when you browse to your IP address or domain name.