Use Nginx to deploy Laravel under Ubuntu

Source: Internet
Author: User
Tags fpm mcrypt

Problem description

Laravel is the most popular Web application Development framework under PHP today, and the start number on GitHub is much more than the second symfony, which I used to do when I was using this framework as a project, usually in Apache. Then configure the. htaccess file to remove the public word from the route, to achieve the pretty URLs effect, this two days in the refinement of the various versions of the wall, ready to deploy on azure, the results found previously installed in the NGINX,MYSQL environment, So it took a little time to study how to deploy, then there is this article, nonsense less, on dry:

Configuring the Environment

 
   
  
  1. sudo apt-get install nginx php5-fpm php5-cli php5-mcrypt git

Nginx will be installed as a Web server, and some PHP tools will be installed, and git is installed to pull the replacement code for post-deployment.

Change PHP Configuration

After installing the appeal component, we need to do some configuration, first we need to open fpm/php.ini, to change the fix_pathinfo to 0

 
   
  
  1. sudo vim /etc/php5/fpm/php.ini
  2. cgi.fix_pathinfo=0

The setting here is to let PHP do not attempt to execute a similar name script when the requested file is absent, to prevent the attacker from tricking PHP into executing some code that should not be executed, and finally we need to explicitly enable the MCrypt extension and restart the PHP5-FPM service to reload so that the changes

 
   
  
  1. sudo php5enmod mcrypt
  2. sudo service php5-fpm restart

Configure Nginx

Below we want to configure Nginx, there are some paths, here I use Apt-get installed Nginx, if it is manually compiled installation, please self-seeking path, first we want to create a directory to place our Laravel code, here I put directly to/usr/share/ Nginx/laravel

 
   
  
  1. sudo mkdir -p /usr/share/nginx/laravel

Below we need to configure our Nginx

 
   
  
  1. sudo nano /etc/nginx/sites-available/default

That's probably what you see here.

  
 
  1. server {
  2. listen 80 default_server;
  3. listen [::]:80 default_server ipv6only=on;
  4. root /usr/share/nginx/html;
  5. index index.html index.htm;
  6. # Make site accessible from http://localhost/
  7. server_name localhost;
  8. location / {
  9. # First attempt to serve request as file, then
  10. # as directory, then fall back to displaying a 404.
  11. try_files $uri $uri/ =404;
  12. # Uncomment to enable naxsi on this location
  13. # include /etc/nginx/naxsi.rules
  14. }
  15. #error_page 404 /404.html;
  16. # redirect server error pages to the static page /50x.html
  17. #
  18. #error_page 500 502 503 504 /50x.html;
  19. #location = /50x.html {
  20. # root /usr/share/nginx/html;
  21. #}
  22. #location ~ \.php$ {
  23. #fastcgi_split_path_info ^(.+\.php)(/.+)$;
  24. # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
  25. # With php5-cgi alone:
  26. #fastcgi_pass 127.0.0.1:9000;
  27. # With php5-fpm:
  28. #fastcgi_pass unix:/var/run/php5-fpm.sock;
  29. #fastcgi_index index.php;
  30. #include fastcgi_params;
  31. #}
  32. #}

Need to replace it with the following configuration file, where server_name to replace your own domain name or IP, where root content is just the directory we created Laravel and a public directory, The purpose of the public directory here is to remove the public from the Laravel route we request each time, giving way to a more semantic

  
 
  1. server {
  2. listen 80 default_server;
  3. listen [::]:80 default_server ipv6only=on;
  4. root /usr/share/nginx/laravel/public;
  5. index index.php index.html index.htm;
  6. server_name server_domain_or_IP;
  7. location / {
  8. try_files $uri $uri/ /index.php?$query_string;
  9. }
  10. location ~ \.php$ {
  11. try_files $uri /index.php =404;
  12. fastcgi_split_path_info ^(.+\.php)(/.+)$;
  13. #With php5-fpm:
  14. fastcgi_pass unix:/var/run/php5-fpm.sock;
  15. fastcgi_index index.php;
  16. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  17. include fastcgi_params;
  18. }
  19. }

Finish these our work basically finished, in the directory to deploy the written Laravel program, open the bound domain name can see the effect of the

Postscript

Today, someone on the QQ asked me the deployment of the problem, in general can use FTP, in the advanced point can use SFTP, or build svn, but since the git, I usually first create a project on Git, the local development of the push up, pull down on the test server, test pass , pull down on the production server, this is enough to deal with most of the scenes, but in the construction of distributed projects I usually write an automated script to do the repetitive work for me, so that the recent period of good mood when I will use Docker.

Note: GitHub above the public project is free, but private projects to spend money, and these two days home to my home network is not even on GitHub, so domestic I recommend to do ' Hidden ' project when using Oschina git, the speed is quite good, and each account can create 1000 free items, public or private can be. This year's new projects have been put on the Oschina,. NET project also gave up TFS (because of the speed reason, sometimes disconnect), except for occasional conflicts vs can't solve the command line that needs to be on git, the experience is pretty good

Use Nginx to deploy Laravel under Ubuntu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.