📅  最后修改于: 2023-12-03 14:43:45.597000             🧑  作者: Mango
Laravel is a popular PHP framework used for building web applications. Nginx is a web server that is known for its performance and scalability. When combined with PHP, these tools provide a powerful development environment for building modern web applications. In this article, we'll discuss how to get started with using Laravel with Nginx and PHP.
Before we dive into the details of setting up Laravel with Nginx and PHP, make sure the following requirements are met:
First, you need to create a new Laravel project. You can create a new project using the following command:
composer create-project --prefer-dist laravel/laravel myapp
This will create a new Laravel project in the "myapp" directory.
Next, you need to configure your application. You can do this by copying the .env.example
file to .env
:
cd myapp
cp .env.example .env
Then, generate the application key:
php artisan key:generate
Once you've generated the key, you can start the development server to test your application:
php artisan serve
The development server will start at http://127.0.0.1:8000. You can open this URL in your web browser to see your application.
Now, let's set up Nginx to serve our Laravel application.
First, create a new Nginx server block:
sudo nano /etc/nginx/sites-available/myapp
And add the following configuration:
server {
listen 80;
listen [::]:80;
root /var/www/html/myapp/public;
index index.php index.html index.htm;
server_name example.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
Replace example.com
with your own domain or IP address.
Save and close the file.
Now, create a symbolic link to enable the server block:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
Test the Nginx configuration:
sudo nginx -t
If the configuration is valid, reload Nginx:
sudo systemctl reload nginx
That's it! Now you have a Laravel application running on Nginx with PHP. You can continue developing your application and deploy it to a production server.
Remember to configure your server's security settings as well. For example, you can enable HTTPS by configuring a SSL certificate for your domain or IP address.
Note: This is only a basic guide to get you started. There are many more configurations and optimizations that you can make to improve the performance and security of your application.