Install Nginx + PHP + MySQL in Ubuntu 14.04
To build an Nginx + PHP + MySQL Environment Based on Ubuntu 14.04, run the following command lines:
1. because a large number of permission operations are required, you can temporarily escalate permissions to the root account for convenience.
Sudo su
2. Install apt source management tools and add nginx and php installation sources.
Apt-get install python-software-properties
Add-apt-repository ppa: nginx/stable
Add-apt-repository ppa: ondrej/php5
3. Update System Software
Apt-get update
4. Install mysql
Apt-get install mysql-server
Note: During installation, you are required to enter the password of the root account of MySQL.
5. install php and support mysql
Apt-get install php5 php5-fpm php5-mysql php-apc
6. Choose to install various functional modules of php based on actual needs (Baidu for details)
Apt-get install php-pear php5-dev php5-curl
Apt-get install php5-gd php5-intl php5-imagick
Apt-get install php5-imap php5-mcrypt php5-memcache
Apt-get install php5-ming php5-ps php5-pspell
Apt-get install php5-recode php5-snmp php5-sqlite
Apt-get install php5-tidy php5-xmlrpc php5-xsl
7. Install nginx
Apt-get install nginx
8. Configure php
Vi/etc/php5/fpm/php. ini
Find:; cgi. fix_pathinfo = 1
Change to: cgi. fix_pathinfo = 0
9. Configure nginx
Mkdir/www
Chmod 755/www
Create a/www directory and set the permission to 755
Vi/etc/nginx/sites-enabled/default
Find root/usr/share/nginx/html;
Change to: root/www;
Note: Set the root directory of the website to the/www directory. Do not write it in/www/format,
This habitual little cleanliness has brought unexpected troubles to me, which will be explained later.
Find index index.html index.htm;
Changed to index. php index.html index.htm;
Note: add index. php to the default index file.
Locate: location ~ . Php $ {*} Block
Make the following adjustments (the changes are annotated at the end of the line ):
Location ~ . Php $ {
Try_files $ uri = 404; # Add
Fastcgi_split_path_info ^ (. +. php) (/. +) $; # anti-Annotation
# NOTE: You shoshould 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; # uncomment
Fastcgi_index index. php; # anti-Annotation
Include fastcgi_params; # anti-Annotation
}
10. Solve the small bug of php-fpm and nginx
After performing the preceding steps, a small bug occurs between nginx and php-fpm:
The static page *. html on the website can be accessed normally, while the *. php file returns a 200 status code,
However, the page content actually output to the browser is blank.
In short, the reason is that nginx cannot correctly pass the *. php file address to php-fpm for parsing,
It is equivalent to receiving a request from php-fpm, but the request points to a non-existent file and returns an empty result.
To solve this problem, you need to change the default fastcgi_params configuration file of nginx:
Vi/etc/nginx/fastcgi_params
Add a line at the end of the file:
Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
For more information about this line, there are two parameters:
$ Document_root refers to the root directory of the website, that is, the root/www we just set earlier;
$ Fastcgi_script_name indicates the file names of each *. php file on the website (in fact, the relative path of the file)
These two are combined to form a complete php file path. For example, your website has a/test/script. php file,
The complete path that nginx passes to php-fpm is:/www/test/script. php
I wrote root/www/When configuring nginx, which is not a problem for nginx,
But when it passes the php file path to php-fpm, it will form:/www // test/script. php
This file address is invalid (the correct PHP file cannot be obtained), so a blank space is returned.
This problem is so concealed that I spent a lot of time viewing logs, searching for information, and adjusting settings.
11. Restart various services
Service php5-fpm (reload)
Service nginx reload
Reload configuration changes.
12. Test
Vi/www/index. php
Content:
<? Php echo phpinfo ();?>
Open your browser to access: http: // localhost. If everything is normal, the php environment information will be output, such:
Ubuntu 14.04 LTS install LNMP Nginx \ PHP5 (PHP-FPM) \ MySQL