Skip to main content

Command Palette

Search for a command to run...

Running laravel on a server

Updated
6 min read

Running laravel in a server

Prerequisite

  • Server with ssh access and Ubuntu OS

  • Open port 80(HTTP) and 443(HTTPS)

  • domain name linked with the server or public IP address of the server

Installations

1. Update and upgrade

First of all, let’s update and upgrade the server

sudo apt update && sudo apt upgrade -y

2. Installing Nginx

We will be using Nginx for the webserver so let’s install it.

sudo apt install nginx -y

Once you install the nginx, you can verify the installation of nginx by using the command

nginx -v

#Output
:'
  nginx version: nginx/1.20.1
'

Untitled

Also, you can verify the service is running by using the command

sudo systemctl status nginx

Untitled

If the Active: active is showing, you can now go to the IP address or domain of the server, and see the default welcome page of the nginx.

Untitled

3. Installing php

We will be using php8.0. Currently, the latest version is PHP is 8.1 but we will be still installing 8.0

  1. Preparing the system to install

     sudo apt install software-properties-common
     sudo add-apt-repository ppa:ondrej/php
     sudo add-apt-repository ppa:ondrej/nginx
     sudo apt update
    
  2. Installing php8.0

     sudo apt install php8.0-fpm
    

    Note

    Install the php-fpm module, but do not install php. If the php module is installed first, it configures the Apache server instead of NGINX. The php-fpm package contains all core modules for PHP.

  3. You can verify that the PHP is installed in your system by

     php -v
    
     # should have output like
    
     : 'PHP 8.0.15 (cli) (built: Jan 29 2022 07:24:52) ( NTS )
     Copyright (c) The PHP Group
     Zend Engine v4.0.15, Copyright (c) Zend Technologies
         with Zend OPcache v8.0.15, Copyright (c), by Zend Technologies
     '
    

    Untitled

  4. Installing other php dependencies required for laravel

     # here we are installing php8.0-mysql as we will be using MySQL as the database. you will need to install other package if you are using any other database
    
     sudo apt install php8.0-gd php8.0-mbstring php8.0-xml php8.0-bcmath php8.0-zip php8.0-curl php8.0-mcrypt php8.0-mysql -y
    

3. Running PHP

You have now PHP and nginx (webserver) in the system.

Let’s now change some configuration of nginx so that you can run .php file ie. serve the file from the server.

  1. First, let’s create a index.php file in /var/www folder.

     cd /var/www
    
     echo '<?php echo "Laravel deployment in 5 mins";' | sudo tee index.php > /dev/null
    

    If you go to your domain/ip address ie. [domain.name/index.php](http://domain.name/index.php) you will see 404 page rendered by nginx. As web server still doesn’t know how to serve php

  2. Now, you have to update the configuration of nginx.

     cd /etc/nginx/sites-available
     # open default file using nano or vim.
    
     # delete all the content of default and past the following configuration
     server {
         listen 80;
             server_name default_server;
         root /var/www;
    
         index index.html index.htm index.php;
    
         location / {
             try_files $uri $uri/ /index.php$query_string;
         }
    
         location ~ \.php$ {
             include snippets/fastcgi-php.conf;
             fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
          }
    
         location ~ /\.ht {
             deny all;
         }
    
     }
    

    After changing the configuration you can test if the configuration file is correct not by

     sudo nginx -t
    

    If the configuration file is current you should have output live above. If there are any issues fix the issues until you get the success output message.

    Now, the configuration file test is a success, you have to reload the nginx to reflect the new configuration.

     sudo systemctl restart nginx
    

    Now, when you go to your domain ie. [domain.name/index.phpyou](http://domain.name/index.phpyou) will get the output as shown below.

4. Installing composer

The "Composer" is the package manager of PHP. I guess you are quite familiar with it. Let’s install it on the server.

EXPECTED_CHECKSUM="$(wget -q -O - https://composer.github.io/installer.sig)"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"

if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
then
    >&2 echo 'ERROR: Invalid installer checksum'
    rm composer-setup.php
    exit 1
fi

php composer-setup.php --quiet
rm composer-setup.php
sudo mv ./composer.phar /usr/local/bin/composer

Now, check if the composer is installed on your server or not by

composer --version

Untitled

5. Adding laravel project

For now, we will just install a new copy of the laravel project directly using Composer.

composer create-project laravel/laravel your-project-name #create new laravel proejct
sudo cp -r your-project-name /var/www #copy file to /var/www

You now have to update the nginx config file where you have to change the entry file of the project

- root /var/www;
+ root /var/www/your-project-name/public

Now you have to change some permission of files inside the laravel project to actually run the project smoothly.

sudo chmod -R 755 /var/www/your-project-name/storage /var/www/your-project-name/bootstrap/cache
sudo chown -R www-data:www-data /var/www/your-project-name/storage /var/www/your-project-name/bootstrap/cache

Now if you go to your domain name, you will be greeted with the laravel default home page.

Untitled

6. Installing database

Today will we install MySQL and use it as our database.

sudo apt install mysql-server -y

you can check if MySQL is installed by

mysql --version
  1. No, you can't run a predefined default script of MySQL to change the default settings of MySQL and configure the user.

     sudo mysql_secure_installation
    

    Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters.

    From 2022, will get this error. (insert digital ocen link here) https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-20-04

7. Create a database, user, and password

we need a new database for the project. It’s wise to use a different user and password for the particular database which has access to only a given database.

sudo mysql
# you will be prompted to mysql console
create database laravel;

CREATE USER 'laravel_user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

GRANT ALL PRIVILEGES ON laravel.* TO 'laravel_user'@'localhost';

exit

Now you should be able to log in to the MySQL using new database

mysql -u laravel_user -p

show databases # you will see laravel database

Untitled

8. Updating a value in .env file of laravel to access database.

#/var/www/your-project-name/.env

- DB_DATABASE=laravel
- DB_USERNAME=root
- DB_PASSWORD=
+ DB_DATABASE=laravel
+ DB_USERNAME=laravel_user
+ DB_PASSWORD=password

now you can run the migration

php artisan migrate
php artisan db:seed

9. Adding authentication

We can add authentication to look if we have correctly added database to the project.

composer require laravel-ui

php artisan ui bootstrap --auth

then if you go to your server /register you will find the register form. Fill the form and click register, you will be registered and logged in. This shows your connection to dabase corretly

Congratulation, you have now deployed laravel.

P

I tried following the steps above to run Laravel on my server, but I encountered several issues, particularly during the MySQL configuration and running Laravel database migrations. Errors like "SET PASSWORD has no significance for user 'root'@'localhost'" made the process quite confusing. Additionally, manually setting up the .env file to connect the database required a lot of adjustments, which was time-consuming and prone to errors if something minor went wrong.

Fortunately, I came across Vultr's guide on installing MySQL on Ubuntu 20.04, which provided clear and detailed instructions to resolve MySQL configuration issues and seamlessly run Laravel. Moreover, using Vultr Managed Databases offered a much simpler solution, especially for avoiding manual setup of users and database permissions.

The Vultr guide helped me overcome the challenges I faced and made the Laravel deployment process much smoother and more efficient. If you're dealing with similar issues, I highly recommend using Vultr's services and their detailed guides for an optimized experience! 😊