It is difficult to build the nginx + php environment on FreeBsd. However, due to historical usage problems, there are still many articles about this experience on the Internet. With the gradual promotion of Ubuntu, it is necessary to study how to build the nginx + php environment under ubuntu to make it easy for friends who need it and to promote the popularization of Ubuntu. In fact, setting up an nginx + php environment in Ubuntu is much faster than that in FreeBsd, because in FreeBsd, I use ports for installation and need to compile it. In Ubuntu, I directly install apt-get install, the installation speed is fast. Let's start the installation!
1. Install nginx. Execute the following command and it will be completed soon. However, the default installation version of apg-get is 0.5.33.
Sudo apt-get install nginx
Default Configuration File Installation location:
Conf:/etc/nginx. conf
Bin:/usr/sbin/nginx
Vhost:/etc/nginx/sites-enable/default
Cgi-params:/etc/nginx/fastcgi-params
For example, create a virtual Server
Server {
Listen 80;
Server_name www.yourdomain.com; # change the domain name as needed
Access_log/var/log/nginx/home. ucenter. access. log; location /{
Root/var/www/yourdomain.com;
Index. php; location ~ . Php $ {
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME/var/www/yourdomain.com $ fastcgi_script_name;
Include/etc/nginx/fastcgi_params;
}
}
2. Install the php-cgi Module.
Execute sudo apt-get install php5-cgi
Default Configuration File Installation location:
Php-cgi:/usr/bin/php-cgi
Php5-cgi:/usr/bin/php5-cgi
Cgi config:/usr/bin/cgi/php. ini
Modify the cgi. fix_pathinfo data of the php. ini file to 1. The default value is 0 cgi. fix_pathinfo = 1. In this way, the SCRIPT_FILENAME variable can be used normally in php-cgi.
3. Installing spawn-fcgi is a lighttpd tool used to control php-cgi.
If the GCC compiling environment is not installed in the system, run the following command to install the build-essential toolkit before installing lighttpd:
Sudo apt-get install build-essential
Wget http://www.lighttpd.net/download/lighttpd-1.4.19.tar.gz
Tar-xvf lighttpd-1.4.19.tar.gz
Cd lighttpd-1.4.19/
Sudo apt-get install libpcre3-dev
./Configure-without-zlib-without-bzip2
Make
Sudo cp src/spawn-fcgi/usr/local/bin/spawn-fcgi
The cgi controller is installed.
4. Start the test system. Start fast_cgi:
Spawn-fcgi-a 127.0.0.1-p 9000-C 5-u www-data-g www-data-f/usr/bin/php-cgi
Note: the ip address and port must correspond to the cgi-pass in the nginx server.-C indicates opening several cgi processes.
Start nginx
Sudo/etc/init. d/nginx start
Well, if there is no error message, it indicates that the configuration is successful. Now write a phpinfo test!
Finally, attach the configuration file of my/etc/nginx/sites-enable/default. This configuration file enables the rewrite function.
Server {
Listen 80;
Server_name localhost; access_log/var/log/nginx/localhost. access. log; location /{
Root/var/www/nginx-default;
Index. php; if (-f $ request_filename/index.html ){
Rewrite (. *) $1/index.html break;
}
If (-f $ request_filename/index. php ){
Rewrite (. *) $1/index. php;
}
If (! -F $ request_filename ){
Rewrite (. *)/index. php;
}}
# Error_page 404/404 .html;
# Redirect server error pages to the static page/50x.html
#
Error_page 500 502 503 x.html;
Location =/50x.html {
Root/var/www/nginx-default;
}
# Proxy the PHP scripts to Apache listening on 127.0.0.1: 80
#
# Location ~ . Php $ {
# Proxy_pass http: // 127.0.0.1;
#}
# Pass the PHP scripts to FastCGI server listening on Fig: 9000
#
Location ~ . Php $ {
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME/var/www/nginx-default $ fastcgi_script_name;
Include/etc/nginx/fastcgi_params;
}
# Deny access to. htaccess files, if Apache's document root
# Concurs with nginx's one
#
# Location ~ /. Ht {
# Deny all;
#}
}
# Another virtual host using mix of IP-, name-, and port-based configuration
#
# Server {
# Listen 8000;
# Listen somename: 8080;
# Server_name somename alias another. alias;
# Location /{
# Root html;
# Index index.html index.htm;
#}
#}
# HTTPS server
#
# Server {
# Listen 443;
# Server_name localhost;
# Ssl on;
# Ssl_certificate cert. pem;
# Ssl_certificate_key cert. key;
# Ssl_session_timeout 5 m;
# Ssl_protocols SSLv2 SSLv3 TLSv1;
# Ssl_ciphers ALL :! ADH :! EXPORT56: RC4 + RSA: + HIGH: + MEDIUM: + LOW: + SSLv2: + EXP;
# Ssl_prefer_server_ciphers on;
# Location /{
# Root html;
# Index index.html index.htm;
#}
#}
How is it? Is it fast and convenient? Try it now. If you are planning to be a WordPress user, you can try it. nginx + php is more efficient than Apache + php!