Home > PHP Framework > Laravel > How to deploy Laravel to a Linux server

How to deploy Laravel to a Linux server

PHPz
Release: 2023-04-19 10:52:43
Original
1799 people have browsed it

Laravel is a PHP-based MVC framework that provides many excellent features and tools to make web application development easier and more efficient. For Laravel developers, deploying a Laravel application is a crucial step. Here, the author will introduce how to deploy Laravel to a Linux server.

1. Environment configuration

Before deploying the Laravel application, we need to ensure that the necessary software has been installed on the server. Here are the steps to install the necessary software:

1. Install PHP

Install PHP on the Linux server through the following command:

sudo apt install php-fpm php-mysql php-curl php-mbstring php-xml php-zip
Copy after login

2. Install the Web server

Laravel applications need to run on a web server, here we choose Nginx.

Install Nginx on the Linux server through the following command:

sudo apt install nginx
Copy after login

3. Install the database

Laravel supports a variety of databases, such as MySQL, PostgreSQL, SQLite, etc. Here we choose MySQL as the database.

Install MySQL on the Linux server through the following command:

sudo apt install mysql-server
Copy after login

2. Upload the Laravel application

After installing the necessary software, we need to upload the Laravel application to the server. File transfer can be done through FTP or using tools such as SCP.

Copy all Laravel files to the /var/www/laravel directory on the server.

3. Configuring Nginx

Configuring Nginx is a critical step, because Nginx needs to pass the request to the correct folder.

1. Set the root directory of the Laravel application in the Nginx configuration file.

Open the Nginx configuration file:

sudo nano /etc/nginx/sites-available/default
Copy after login

Copy the following code into the server block:

root /var/www/laravel/public;

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
}
Copy after login

2. Restart Nginx

Restart Nginx:

sudo service nginx restart
Copy after login

4. Configure the database

To enable the Laravel application to access the MySQL database, corresponding configuration is required.

Open the .env file in the root directory of the Laravel application:

nano /var/www/laravel/.env
Copy after login

Fill the following information into the .env file:

DB_HOST=localhost
DB_DATABASE=database_name
DB_USERNAME=username
DB_PASSWORD=password
Copy after login

Replace the database name, username, and password with Information you actually use.

5. Run the Laravel application

After completing the above steps, we have successfully deployed the Laravel application to the Linux server.

Enter the server's IP address or domain name in the browser to access the Laravel application.

Summary

Deploying Laravel to a Linux server requires a series of configuration steps, involving components such as Nginx, PHP and MySQL. This article covers the detailed steps from environment configuration to Laravel application upload and configuration. Hope this helps developers who need to deploy Laravel applications on Linux servers.

The above is the detailed content of How to deploy Laravel to a Linux server. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template