Setup Ubuntu Server (2020)

Bash

If at all possible, use latest Ubuntu version that is on a LTS release, and setup SSH key access, instead of a master password.

Once created & can login, open ~/.bashrc and put in the following:

alias upgrade='sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade && sudo apt-get autoremove && sudo apt-get autoclean'
COMPOSER_PATH=~/.composer/vendor/bin

Then source it with source ~/.bashrc and run apt-get update. Now we can run upgrade.

Note: When updating the kernel, a message may prompt that is pink/purple in the background, and gray with about seven options and the below text. Make sure keep the local version currently installed is chosen as the option, which is selected by default.

A new version of /boot/grub/menu.lst is available, but the version installed currently has been locally modified.

Sync Time

dpkg-reconfigure tzdata

Apt Packages

Next, we'll install the PHP packages we need:

apt-get install php7.4 php7.4-bcmath php7.4-bz2 php7.4-cli php7.4-common php7.4-curl php-curl curl php7.4-dev php7.4-fpm php7.4-gd php7.4-intl php7.4-json php7.4-mbstring php7.4-mysql php7.4-sqlite3 php7.4-xml php7.4-zip php-pear php7.4-cgi php-mbstring php-cli php-intl

Now we setup the basic services we'll need for a running laravel application:

  • Beanstalkd (queue)

  • Redis (cache, queue)

  • MySQL (client/server for DB)

  • Supervisor (running queue/keeping it up)

  • Nginx (http server)

  • Lets Encrypt (ssl certificates)

  • ImageMagick

  • Misc Server Stuff (such as git, zip

PHP Setup

PHP FPM should be running already, but let's double-check:

We'll first update the FPM php.ini file, located at - /etc/php/7.4/fpm/php.ini

Let's do the same changes for the CLI configuration, except skip memory limit. The file is at - /etc/php/7.4/cli/php.ini

After that's done, restart FPM with service php7.4-fpm restart and double-check that it's running fine with service php7.4-fpm status

Composer Setup

After setup, test that composer will run under root, and non-root. From that point on, it is much better to NOT run composer as root.

Next, to speed up composer going forward, with global, and non-global packages, install this:

Nginx Setup

Default vHost

Nginx should be running already, but let's double check:

Let's open the default vhost in /etc/nginx/sites-available/default and replace it with the following:

Run nginx -t to make sure nothing got messed up and set the root/sock path to match what you set up. By default it's /var/www/html , and it depends for the sock file. To find out for sure, and to do further configuration (FPM) before setting up a staging vhost, open the following file:

Look for listen = and you should see it being referenced now. Take that path and put it in the default vhost file.

Staging vHost

For this we will need to utilize Letsencrypt, enabling IPv6 on our machines, HTTP2, being able to see a hello world example, and running on PHP. First off, here's the vHost file with SSL being commented out. This will also redirect HTTP traffic to HTTPS.

The above can go in /etc/nginx/sites-available/sub.domain.com.vhost , and create a symbolic link in sites-enabled, by running:

Let's Encrypt Setup

With Let's Encrypt installed, and the Nginx plugin for it, and vHost setup, let's create a certificate! When running these commands, be sure to choose the Nginx option specifically for it. Nginx should have a non-ssl vhost to read, just comment out the SSH lines before running this (and restart nginx).

If you need multiple domains, keep passing in -d param. Here's an example with multiple sub-domains, and doing a dry run with the flag:

If everything checks out okay, then you can remove the comments by the SSL keys info, above for Nginx.

MySQL Setup

MySQL should be running already, but let's double check:

After installation you can secure a password & some recommended security improvement by running mysql_secure_installation via SSH. Skip the first one if you want, but create a new password, disable remote login, anonymous users, test databases, and definitely reload privileges.

Non-Root User Setup

So root is not so depended on, why don't we create a non-root user that can be used when logging into MySQL, for the Laravel database type, and in general is a great idea.

Remember that root password that was set previously? We'll need it below. After logging in, copy/paste the commands after the first one, one by one.

Redis

Require Password

To improve security, we should have a password set in the aforementioned .conf file, and then we can easily set it in the laravel app. Due to the speediness of redis, we need a long, unrememberable, password. Run the following:

Beanstalkd

Supervisor Setup

Create the file /etc/supervisor/conf.d/siteorenvname.conf and put the following in, with personalization:

After saving the file, run these commands:

Laravel Necessities

Non-Root User Setup

Laravel Permissions Setup

After creating project so files/folders are populated on disk, a git pull of a laravel project, whatever it is, below is what you want to run after composer. Right before development starts, make sure the permissions are right, locally, or on staging/production.

Please click on "laravel_permissions.sh" on the bottom left to see full instructions, and any feedback from the community.

File Storage: Digital Ocean

Instead of using a local driver for storage, or Amazon S3, let's use the same stack of Digital Ocean, but this time use their S3 alternative, which uses the same exact driver.

Open up config/filesystems.php and add in a new disk:

Create an API key, and Spaces bucket before proceeding any further. At the time of writing this, Spaces costs $5/month for unlimited buckets, besides further bandwidth costs. With a domain connected, it would be recommended to enable the CDN, and keep the other settings as is.

After updating the config file, lets set the values in our .env file:

nyc3 is the name of the region for your space, which you can find by going to Dashboard -> Spaces -> Look at name of space in the list. Region names will look like sfo2, nyc3, and so on. As for FILESYSTEM_DRIVER, this line makes Digital Ocean Spaces the default for any file storage usage, which is a good thing!

With a New York example above, here's another example with San Fransisco:

Run php artisan config:clear, clear cache, and restart the queue just in case.

Example Laravel .env File

With the above packages being installed, and configured, below is a sample .env file that can be loaded in. Adjust to your vHost location on your local/remote box, username changes - that sort of thing. In terms of drivers, testing that these things work - as of January 18th, 2020 - these all work with the newest Digital Ocean Ubuntu Server version.

Away we go:

Install NPM/Yarn with Correct Permissions

First, install NodeJS. After it completes running, two separate version numbers should popup. An example is below the install line:

With NPM, and NodeJS installed, we should install yarn next. This is simply a preference thing. A single version number, such as 1.21.1, should popup at the end if all went well.

In an ideal world, we would be done at this point. Unfortunately, permissions are back as a necessary step to resolve. Run the below, in order, and change out devs, if you created a different group name:

Next, update your ~/.bashrc file to include the following block - this will set the paths for NPM, Composer, Local Path & be ready to go!

Simply run source ~/.bashrc after saving the file. Running npm -v, composer, and yarn -v should all work properly. Since you'll want to end up using it anyways, run yarn global add @vue/cli to be 100% sure the front-end stuff is working alright.

PusherJS/Laravel Echo/Sockets

Run these commands after signing up for a free Pusher account:

Update your .env file to use the new broadcast driver, and update the values for pusher. We should also pass the channel name so VueJS has access to everything it needs for broadcasting events.

Misc Commands

Resources

Last updated