Ubuntu 14.04 Installation nginx+php

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/helinfeng/p/4219051.html

Build Nginx, PHP, and MySQL environments based on the latest Ubuntu 14.04(September 2014)
All of the following command-line operations:

1, due to the need for a large number of permissions operations, temporary elevation of privileges, using the root account

sudo su

2, install the APT source management tool, add Nginx and PHP installation source

Install python-software-propertiesadd-apt-repository ppa:nginx/stableadd- Apt-repository PPA:ONDREJ/PHP5

3, update the system software

Apt-get Update

4. Install MySQL

Install Mysql-server

Note: During the installation process, you will be asked to enter the password for the root account of MySQL.

5. Installation of PHP and support for MySQL

Install php5 php5-fpm php5-mysql PHP-APC

6, according to the actual needs, the optional installation of various types of PHP functional modules (specifically please Baidu)

Install php-pear php5-dev php5-curlaptinstall php5-gd php5-intl php5- imagickapt Install php5-imap php5-mcrypt php5-memcacheaptinstall php5-ming php5-PS php5- pspellapt Install php5-recode php5-snmp php5-sqliteaptinstall php5-tidy php5-xmlrpc php5-xsl

7. Installing Nginx

Install Nginx

8. Configure PHP

vi /etc/php5/fpm/php.ini
Found:; cgi.fix_pathinfo=1 instead of: cgi.fix_pathinfo=0

9. Configure Nginx

mkdir /wwwchmod755 /www

Create a/www directory set to 755 permissions

VI /etc/nginx/sites-enabled/default

Found it:root /usr/share/nginx/html;

Switchroot /www;

Description: Set the Web site root directory to the/www directory, and be careful not to write/www/this format,

This habitual little neat freak brought me an unexpected disturbance, and I went on to elaborate.

Found it:index index.html index.htm;
Switchindex index.php index.html index.htm;

Description: Add index.php to the default index file

Found: location ~ .php$ { * } chunks
Make the following adjustments (changes are commented at the end of the line):

 location ~ =404   ^ (. +.php) (/.+ ) $; #反注释 # # Note:you should have   cgi.fix_ pathinfo = 0;   " in   php.ini # # # with php5 -cgi alone: # fastcgi_p The  127.0 . 0.1 : 9000     -fpm:fastcgi_pass Unix: /var/run/php5-fpm.sock; #反注释 Fastcgi_index index.php; #反注释 include Fastcgi_params; #反注释}  

10, solve the php-fpm and Nginx small bug

Following the above steps, due to a small bug between Nginx and php-fpm, this phenomenon can be caused by:
The static page *.html in the website will be accessed normally, while the *.php file returns a 200 status code,
But the actual output to the browser page content is blank.

In short, the reason is that Nginx does not correctly pass the address of the *.php file to php-fpm to parse,
The equivalent of PHP-FPM received a request, but this request points to a nonexistent file, and returns an empty result.

In order to solve this problem, the Nginx default fastcgi_params configuration file needs to be changed:

VI /etc/nginx/fastcgi_params

Add a line at the end of the file:

Fastcgi_param script_filename $document _root$fastcgi_script_name;

For the contents of this line, say a few words, of which there are two parameters:

$document _root means the root directory of the website, that is, we have just set the root/www in front;
$fastcgi _script_name refers to each *.php file name on the website (in fact, the relative path of the files)
These two together form the complete PHP file path, such as your website has a/test/script.php file,
The full path that Nginx passes to PHP-FPM is:/www/test/script.php

I said in front of the configuration Nginx, written as root/www/; This is not a problem for nginx,
But when it passes PHP file paths to PHP-FPM, it forms:/www//test/script.php
This file address is invalid (not getting the correct php file), so there is a problem with the return blank.
The problem is so hidden that I spend a lot of time looking at log, finding information, and adjusting settings.

11, restart the services

Service php5-fpm reloadservice nginx Reload

Reload the configuration changes.

12. Testing

VI /www/index.php

Content:

echo phpinfo ();?>

Please open your browser to access: http://localhost If everything works, the PHP environment information will be output.

PathInfo and URL rewrite mode with thinkphp supported under Nginx

The following is transferred from: http://www.sundabao.com/nginx%E4%B8%8B%E6%94%AF%E6%8C%81thinkphp%E7%9A%84pathinfo%E5%92%8Curl-rewrite% e6%a8%a1%e5%bc%8f/

Solution Solutions

I was configured under sites (Vhost) under the/etc/nginx/sites-available/directory. Of course you can also configure directly in the/etc/nginx/nginx.conf.
Add the following code in the localhost/{} configuration:

#如果请求既不是一个文件, not a directory, execute the rewrite rule if (-e $request _filename) {        #地址作为将参数rewrite到index. php.         ^/(. *) $/index.php/$1;        #若是子目录则使用下面这句, change the subdir to a directory name.         ^/subdir/(. *) $/subdir/index.php/$1;}

The complete code is as follows:

Location/ {    /var/www;     file  Then Then fall back to   index.html    try_files $uri $uri//index.html;          # Uncomment to enable    Naxsion the This location/etc/nginx/naxsi.rules    if (-E $request _filename)    {            ^/phpparser/(. *) $/phpparser/index.php?s=$1 Last ;            break;    }}

Then add the following two lines in the localhost ~ \.php{} configuration column:

Fastcgi_split_path_info ^ (. +\.php) (. *) $;                             

The complete configuration is as follows

Location ~\.php$ {root/var/www; Try_files $uri=404; Fastcgi_split_path_info^ (. +\.php) (/.+) $;# # note:you should has"cgi.fix_pathinfo = 0;" inchphp.ini## # with PHP5-CGI alone: #fastcgi_pass127.0.0.1:9000; Fastcgi_param path_info $fastcgi _path_info;# # with PHP5-Fpm:fastcgi_pass Unix:/var/run/php5-Fpm.sock;    Fastcgi_index index.php; Include Fastcgi_params;}

Ubuntu 14.04 Installation nginx+php

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.