Prometheus Computing
Development, Support, Consultancy
Setting up an Ubuntu Server For Laravel

Setting up an Ubuntu Server For Laravel

DevOps

Laravel
Ubuntu
PHP

Published: March 5, 2024

Author: Andrew Arscott

Introduction

When you have completed your development, you will naturally want to get your website online. Probably the quickest method to do this is via Laravel Forge. Forge is a management solution which will do all the heavy lifting for you, including provisioning a server with the neccesary software, databases, caching solutions and so much more. Forge takes all of the hard work out of managing a web server and integrates with solutions like Akamai, AWS and many more. You can even bring your own server if you want too from a supplier not supported by them.

Forge isn't for everyone though, some people still prefer to manually manage their server, and personally, I'd recommend doing it at least once so you understand what is happening under the hood when you use a solution like Forge.

The most common stack you'd encounter is LAMP. Linux Apache MySQL and Php. Some setups, including Forge, will use Nginx instead of Apache, but for this tutorial we will be using Apache.

The Laravel documentation has information about deployment, we recommend reviewing the documentation which can be found here

Requirements

Server

Naturally, you will need a server. In this tutorial we will be using an Ubuntu server. There are other flavours of linux available like CentOS, but our preference is Ubuntu. You could also host on a Windows server if you desired, but the most common setup for Laravel would be a Linux server.

Web Server Software

Our web server software will be Apache. Apache has been around for a long time and is the time trusted server of many developers.

Database

Laravel supports many different databases through it's assortment of drivers. In this tutorial, we will be using MySQL.

PHP

At the time of writing, Laravel 10 is the current major version, and it has it's own requirements, you can find these requirements on their website.

They are:

  • PHP >= 8.1
  • Ctype PHP Extension
  • cURL PHP Extension
  • DOM PHP Extension
  • Fileinfo PHP Extension
  • Filter PHP Extension
  • Hash PHP Extension
  • Mbstring PHP Extension
  • OpenSSL PHP Extension
  • PCRE PHP Extension
  • PDO PHP Extension
  • Session PHP Extension
  • Tokenizer PHP Extension
  • XML PHP Extension

Redis (optional)

Redis is my preferred caching tool. It can also be used to handle your job queues, and when used in conjunction with a tool like Laravel Horizon, it makes a really nice experience for managing your queues. Of course you may choose another caching solution, and have no queues running, so this it entirely up to you.

Composer (optional)

You may choose to install composer so you can pull your dependencies on your server as part of your deployment process. If you do not, you will need to push the dependencies up with your website pre-installed.

Node/NPM (optional)

This step will depend on your build and deployment process. Many developers like to build their assets and store them in their source control. This means that when the code is put onto the server, either via pulling directly from git, or by uploading it via FTP, the assets are already built. I personally like to build the assets on the server so they don't have to live in my source control, and equally, because when I used to build before putting onto the server, I'd always forget to build!

Git (optional)

Like above, this step depends on your build process. Forge allows you to install from a git repo and then with it's deployment tools, it will pull automatically when you push to your production branch. This may not be required for your deployment method, especially if you are pushing via FTP instead.

Preparing your server

First things first, let's update our package manager, this will ensure our server has the latest packages when we start installing them.

sudo apt update

Installing Apache

You can install Apache using the following command:

sudo apt install apache2

Follow the prompts to confirm Apache's installation. This installation should take a few minutes to complete.

Some cloud providers have their own firewall tools running outside of the Linux Box. So this section may not always be applicable.

Once the installation has been completed, we need to tell the firewall to allow traffic to the relevant ports. In this case the ports we want will be 80 and 443. 80 allows insecure traffic, ie, http and 443 allow secure traffic, ie, https - as we won't have an SSL certificate straight away, we will enable both. You can always choose to change this to be secure only later on if you so choose.

Ubuntu's default firewall config tool is called UFW (Uncomplicated Firewall). It has different profiles you can use in your server. To list the profiles currently available run the following command:

sudo ufw app list

In the output, you should see Apache, Apache Full and Apache Secure. Apache is port 80 only, Apache Full is both port 80 and 443, and Apache Secure is 443 only. We want to enable Apache Full. To do this, run the following command:

sudo ufw allow in "Apache Full"

This will instruct the firewall to use this profile. You can confirm this has been changed by running:

sudo ufw status

You will see an output which will contain entries called Apache Full.

At this point, Apache will be running and serving it's default page. You can confirm this by loading your servers IP address, or if you have one configured, a domain pointing to the server.

Installing MySQL

Now, we need a database. So let's get to work installing the database. Install the MySQL server using the following command:

sudo apt install mysql-server

Once again, confirm you wish to install the packages once prompted. This process will take a few minutes to run.

Now, we need to configure the freshly installed MySQL server. You can do so by running:

sudo mysql_secure_installation

This will configure the MySQL installation. The prompt will as you if you wish to use the VALIDATE PASSWORD COMPONENT - this tool will check your password to make sure it is secure enough, and reject it if not. Whether you enable this or not is very much down to your personal preference. As long as you use suitably complex passwords, you should not need this tool, and equally if you are installing this server in a local environment for testing purposes, the complexity of the password may not be an issue at all.

Next a prompt will ask you about an anonymous user who can access the database. The prompt will recommend you remove these users before you move this server into production. Again, you may choose to remove the users at this point or at a later stage.

The prompt will ask you to choose if the root login should only be available locally. IE, from within the server. This is a good recommendation as it prevents someoen from guessing the root password from the network.

When MySQL is installed, a test datbase is included, the prompt will ask you to remove this database. I like to keep this around as something to test against should I encounter any issues with the installation.

Finally, it will ask you to reload the priviledge tables. This will make sure the config changes you have made during the installation take effect.

Great, MySQL is now install and configured. You can test it is working correctly by accessing the console by running the following command:

sudo mysql

This will load you into the console as the root user. The sudo command is providing the relevant credentials allowing you to login. To exit the console, type exit

Installing PHP

Now we need to install PHP with the required dependencies, but first, we wan't to add a new repository, so we can pull the most recent files. To do this run:

sudo add-apt-repository ppa:ondrej/php

and then run

sudo apt install php libapache2-mod-php php-mbstring php-cli php-bcmath php-json php-xml php-zip php-pdo php-common php-tokenizer php-mysql

This should install the most recent version of PHP, to confirm your installation run:

php -v

Installing Composer

Composer is a package manager used to install PHP packages. This is optional, but useful if you are installing your dependencies on the server as part of your deployment stage.

Firstly, get the installer script, and run it:

curl -sS https://getcomposer.org/installer | php

At this point, you might wish to verify the hash of the installer, we will assume you know how to do this yourself.

Move the composer.phar to a diretory called composer in your bin path

sudo mv composer.phar /usr/local/bin/composer

Now we must give it permissions to allow it to be executed:

sudo chmod +x /usr/local/bin/composer

Finally, verify composer has installed correctly by running the following command:

composer --version

At the time of writing, you should have installed Composer 2

Installing Node and NPM

Node is used to allow us to build our front end assets. This is optional as you may choose to do this on your machine as part of a deployment step. If you are choosing to build your assets on the server during deployment, then you will need node and npm.

To install node 20, run:

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - &&\
sudo apt-get install -y nodejs

If you wish to load another version of node, you can view details about installation of other versions here

Confirm the installation by running:

node -v

Then, you can install NPM by running:

sudo apt install npm

Again, confirm it's installed by running:

npm -v

Summary

By following this guide, you will now have a server setup where you can host a Laravel website. There are additional services which you can configure like Redis, as I mentioend above. Keep an eye out for additional configuration guides covering these topics.