Deploy Laravel using Nginx in Ubuntu

Source: Internet
Author: User
Problem description Laravel is the most popular web application development framework in PHP today. The number of start on github far exceeds that of the second Symfony. In the past, when I used this framework as a project, I usually threw it into apache, then configure. the htaccess file removes the public words in the route to achieve the PrettyURLs effect. In the past two days, we have improved the walls of various versions and planned to deploy them on azure. We found that nginx was previously installed, in an environment like mysql

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

  1. 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.

  1. sudo vim /etc/php5/fpm/php.ini
  2. 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

  1. sudo php5enmod mcrypt
  2. 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

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

We need to configure our nginx

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

What you see here is probably like this.

  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. #}

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.

  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. }

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 http://www.linuxidc.com/Linux/2015-08/121986.htm with Nginx on Ubuntu 5.0

For more information about Ubuntu, see Ubuntu special page http://www.linuxidc.com/topicnews.aspx? Tid = 2

This article permanently updates the link address: Http://www.linuxidc.com/Linux/2015-08/121988.htm

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.