Deploy Laravel using Nginx in Ubuntu
Problem description
Laravel is the most popular web application development framework in PHP today. The number of start nodes on github far exceeds that of the second Symfony. Previously, when I used this framework for projects, I usually threw it into apache, then configure. the htaccess file removes the public words in the route to achieve the Pretty URLs effect. In the past two days, the firewall of each version has been improved and is ready to be deployed on azure. It is found that nginx was installed previously, in an environment like mysql, it took me a little time to study how to deploy it. Then I got this article. I am not talking about it:
Configure the environment
sudo apt-get install nginx php5-fpm php5-cli php5-mcrypt git
Nginx will be installed as the web server and some PHP tools will be installed here. git is installed to pull the code during later deployment.
Change PHP configuration
After the appeal component is installed, We need to configure it. First, we need to enable fpm/php. ini and change fix_pathinfo to 0.
sudo vim /etc/php5/fpm/php.ini
cgi.fix_pathinfo=0
The setting here is to prevent PHP from trying to execute scripts with similar names when the requested file is not there, so as to prevent attackers from deceiving PHP to execute code that should not be executed, finally, we need to enable the MCrypt extension explicitly and restart the php5-fpm service to reload to make the changes just now
sudo php5enmod mcrypt
sudo service php5-fpm restart
Configure Nginx
Below we need to configure nginx, which contains some paths. Here I use nginx installed with apt-get. If it is a manual compilation and installation, please find the path from here, first, we need to create a directory to place our laravel code. Here I will directly put it in/usr/share/nginx/laravel
sudo mkdir -p /usr/share/nginx/laravel
We need to configure our nginx
sudo nano /etc/nginx/sites-available/default
What you see here is probably like this.
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location /{
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/=404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/html;
#}
#location ~ \.php$ {
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
#fastcgi_index index.php;
#include fastcgi_params;
#}
#}
Replace it with the following configuration file, in which server_name must be replaced with your own domain name or ip address, the content in root is the directory of laravel we just created and has a public directory. Here, the public directory is used to remove the public in the laravel route in each request, so that the routing semantics is stronger.
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/laravel/public;
index index.php index.html index.htm;
server_name server_domain_or_IP;
location /{
try_files $uri $uri//index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
After all this work is done, we can basically complete the work. Deploy the prepared laravel program in the directory and open the bound domain name to see the effect as shown in Figure
Postscript
Someone asked me about the deployment on QQ today. In general, ftp can be used, sftp can be used at a level, or svn can be set up. However, since git is available, generally, I first create a project in git, develop push locally, and run pull on the test server. After the test is passed, run pull on the production server, this is enough to deal with most scenarios, but when building a distributed project, I usually write an automated script to complete the repetitive work for me, I will use docker when I am in a good mood at this stage.
Deploying Laravel 14.04 using Nginx on Ubuntu 5.0