Home >Backend Development >PHP Tutorial >Install and configure Nginx server from scratch under Linux PHP development environment, _PHP tutorial
Nginx is a very lightweight HTTP server written by Russians. It is written in an event-driven manner, so It has very good performance and is also a very efficient reverse proxy and load balancing. It has performance that matches Lighttpd, and does not have the memory leak problem of Lighttpd. Moreover, Lighttpd's mod_proxy also has some problems and has not been updated for a long time.
So I plan to use it to replace Apache on Linux servers. However, Nginx does not support running in cgi mode because it can reduce some program vulnerabilities caused by this. Then we must use FastCGI to execute PHP programs.
The following is the process I used to successfully configure Nginx PHP5 FastCGI
First install or compile Nginx
Install Nginx
The source code package can be downloaded from the official homepage. Ubuntu 7.10 can be installed directly through apt, or you can download the latest deb package from here:
sudo apt-get install nginx
If you want to compile it yourself, you need to make sure you already have the compiler and PCRE library (rewrite module for Nginx. If you don’t need this module, you can use ./configure –without-rewrite during configure). The compilation method is as follows :
wget http://sysoev.ru/nginx/nginx-0.5.34.tar.gz tar zxvf nginx-0.5.34.tar.gz cd nginx-0.5.34 ./configure #默认配置安装路径为/usr/local/nginx 可以追加--prefix=/usr设置到/usr make && make install # install要求有root权限
The file structure after Ubuntu installation is roughly as follows:
If you compile it using the default configuration, it will be placed under /usr/local/nginx. The following is the directory structure:
You can start nginx below to see the effect (please make sure that no other services are using port 80):
Ubuntu please run:
sudo /etc/init.d/nginx start
Others please run:
/usr/local/nginx/sbin/nginx
Then you can see the effect through http://localhost/.
To configure the automatic operation of nginx, you can add /usr/local/nginx/sbin/nginx to /etc/rc.local, and Ubuntu can execute it
update-rc.d nginx defaults
Install PHP5
As for how to install PHP on Linux, there are many articles, and there are even ready-made packages on many platforms, so there is no need to compile it yourself.
1. First install the php auxiliary package as follows
Commonly used bags include
Example:
tar -jxf zlib-1.2.3.tar.bz2
Unzip
tar zxf tar -jxf zlib-1.2.3.tar.bz2
Go to
cd zlib-1.2.3Execute after
./configure
Then execute
make make install
The rest of the installation methods are the same.
2. After installing the above auxiliary packages, install the php package
Steps
tar -zxvf php-5.2.14.tar.gz && cd php-5.2.14
The method is the same as above and executed
./configure --prefix=/usr/local/php5
Add the modules that need to be loaded after
./configure --prefix=/usr/local/php5 --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql5 --with-freetype-dir --with-zlib --with-png-dir --with-iconv --with-libxml-dir --with-jpeg-dir --with-curl --with-gd --enable-ftp --enable-zip --enable-mbstring --with-mcrypt=/usr/local/libmcrypt
(These are loaded modules, not all) Press Enter to execute.
Php's configuration file is php.ini.
One of the major advantages of PHP5's CGI method is that it has built-in FastCGI support. You only need to specify the bound address and port parameters to run in FastCGI mode, as follows:
php-cgi -b 127.0.0.1:9000
How to configure it to run with nginx?
Configure Nginx for PHP FastCGI
Please save the following content as a fastcgi_params file under /usr/local/nginx/conf (Ubuntu can be saved under /etc/nginx), which sets basic environment variables for our FastCGI module:
#fastcgi_params fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; # PHP only, required if PHP was built with --enable-force-cgi-redirect fastcgi_param REDIRECT_STATUS 200;
Please pay special attention to the "fastcgi_script_name" line. PHP-CGI specifically needs this line of information to determine the location of the PHP file.
In addition, you need to turn on the cgi.fix_pathinfo option in the PHP-CGI configuration file (this configuration file is located in /etc/php5/cgi/php.ini on Ubuntu):
cgi.fix_pathinfo=1;
In this way, php-cgi can use the SCRIPT_FILENAME variable normally.
Next, in the nginx configuration, configure the php file to be executed using the FastCGI process:
server { index index.php; root /usr/local/nginx/html; location ~ .*.php$ { include /usr/local/nginx/conf/fastcgi_params; #请根据自己保存的路径进行设置 fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; #请根据自己的FastCGI绑定的地址和端口进行配置 } }
Notify Nginx to reload configuration:
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
Ubuntu users can use the init script: sudo /etc/init.d/nginx reload
Then start php-cgi -b 127.0.0.1:9000
Suppose you put index.php in the document root directory and include the content of "phpinfo();". Now look at http://localhost/index.php and you should be able to see the PHP debugging information.
Configure php process
There are two problems with the FastCGI running method using php-cgi directly (it seems that there should be a solution, if you know it, you can teach me):
1. Difficult to configure restart if process crashes
2. The efficiency of single process is low
Therefore, we can use Lighttpd's spawn-fcgi to control the running of the process. The method to obtain spawn-fcgi is as follows:
wget http://www.lighttpd.net/download/lighttpd-1.4.18.tar.bz2 #获取Lighttpd的源码包 tar -xvjf lighttpd-1.4.18.tar.bz2 cd lighttpd-1.4.18 ./configure #编译 make cp src/spawn-fcgi /usr/local/bin/spawn-fcgi #取出spawn-fcgi的程序
Now we can use spawn-fcgi to control the FastCGI process of php-cgi
/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php-cgi
The meaning of the parameters is as follows
Then we can add this line of code to the bottom of the /etc/rc.local file, so that the PHP FastCGI process can be started at the same time when the system starts.