How To Install Laravel on an Ubuntu Server

So you can run SurvLoop on the web.

Updated: November 19, 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 php-mbstring php-xml php7.2-zip

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/info.php

Type "<?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, and save the file:

e.g. http://111.111.111.111/info.php

Create Swap File (optional)

If you use DigitalOcean's cheapest and smallest default Droplet, you may need to create some more temporary memory to complete the install process. This method has worked for me:

$ sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
$ sudo /sbin/mkswap /var/swap.1
$ sudo /sbin/swapon /var/swap.1

Install Pear & Composer

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

$ sudo apt-get install php-pear pkg-config php-xml php7.2-xml php-dev
$ sudo wget http://pear.php.net/go-pear.phar
$ php go-pear.phar
$ sudo apt-get install curl
$ curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

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

Swap out example.com with your domain, if you want to set one up now.

$ sudo nano /etc/nginx/sites-available/example.com

Add the following content, which was taken and slightly modified from the default server block configuration file, to your new server block configuration file (swap out domain):

server {
        listen 80;
        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name example.com;

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ .php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }

        location ~ /.ht {
                deny all;
        }
}
You can read more about these settings in the original DigitalOcean instructions. Save and close the file. Create a link for it, and remove the default:

$ sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
$ sudo unlink /etc/nginx/sites-enabled/default

You can quickly double-check for any syntax errors by running this:

$ sudo nginx -t

Then reboot Nginx once more:

$ sudo systemctl reload nginx

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:

$ sudo apt-get update
$ sudo apt-get install software-properties-common $ sudo add-apt-repository universe $ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install certbot python-certbot-nginx $ sudo certbot --nginx $ sudo certbot renew --dry-run

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