Laravel Ubuntu 20.04: Your Step-by-Step Guide
Laravel Ubuntu 20.04: Your Step-by-Step Guide
Hey there, fellow developers! So, you’re looking to get Laravel up and running on your Ubuntu Server 20.04 machine, huh? Awesome choice! Laravel is a super popular PHP framework that makes building web applications a breeze, and Ubuntu 20.04 is a solid, stable OS to host it on. In this guide, we’re going to walk through the entire process, step-by-step, so even if you’re relatively new to server administration, you’ll be able to follow along. We’ll cover everything from setting up your server environment to installing Composer, PHP, and finally, getting your first Laravel project up and running. By the end of this, you’ll have a fully functional Laravel development environment ready for you to start coding your next masterpiece. Let’s dive in!
Table of Contents
- Prerequisites: What You’ll Need Before We Start
- Step 1: Updating Your Server and Installing Essential Packages
- Step 2: Installing Composer: The PHP Dependency Manager
- Step 3: Setting Up Your Web Server (Nginx)
- Step 4: Installing Laravel via Composer
- Step 5: Finalizing and Testing Your Laravel Installation
Prerequisites: What You’ll Need Before We Start
Alright guys, before we get our hands dirty with the actual installation, let’s make sure you’ve got everything you need. First off, you’ll need
access to an Ubuntu Server 20.04 machine
. This could be a cloud server from providers like DigitalOcean, Linode, AWS, or even a local virtual machine running on your computer. You’ll need
SSH access
to this server, which means you should be comfortable using a terminal. If you’re on Windows, tools like PuTTY or the built-in Windows Subsystem for Linux (WSL) are your best friends. If you’re on macOS or Linux, your default terminal will do just fine. You’ll also need
sudo
privileges
on your Ubuntu server. This is essential because we’ll be installing software and making system-level changes. Finally, a stable
internet connection
is a must for downloading all the necessary packages. Make sure your server’s firewall is configured to allow SSH access (usually on port 22) so you can connect. It’s also a good idea to ensure your server is up-to-date before we begin. You can do this by running
sudo apt update
and
sudo apt upgrade -y
in your terminal. This ensures you’re starting with the latest security patches and software versions available for your system. Having these basics covered will make the rest of the installation process super smooth. Let’s get this party started!
Step 1: Updating Your Server and Installing Essential Packages
First things first, let’s make sure our Ubuntu server is up-to-date. This is a crucial step to ensure you have the latest security patches and software versions. Open up your terminal and connect to your server via SSH. Once you’re logged in, run the following commands:
sudo apt update
sudo apt upgrade -y
The
apt update
command refreshes the list of available packages from the repositories, and
apt upgrade
installs the newer versions of all packages currently installed on your system. The
-y
flag automatically answers ‘yes’ to any prompts, which is super handy when you’re working remotely.
Now, Laravel relies heavily on PHP and several PHP extensions. We need to install a PHP version that’s compatible with Laravel. For most modern Laravel versions, PHP 8.0 or higher is recommended. Ubuntu 20.04’s default repositories might not have the latest PHP versions, so we’ll add a PPA (Personal Package Archive) for a more up-to-date PHP.
Let’s install the
software-properties-common
package, which is needed to manage PPAs:
sudo apt install software-properties-common -y
Next, we’ll add the Ondřej Surý PPA, which is a well-maintained source for PHP packages:
sudo add-apt-repository ppa:ondrej/php -y
After adding the PPA, we need to update our package list again so that
apt
recognizes the new packages available from this PPA:
sudo apt update
Now, let’s install PHP along with some essential extensions that Laravel needs. We’ll install PHP 8.1 (you can adjust the version if needed, but 8.1 is a safe bet for current Laravel versions) and the common extensions:
sudo apt install php8.1 php8.1-cli php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl php8.1-xml php8.1-bcmath -y
Let’s break down those extensions:
-
php8.1-cli: The command-line interface for PHP. Essential for running Artisan commands. -
php8.1-common: Common files for PHP. -
php8.1-mysql: Required for interacting with MySQL databases, which is a popular choice for Laravel projects. -
php8.1-zip: Needed for handling ZIP archives, often used by Composer and Laravel for package management and file operations. -
php8.1-gd: Used for image manipulation. If your application will handle images, you’ll need this. -
php8.1-mbstring: For multi-byte string handling, crucial for supporting various character sets and languages. -
php8.1-curl: Allows PHP to make HTTP requests to other servers, useful for API integrations. -
php8.1-xml: For working with XML data. -
php8.1-bcmath: Provides arbitrary precision mathematics, which is important for financial calculations or any scenario requiring high precision.
Once the installation is complete, you can verify your PHP version by running:
php -v
This should output something like PHP 8.1.x, confirming that everything was installed correctly. You’ve successfully set up the core PHP environment for Laravel!
Step 2: Installing Composer: The PHP Dependency Manager
Alright guys, now that we’ve got PHP sorted, the next critical piece of the puzzle is Composer . If you’re not familiar with it, Composer is the dependency manager for PHP. It allows you to declare the libraries your project depends on, and it will install them for you. Laravel itself is installed via Composer, and most of your project’s dependencies will be managed through it. It’s an absolute must-have.
We’ll install Composer globally on your server so you can use it from anywhere in your terminal. First, let’s navigate to your
/tmp
directory, as it’s a good place to download files temporarily:
cd /tmp
Now, we’ll download the Composer installer script using
curl
. We’ll fetch the latest stable version.
curl -s https://getcomposer.org/installer -o composer-setup.php
This command downloads the installer script and saves it as
composer-setup.php
in your current directory (
/tmp
). The
-s
flag makes
curl
operate in silent mode, and
-o
specifies the output filename.
Next, we need to verify the installer’s integrity to ensure it hasn’t been tampered with. You can find the latest installer hash on the
Composer website
. Make sure to check this link for the most current hash. For example, let’s say the current hash is
EXPECTED_CHECKSUM
. You would run the following command to verify:
php -r "if (hash_file('sha384', 'composer-setup.php') === 'EXPECTED_CHECKSUM') { echo "Installer verified"; } else { echo "Installer corrupt"; unlink('composer-setup.php'); } echo PHP_EOL;"
Important:
Replace
EXPECTED_CHECKSUM
with the actual SHA-384 hash you find on the Composer website. If the output is
Installer verified
, you’re good to go. If it says
Installer corrupt
, stop and re-download the installer, making sure you have the correct hash.
Once verified, we can run the installer to install Composer globally. We’ll use the
php
command to execute the installer script and specify the installation path as
/usr/local/bin/composer
:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
This command runs the installer, places the
composer
executable in
/usr/local/bin
(which is in your system’s PATH), and names it
composer
. The
sudo
is necessary because
/usr/local/bin
is a system directory.
Finally, let’s clean up the installer script we downloaded:
rm composer-setup.php
To verify that Composer has been installed correctly and is accessible system-wide, you can run:
composer --version
This command should output the installed Composer version. Congratulations, you’ve now got Composer installed globally on your Ubuntu server, which is a massive step towards getting Laravel up and running!
Step 3: Setting Up Your Web Server (Nginx)
Okay guys, we need a web server to serve our Laravel applications. While Apache is an option,
Nginx
is often preferred for its performance and efficiency, especially with PHP applications. We’ll set up Nginx to handle our requests. If you already have Apache installed and prefer to use it, you’ll need to disable it (
sudo systemctl stop apache2
and
sudo systemctl disable apache2
) before proceeding with Nginx, or configure Nginx to run on a different port.
First, let’s install Nginx:
sudo apt install nginx -y
Once Nginx is installed, it should start automatically. You can check its status with:
sudo systemctl status nginx
If it’s not running, you can start it with
sudo systemctl start nginx
and enable it to start on boot with
sudo systemctl enable nginx
.
Now, we need to configure Nginx to work with PHP-FPM (FastCGI Process Manager), which is how Nginx communicates with our PHP code. We installed PHP earlier, and the FPM package should have been installed along with it (
php8.1-fpm
). Let’s ensure the PHP-FPM service is running and enabled:
sudo systemctl status php8.1-fpm
sudo systemctl enable php8.1-fpm
If it’s not active, start it with
sudo systemctl start php8.1-fpm
.
Next, we need to create a new Nginx server block (virtual host) configuration file for our Laravel application. We’ll create a file named
laravel
in the
/etc/nginx/sites-available/
directory. You can use
nano
or your preferred text editor:
sudo nano /etc/nginx/sites-available/laravel
Now, paste the following configuration into the file.
Remember to replace
your_domain_or_ip
with your server’s domain name or IP address.
server {
listen 80;
listen [::]:80;
root /var/www/laravel/public;
index index.php index.html index.htm;
server_name your_domain_or_ip;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht \.svn /commands /storage /vendor /bootstrap {
deny all;
}
}
Let’s break down this configuration:
-
listen 80;: Tells Nginx to listen on port 80 for incoming HTTP requests. -
root /var/www/laravel/public;: Specifies the document root for your Laravel application. We’ll create this directory later. -
index index.php index.html index.htm;: Defines the files Nginx should look for when a directory is requested. -
server_name your_domain_or_ip;: Sets the domain name or IP address that this server block should respond to. -
location / { ... }: This block handles all requests.try_filesis crucial for Laravel as it tells Nginx to try to serve the requested file directly, then as a directory, and finally pass the request toindex.phpif none of the above are found. This is how Laravel’s routing works. -
location ~ \.php$ { ... }: This block handles requests for.phpfiles. It includes standard PHP FastCGI configurations and tells Nginx to pass the request to the PHP-FPM socket (unix:/var/run/php/php8.1-fpm.sock). Make sure the PHP version in the socket path matches the one you installed. -
location ~ /\.ht \.svn /commands /storage /vendor /bootstrap { deny all; }: This is a security measure to prevent direct access to sensitive files and directories that should not be publicly accessible.
Save the file (Ctrl+X, then Y, then Enter in
nano
).
Now, we need to enable this new server block by creating a symbolic link to it in the
sites-enabled
directory:
sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
It’s a good practice to remove the default Nginx configuration to avoid conflicts:
sudo rm /etc/nginx/sites-enabled/default
Before restarting Nginx, it’s wise to test the configuration for any syntax errors:
sudo nginx -t
If you see
syntax is ok
and
test is successful
, you’re good to go. Now, restart Nginx to apply the changes:
sudo systemctl restart nginx
You’ve successfully configured Nginx to serve your future Laravel applications. We’re getting closer, guys!
Step 4: Installing Laravel via Composer
Alright, we’ve got our server updated, PHP installed, Composer ready, and Nginx configured. The moment you’ve been waiting for: installing Laravel ! We’ll use Composer to create a new Laravel project.
First, let’s create a directory where your web applications will live. A common place is
/var/www/
. We’ll create a directory for our project, let’s call it
my-laravel-app
for now. Remember, the
root
directive in our Nginx configuration points to
/var/www/laravel/public
, so we’ll create a structure that matches that.
Let’s create the main application directory first:
sudo mkdir -p /var/www/laravel
Now, we’ll use Composer to create a new Laravel project directly inside this directory. We’ll use the
create-project
command. You can replace
laravel/laravel
with a specific version if you need one, but
laravel/laravel
will grab the latest stable version.
cd /var/www/laravel
sudo composer create-project laravel/laravel .
Explanation:
-
cd /var/www/laravel: Navigates us into the directory where we want to create the project. -
sudo composer create-project laravel/laravel .: This is the core command.create-projecttells Composer to create a new project from a package.laravel/laravelis the package name for the latest Laravel skeleton. The.at the end tells Composer to create the project in the current directory (/var/www/laravel).
Composer will now download Laravel and all its dependencies. This might take a few minutes depending on your internet speed.
Once Composer finishes, you’ll have all the Laravel files in
/var/www/laravel/
. However, our Nginx configuration expects the web-accessible files to be in a
public
subdirectory. Laravel’s structure already includes this! The
create-project
command creates the standard Laravel structure, including a
public
directory.
We need to ensure that the Nginx configuration points to the correct
public
directory. Our previous Nginx config was:
root /var/www/laravel/public;
This is correct for the structure that
composer create-project
creates. If you had created your project in
/var/www/myproject
and then wanted to use that, you’d adjust the Nginx config’s
root
directive and
location ~ \.php$
block to point to
/var/www/myproject/public
and
fastcgi_pass
accordingly.
Now, let’s adjust permissions. The web server (Nginx) and the PHP process need to be able to write to certain directories within your Laravel application, specifically the
storage
and
bootstrap/cache
directories, for logging, caching, and session handling.
We need to change the ownership of the
/var/www/laravel
directory to your user (or a specific web server user if you have one set up, but for simplicity, we’ll use the user that will manage the files and ensure Nginx can access them). Let’s assume your current user is
www-data
for the Nginx worker processes, but you’ll want to own the files to edit them. A common practice is to change ownership to your user, then give the web server group write permissions. For simplicity and common setups, we’ll often set ownership to
www-data
and grant permissions.
Let’s first ensure the Nginx user (
www-data
on Ubuntu) can write to the necessary directories. We’ll change the ownership of the entire Laravel directory to
www-data
:
sudo chown -R www-data:www-data /var/www/laravel
This ensures that the web server process has the necessary permissions to write logs, cache files, etc. You might need to adjust this based on your specific deployment strategy and user setup. For development, you might prefer to
chown
it to your own user and then
chmod
the
storage
and
bootstrap/cache
directories:
# Example for your user: Replace 'your_user' with your actual username
sudo chown -R your_user:www-data /var/www/laravel
sudo chmod -R 775 /var/www/laravel/storage
sudo chmod -R 775 /var/www/laravel/bootstrap/cache
We’ll stick with the
www-data:www-data
ownership for now, as it’s a common starting point for server setups.
Step 5: Finalizing and Testing Your Laravel Installation
We’re almost there, guys! We’ve installed Laravel, and our Nginx server is configured to point to its
public
directory. Now, let’s do a final check and test.
First, ensure your Nginx configuration is pointing to the correct root directory. In
/etc/nginx/sites-available/laravel
, the
root
directive should be set to
/var/www/laravel/public;
.
We also need to make sure that our application key is set. When Laravel is installed, it generates an application key in the
.env
file. If you ran
composer create-project
as root, the
.env
file might not exist or might have incorrect permissions. Let’s create a
.env
file and generate a key.
Navigate to your Laravel project directory:
cd /var/www/laravel
Copy the sample environment file to
.env
:
sudo cp .env.example .env
Now, generate the application key. This command will update the
APP_KEY
in your
.env
file.
sudo php artisan key:generate
This command should output
Application key set successfully.
. If you get a permission error, ensure the
www-data
user can write to the
.env
file (you might need to
sudo chown www-data:www-data .env
if you previously set ownership to your user).
Now, let’s adjust the ownership of the
.env
file back to
www-data
if it wasn’t already.
sudo chown www-data:www-data .env
With everything in place, it’s time to test! Open your web browser and navigate to your server’s IP address or domain name (e.g.,
http://your_domain_or_ip
).
If everything was set up correctly, you should see the default Laravel welcome page! This is a beautiful sight, right? It means your Laravel application is up and running on your Ubuntu 20.04 server and being served by Nginx.
Troubleshooting Common Issues:
-
502 Bad Gateway:
This often means PHP-FPM is not running or Nginx cannot communicate with it. Check the status of
php8.1-fpm(sudo systemctl status php8.1-fpm) and ensure thefastcgi_passdirective in your Nginx config matches the correct socket path. -
404 Not Found:
This could indicate an issue with your Nginx
rootdirective or Laravel’s routing. Double-check that therootpoints to/var/www/laravel/publicand thattry_filesis configured correctly in your Nginxlocation /block. -
Blank Page / Internal Server Error:
This is often caused by PHP errors. Check the Nginx error logs (
/var/log/nginx/error.log) and the Laravel logs (/var/www/laravel/storage/logs/laravel.log) for more details. -
Permission Denied:
This is a very common issue. Ensure that the
www-datauser (or the user your web server runs as) has write permissions for thestorageandbootstrap/cachedirectories. Runningsudo chown -R www-data:www-data /var/www/laravel(and ensuring the.envfile is also owned bywww-data) usually resolves this.
Congratulations, you’ve successfully installed Laravel on your Ubuntu Server 20.04! You’re now ready to start developing amazing web applications. Keep experimenting, keep coding, and happy developing, guys!