How To Install Laravel on an Ubuntu Server

So you can run SurvLoop on the web.

Updated: March 23, 2019

How To Setup an Ubuntu Server on Digital Ocean

 

Create Non-Root User

I'm calling this user frank, but you can call it anything. This is a summary of Digital Ocean's Initial Server Setup with Ubuntu 18.04.

# ssh root@your_server_ip
$ adduser frank
$ usermod -aG sudo frank
$ ufw allow OpenSSH
$ ufw enable

And copy your SSH Key from your root user to your new one:

$ rsync --archive --chown=frank:frank ~/.ssh /home/frank

Then exit as root, and log back in as your non-root user.

$ exit
# ssh frank@your_server_ip

Install LEMP Stack

This is a summary of How To Install Linux, Nginx, MySQL, PHP (LEMP stack) on Ubuntu 18.04

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.

$ sudo apt update
$ sudo apt install nginx
$ sudo ufw allow 'Nginx HTTP'
$ sudo apt install mysql-server
$ sudo mysql_secure_installation
During the MYSQL install, I choose to...
  • n - No thanks, password plugin
  • strong-password - Needs to be copied and saved somewhere safe
  • y - Remove anonymous user
  • y - Disallow root login remotely
  • y - Remove test database
  • y - Reload privelege tables
$ sudo mysql
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'super_strong_password';
mysql> FLUSH PRIVILEGES;
mysql> exit
$ sudo add-apt-repository universe
$ sudo apt install php-fpm php-mysql

This is a good time to associate your domain with the server, which is described in Step 3 of the tutorial.

$ sudo systemctl reload nginx

 

Test That PHP Is Running (optional)

Create a new file in the default public root:

nano /var/www/html/index.php

Type this, "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

Enable Remote MYSQL Connection (>=v5.7)

$ nano /etc/mysql/mysql.conf.d/mysqld.cnf

Comment out the line for bind-address, set it to 0.0.0.0, or increase security with your own IP address.

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/html/index.html
$ composer create-project laravel/laravel /var/www/laravel 5.8.*
$ 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/html" 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.

Next, Install SurvLoop on top of Laravel

Frequently Update Ubuntu, PHP, etc

$ sudo apt-get update
$ sudo apt update && sudo apt dist-upgrade
$ sudo apt install update-manager-core
$ sudo do-release-upgrade
$ composer update
$ php artisan cache:clear

To upgrade your server's PHP version, and ensure your code uses it...


$ sudo apt-get install php7.3 php7.3-cli php7.3-common libapache2-mod-php7.3 php7.3-mysql php7.3-gd php7.3-imagick php7.3-recode php7.3-tidy php7.3-xmlrpc
$ update-alternatives --set php /usr/bin/php7.3
$ a2dismod php7.2
$ a2enmod php7.3
$ systemctl restart apache2