Home > PHP Framework > Laravel > laravel deployment project

laravel deployment project

王林
Release: 2023-05-29 10:43:37
Original
1522 people have browsed it

Laravel is a modern PHP framework with a complete MVC architecture, object-oriented development model, powerful routing control, flexible ORM, rich tool libraries and template engines. It is used in Web application development. Very popular. In this article, we will introduce how to deploy a Laravel project on a Linux server.

Preparation work

Before deploying the Laravel project, you need to ensure the following conditions:

1. Server system: CentOS or Ubuntu and other mainstream Linux systems

2. Server environment: Nginx or Apache HTTP Server

3. Database: MySQL or PostgreSQL and other relational databases

4.PHP: PHP7.0 or higher, relevant extensions need to be installed

5. Version control: Git or SVN, to facilitate code management and deployment

6. Domain name: A bound domain name or IP address is required to facilitate access and testing of the website

After the above conditions are met, you can enter the deployment process of the Laravel project.

Step 1: Install Composer

Composer is a package manager for PHP, used to manage dependencies and auto-loading functions. Before deploying a Laravel project, you need to install Composer.

1. The following is how to install Composer using the curl command:

curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Copy after login

2. After the installation is completed, you can check whether the installation is successful through the composer command:

composer -v
Copy after login

Step 2: Clone the code

1. Use SSH to connect to the server and enter the directory where the project is deployed:

cd /var/www/
Copy after login

2. In the directory, execute the following command to clone the project code:

git clone https://github.com/your_git_repository.git
Copy after login

"your_git_repository" here represents the Git warehouse address of the project.

3. Enter the project directory and use Composer to install dependencies:

cd your_project_directory
composer install
Copy after login

The "your_project_directory" here indicates the name of the directory where the project is located.

Step 3: Configure environment variables

1. In the project directory, copy the ".env.example" file and rename it to ".env":

cp .env.example .env
Copy after login

2. Modify relevant configurations in the ".env" file, including database connection information, email services, etc. If necessary, you can also set the debugging mode, log output, etc. of the application. Save and exit when finished.

3. Generate a new key:

php artisan key:generate
Copy after login

This key is used to encrypt the generated tokens, cookies and other data.

Step 4: Configure the Web server

1. In the configuration file of Nginx or Apache HTTP Server, add a virtual host configuration, including domain name, directory and other information. Taking Nginx as an example, create a configuration file located in the "/etc/nginx/sites-available/" directory, such as "your_domain.conf":

server {
    listen 80;
    server_name your_domain.com;

    root /var/www/your_project_directory/public;
    index index.php index.html index.htm;

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

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

2. Link the configuration file to "/etc/ nginx/sites-enabled/" directory, enable virtual host:

sudo ln -s /etc/nginx/sites-available/your_domain.conf /etc/nginx/sites-enabled/
Copy after login

3. Reload Nginx configuration:

sudo service nginx reload
Copy after login

It should be noted that "your_domain.com" in the above configuration file and "your_project_directory" need to be replaced with the actual domain name and project directory name.

Step 5: Run migration and start queue

1. In the project directory, run the migration operation and create related database tables:

php artisan migrate
Copy after login

2. Start Laravel queue and process Asynchronous tasks:

php artisan queue:listen
Copy after login

To execute this command in the background, you can use the following methods:

nohup php artisan queue:listen &
Copy after login

Or write the startup command into the configuration file in the /etc/supervisor/conf.d directory and use supervisor. manage.

At this point, the deployment of the Laravel project is completed. Enter the domain name or server IP address in the browser to access the website. If you want to update the code, you only need to use the Git pull command to update the code in the warehouse.

Summary

Laravel is a powerful PHP framework. Applications developed using Laravel have greatly improved efficiency, performance and maintainability. This article introduces how to deploy a Laravel project on a Linux server, including steps such as installing Composer, cloning the code, configuring environment variables, configuring the web server, and running migrations. I hope this article can help readers in need to deploy Laravel projects.

The above is the detailed content of laravel deployment project. 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