# Installing and Configuring Laravel with Nginx on Ubuntu 24.04

> URL: https://www.atlantic.net/dedicated-server-hosting/installing-and-configuring-laravel-with-nginx-on-ubuntu-24-04/ | Published: 2024-01-10 | Updated: 2025-05-14 | Author: Hitesh Jethva

Laravel is an open-source PHP programming language framework based on the MVC architecture and the Symphony framework. It offers a set of tools to build modern PHP applications. Laravel is highly scalable, has elegant syntax and advanced features, and has built-in support for distributed cache systems, which help simplify web application development.

## Step 1 – Install LEMP Server

Laravel runs on a web server, uses MySQL as a database backend, and is written in PHP, so you will need a LEMP stack installed on your server. You can install all LEMP server components using the following command.

```
apt install nginx mysql-server php php-fpm php-mbstring php-mysql php-xml php-bcmath php-curl zip unzip -y
```

After installing the LEMP stack, start and enable all necessary services.

```
systemctl start nginx mysql php8.3-fpm 
systemctl enable nginx mysql php8.3-fpm
```

## Step 2 – Create a Database for Laravel

Next, you will need to create a database and user for Laravel.

First, connect to the MySQL with the following command.

```
mysql
```

Next, create a database and user for Laravel.

```
CREATE DATABASE laravel;
CREATE USER 'laravel_user'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL ON laravel.* TO 'laravel_user'@'%';
```

Next, flush the privileges and exit from the MySQL shell.

```
FLUSH PRIVILEGES;
EXIT;
```

## Step 3 – Install Laravel

In this section, we will install Laravel using Composer.

First, install the Composer with the following command.

```
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
chmod +x /usr/local/bin/composer
```

Next, navigate to the Nginx web root and create a Laravel project.

```
cd /var/www/html
composer create-project --prefer-dist laravel/laravel laravel
```

You will see the following output.

```
> @php artisan vendor:publish --tag=laravel-assets --ansi --force

   INFO  No publishable resources for tag [laravel-assets].  

No security vulnerability advisories found.
> @php artisan key:generate --ansi

   INFO  Application key set successfully.
```

Next, set proper permissions and ownership on the Laravel directory.

```
chown -R :www-data /var/www/html/laravel
chmod -R 775 /var/www/html/laravel
```

Next, change the directory to Laravel and generate PHP artisan.

```
cd laravel
php artisan key:generate
```

Next, run the migration to create the table.

```
php artisan migrate
```

Next, edit the environment variable file.

```
nano /var/www/html/laravel/.env
```

Define your Laravel database settings as shown below.

```
APP_URL=http://laravel.example.com
LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel_user
DB_PASSWORD=password
```

Save and close the file when you are done.

## Step 4 – Configure Nginx for Laravel

Next, you must create a Nginx virtual host configuration file for Laravel.

```
nano /etc/nginx/conf.d/laravel.conf
```

Add the following configuration.

```
server {
    listen 80;
    server_name laravel.example.com;
    root /var/www/html/laravel/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
```

Save and close the file, then edit the Nginx main configuration file.

```
nano /etc/nginx/nginx.conf
```

Add the following line after the line http {:

```
server_names_hash_bucket_size 64;
```

Save the file, then verify Nginx for syntax errors.

```
nginx -t
```

Output.

```
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
```

Next, restart the Nginx service to apply the changes.

```
systemctl reload nginx
```

## Step 5 – Access Laravel Web UI

At this point, Laravel is installed and configured with Nginx. Now, open your web browser and access the Laravel web UI using the URL **http://laravel.example.com.** You will see the Laravel sample page on the following screen.

![](https://www.atlantic.net/wp-content/uploads/2024/01/p1-1.png)

 

## Conclusion

Congratulations! You have successfully installed Laravel with Nginx on Ubuntu 24.04. You can now explore the Laravel features and deploy your PHP-based application using the Laravel framework. You can now deploy Laravel on [dedicated server hosting](https://www.atlantic.net/dedicated-server-hosting/) from Atlantic.Net!
