Home > PHP Framework > Laravel > How to install and configure Laravel and Nginx on Ubuntu

How to install and configure Laravel and Nginx on Ubuntu

PHPz
Release: 2023-04-13 13:53:16
Original
808 people have browsed it

Laravel is a widely used PHP framework that many developers like to use to build efficient and maintainable web applications. Nginx is a web server with excellent performance that can provide stable services for Laravel. In this article, we will discuss how to install and configure Laravel and Nginx on Ubuntu operating system.

1. Install PHP and related extensions

Before installing Laravel, you need to install PHP and some related extensions. In Ubuntu systems, you can install it using the following command:

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

These extensions are required to run Laravel, and they will help your application be more efficient.

2. Install Composer

Composer is a package manager for PHP, which can help you manage and install Laravel dependencies. In Ubuntu systems, you can use the following command to install Composer:

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
Copy after login

With the above command, you can install Composer globally into the system.

3. Install Laravel

After installing Composer, you can use the following command to install Laravel:

composer global require laravel/installer
Copy after login

After installing Laravel, you can use the following command to create a new Laravel Project:

laravel new myproject
Copy after login

This will create a new project named "myproject" in the current directory. After the installation is complete, you can start the Laravel application using the following command:

cd myproject
php artisan serve
Copy after login

This will start the Laravel development server and you can visit http://localhost:8000 to view your application.

4. Install Nginx

Nginx is a web server with excellent performance, which can provide efficient and stable services for Laravel applications. In Ubuntu systems, you can use the following command to install Nginx:

sudo apt-get install nginx
Copy after login

After the installation is complete, you need to configure Nginx to use PHP-FPM to handle PHP requests. You can use the following command to open Nginx's default configuration file:

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

Then, add the following lines to the server block:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
Copy after login

After the addition is completed, press Ctrl X and enter "Y" Confirm to save the changes and press Enter to exit the nano editor.

Finally, you need to restart Nginx for the changes to take effect. You can use the following command to restart Nginx:

sudo systemctl restart nginx
Copy after login
Copy after login

5. Use Nginx as a web server for your Laravel application

After installing and configuring Nginx, you need to configure your Laravel application to use Nginx as your web server. You can use the following command to open the configuration file of your Laravel application:

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

Then add the following lines to the server block:

root /var/www/myproject/public;

index index.php index.html index.htm;

server_name example.com;

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

Depending on your project characteristics, you will need to adjust these lines . For example, you need to change "example.com" to your domain name or IP address.

After adding, press Ctrl X, enter "Y" to confirm saving changes, and then press Enter to exit the nano editor.

Finally, connect the new configuration file to the sites-enabled directory. This can be achieved using the following command:

sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
Copy after login

This will create a symbolic link pointing to the Laravel configuration file in the sites-available directory. , so that Nginx can read it.

Once completed, restart Nginx for the changes to take effect:

sudo systemctl restart nginx
Copy after login
Copy after login

Now, you can use Nginx to serve Laravel web by visiting http://example.com.

Summary

In this article, we discussed how to install and configure Laravel and Nginx on Ubuntu operating system. Since Laravel is written using PHP, you need to install PHP and related extensions. Additionally, you need to install Composer to manage and install Laravel's dependencies. Nginx is a web server with excellent performance that can provide stable and efficient services for Laravel applications. After installing and configuring Nginx, you need to configure your Laravel application to use Nginx as your web server. Once completed, you can easily build efficient and maintainable web applications in Laravel.

The above is the detailed content of How to install and configure Laravel and Nginx on Ubuntu. 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