Laravel is a powerful PHP framework designed to make web development a smooth and enjoyable process. Coupled with Nginx, a high-performance web server, you can create robust and scalable web applications. This detailed guide will walk you through the steps to install Laravel PHP Framework with Nginx on Ubuntu 22.04 using root access.

Step 1: Initiating Instance Creation

  • To create a new instance, click on the blue “Create” button located in the top-right corner of the page.
  • Selecting Instance Type
  • You’ll be presented with options to create different types of resources. Click on “Add New Instance” to begin setting up a new virtual machine.
Shape.host Resources dashboard showing the Create menu and available instance templates.

Step 2: Choosing a Location

  • On the “Create New Instance” page, you’ll see a list of available locations. Select your preferred data center location. In this example, “Amsterdam, Netherlands” is highlighted.
Shape.host instance form with Amsterdam selected as the data center location.

Step 3: Selecting a Plan

  • Choose a plan that fits your needs. The screenshot shows various options ranging from $3.5 to $22 per month, with different specifications for CPU, memory, storage, and bandwidth.
  • Choosing an Operating System
  • Scroll down to the “Choose an image” section. You can select from various distributions such as AlmaLinux, CentOS, Debian, Fedora, FreeBSD, RockyLinux, Ubuntu, Windows, and Windows Server. In this case, Ubuntu 22.04 is selected.
Shape.host instance form with the $22 monthly plan and Ubuntu image selected.

Step 4: Authentication Method

Choose between SSH keys (recommended for better security) or a password for root access. The password option is selected in the image.

To complete the process, you would click the “Create Instance” button at the bottom of the page, which is not visible in the provided screenshots.

Shape.host instance form showing authentication, hostname and instance creation options.

Step 5: Connecting to Your Instance

  • Retrieve SSH Credentials:
  • Note the IP address of your newly created instance from the Shape.host dashboard.
  • Connect via SSH:
  • Open a terminal on your local machine.
  • Use the following command to connect to your instance:
    ssh root@your_instance_ip
  • Replace your_instance_ip with the actual IP address of your instance.

Before we begin, ensure you have:

  1. A fresh installation of Ubuntu 22.04.
  2. Root access to the server.

Step 6: Update Your Server

First, update your package lists to make sure you have the latest versions of your installed packages.

apt update && apt upgrade -y
Terminal showing APT package management commands and output.

Step 7: Install Nginx

Install Nginx, which will serve as our web server.

apt install nginx -y
Terminal showing APT package management commands and output.

Once installed, start and enable Nginx to run on boot.

systemctl start nginx
systemctl enable nginx
Terminal showing systemd service management commands or output.

You can check the status to ensure it’s running:

systemctl status nginx
Terminal showing systemd service management commands or output.

Step 8: Install PHP and Required Extensions

Laravel requires PHP and several PHP extensions. Install them with the following command:

apt install php-fpm php-cli php-mbstring php-xml php-bcmath php-mysql php-json php-curl php-zip php-gd php-pear -y
Terminal showing APT package management commands and output.

Step 9: Install Composer

Composer is a dependency manager for PHP, which you will need to install Laravel. Download and install Composer with the following commands:

cd ~
curl -sS https://getcomposer.org/installer -o composer-setup.php
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Terminal showing file and directory setup commands.

Verify the installation:

composer --version
Terminal showing composer commands or output during Install Composer.

Step 10: Install Laravel

Navigate to the web root directory and use Composer to create a new Laravel project.

cd /var/www
composer create-project --prefer-dist laravel/laravel my_laravel_app
Terminal showing file and directory setup commands.

Set the appropriate permissions for the Laravel directory:

chown -R www-data:www-data /var/www/my_laravel_app
chmod -R 755 /var/www/my_laravel_app
Terminal showing file ownership or permission commands.

Step 11: Configure Nginx

Create a new Nginx server block for your Laravel application. Open a new configuration file in Nginx:

nano /etc/nginx/sites-available/my_laravel_app
Terminal showing nano commands or output during Configure Nginx.

Add the following configuration to the file:

server {
    listen 80;
    server_name your_domain_or_IP;
    root /var/www/my_laravel_app/public;

    index index.php index.html index.htm;

    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 {
        deny all;
    }
}
Terminal showing the GNU nano editor and configuration text.

Enable the configuration by creating a symbolic link to sites-enabled directory:

ln -s /etc/nginx/sites-available/my_laravel_app /etc/nginx/sites-enabled/
Terminal showing the Nginx site configuration symlink command for Laravel.

Test the configuration to make sure there are no syntax errors:

nginx -t
Terminal showing Nginx configuration test output.

If the test is successful, reload Nginx to apply the changes:

systemctl reload nginx
Terminal showing systemd service management commands or output.

Step 12: Adjust PHP Settings

Open the PHP-FPM configuration file to adjust PHP settings for Laravel:

nano /etc/php/8.1/fpm/php.ini
Terminal showing nano commands or output during Adjust PHP Settings.

Ensure the following settings are configured appropriately (you can use Ctrl+W in nano to search for these directives):

file_uploads = On
allow_url_fopen = On
short_open_tag = Off
memory_limit = 256M
upload_max_filesize = 100M
post_max_size = 100M
max_execution_time = 300
Terminal showing the GNU nano editor and configuration text.

Restart PHP-FPM to apply these changes:

systemctl restart php8.1-fpm
Terminal showing systemd service management commands or output.

Step 13: Configure Laravel Environment

Navigate to your Laravel project directory and copy the .env.example file to .env:

cd /var/www/my_laravel_app
cp .env.example .env
Terminal showing file and directory setup commands.

Generate the application key:

php artisan key:generate
Terminal showing php commands or output during Configure Laravel Environment.

Step 14: Set Up Database (Optional)

If your application requires a database, you can set up MySQL or any other database supported by Laravel. Here’s a quick example using MySQL:

Install MySQL:

apt install mysql-server -y
Terminal showing APT package management commands and output.

Secure the MySQL installation:

mysql_secure_installation
Terminal showing the mysql_secure_installation command.

Log in to MySQL and create a database and user for Laravel:

mysql -u root -p
Terminal showing mysql commands or output during Set Up Database (Optional).

Inside the MySQL shell, run the following commands:

CREATE DATABASE laravel_db;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Terminal showing SQL database and user configuration commands.

Update your .env file to include your database credentials:

nano .env
Terminal showing nano commands or output during Set Up Database (Optional).
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=password
Terminal showing the GNU nano editor and configuration text.

Run the migrations to set up your database schema:

php artisan migrate
Terminal showing php commands or output during Set Up Database (Optional).

Accessing Your Application

At this point, you should be able to access your Laravel application by navigating to your server’s IP address or domain in your web browser.

If you’re looking for reliable and scalable hosting solutions for your Laravel applications, consider using Shape.host’s Linux SSD VPS services. They offer high-performance, fully managed cloud servers that ensure your applications run smoothly and efficiently. Check out Shape.host for more information on their offerings and how they can support your development needs.