Laravel is a popular PHP framework, and applications developed using it are widely used for web development, but many people don't know how to deploy them to the server. In this article, we will learn how to deploy a Laravel application using simple steps.
Preparation
Before you begin, make sure you have installed a LAMP or LEMP stack (Linux, Apache or Nginx, MySQL, and PHP). Additionally, you need to install composer, which is a package manager for PHP.
Step 1: Get the laravel application
First, we need to get the source code of the laravel application. Laravel provides two methods: use the git clone command to clone the source code repository from GitHub or use the composer create-project command to download a new laravel application from packagist.org.
Use the git clone command to clone the source code library:
$ git clone https://github.com/laravel/laravel.git
Use the composer create-project command to download the new laravel Application:
$ composer create-project --prefer-dist laravel/laravel blog
Step 2: Set up the application
Next, we need to set up the laravel application. Open the command line, go to the application directory, and enter the following command:
$ cp .env.example .env
$ php artisan key:generate
The above command will copy .env. example file and rename it to .env, then generate a new key. Now we need to edit the .env file and set the relevant information of the database.
Step 3: Configure Apache or Nginx
In this step, we will configure the web server to correctly parse our Laravel application. Let's take Nginx as an example and create a new file in the /etc/nginx/sites-available/ directory:
$ sudo vi /etc/nginx/sites-available/myapp
Enter in the file The following:
server {
listen 80; server_name example.com; root /var/www/html/myapp/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.2-fpm.sock; fastcgi_split_path_info ^(.+.php)(/.+)$; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; }
}
The above configuration uses Nginx as the web server, listening on port 80 by default, and using the application's public directory as the root directory. Finally, restart Nginx for the changes to take effect:
$ sudo systemctl restart nginx
Step 4: Publish the application
Now, we can publish the Laravel application by following some simple steps. Go to the root directory of your application and run the following commands:
$ composer install --optimize-autoloader --no-dev
$ php artisan config:cache
$ php artisan route:cache
$ php artisan view:cache
The above command will optimize the autoloader, parse the application's configuration, routes and views, and actually cache them. This will speed up application loading.
Step 5: Set File and Directory Permissions
In order to ensure that your Laravel application runs properly and allows files to be written when you make any changes to it, you need to set the permissions on the directory and files. Go to the root directory of the application and enter the following command:
$ chown -R www-data:www-data /var/www/html/myapp
$ chmod -R 755 /var/www/html/myapp/ storage
$ chmod -R 755 /var/www/html/myapp/bootstrap/cache
The above command sets the ownership and access permissions of files and directories to the Nginx user and group.
Step 6: Test the application
Now we can test whether the laravel application has been successfully deployed. Open a web browser and enter your server's IP address or domain name, and you should be able to see your application's home page.
Conclusion
In this article, we explored how to deploy a Laravel application to a web server. We walk through the process in simple steps and explain what each step does. Deploying Laravel applications can have some challenges, especially for those who are not familiar with PHP frameworks and web server management. Therefore, in order for the application to run properly, it is recommended to follow all steps carefully to ensure that the Laravel application behaves as expected after deployment.
The above is the detailed content of How to deploy laravel. For more information, please follow other related articles on the PHP Chinese website!