Home > Backend Development > PHP Tutorial > Install and configure Nginx server from scratch under Linux PHP development environment, _PHP tutorial

Install and configure Nginx server from scratch under Linux PHP development environment, _PHP tutorial

WBOY
Release: 2016-07-12 09:02:30
Original
773 people have browsed it

Install and configure the Nginx server PHP development environment from scratch under Linux.

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
Copy after login

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权限
Copy after login

The file structure after Ubuntu installation is roughly as follows:

  • All configuration files are under /etc/nginx, and each virtual host has been arranged under /etc/nginx/sites-available
  • The program file is in /usr/sbin/nginx
  • The log is placed in /var/log/nginx
  • And the startup script nginx has been created under /etc/init.d/
  • The default virtual host directory is set to /var/www/nginx-default

If you compile it using the default configuration, it will be placed under /usr/local/nginx. The following is the directory structure:

  • /usr/local/nginx/conf configuration directory
  • /usr/local/nginx/html default website root directory
  • /usr/local/nginx/logs log and pid file directory
  • /usr/local/nginx/sbin execution file directory

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
Copy after login

Others please run:

/usr/local/nginx/sbin/nginx
Copy after login

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
Copy after login

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

  • zlib-1.2.3.tar.bz2
  • jpegsrc.v6b.tar.gz libpng-1.2.22.tar.bz2 libmcrypt-2.5.8.tar.gz
  • mhash-0.9.9.9.tar.gz mcrypt-2.6.8.tar.gz

Example:

tar -jxf zlib-1.2.3.tar.bz2
Copy after login

Unzip

tar zxf tar -jxf zlib-1.2.3.tar.bz2
Copy after login

Go to

 cd zlib-1.2.3 
Copy after login
Execute after


./configure 
Copy after login

Then execute

make make install 
Copy after login

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
Copy after login

The method is the same as above and executed

./configure --prefix=/usr/local/php5 
Copy after login

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 
Copy after login

(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

Copy after login

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;
Copy after login

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;
Copy after login

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绑定的地址和端口进行配置
  }
}
Copy after login

Notify Nginx to reload configuration:

kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
Copy after login

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的程序
Copy after login

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

  • -f Specifies the execution program location of the process that calls FastCGI, and sets it according to the PHP installed on the system
  • -a binds to address addr
  • -p Bind to port port
  • -s Path bound to unix socket path
  • -C Specifies the number of FastCGI processes generated, the default is 5 (only for PHP)
  • -P Specifies the PID file path of the generated process
  • What identity does -u and -g FastCGI use to run (-u user -g user group)? You can use www-data under Ubuntu. Others are configured according to the situation, such as nobody, apache, etc.

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.

Articles you may be interested in:

  • View the compilation parameters of nginx apache mysql php under Linux
  • Memcache installation steps under CentOS 5.4 (Linux Nginx PHP Memcached)
  • Linux Nginx Php sets up high-performance WEB server

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1084579.htmlTechArticleInstall and configure the Nginx server PHP development environment from scratch under Linux. Nginx is a very lightweight software written by Russians. HTTP server, written in an event-driven manner, so has very good...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template