Install and configure the Nginx server + PHP development environment from scratch in Linux. _ PHP Tutorial

Source: Internet
Author: User
Tags mcrypt php development environment
Install and configure the Nginx server + PHP development environment from scratch in Linux ,. In Linux, the Nginx server and PHP development environment are installed and configured from scratch. Nginx is a lightweight HTTP server compiled by Russians and compiled in event-driven mode, therefore, there is a very good Linux environment from scratch to install and configure the Nginx server + PHP development environment,

Nginx is a lightweight HTTP server written by Russians and is written in event-driven mode. Therefore, Nginx has excellent performance and is also a highly efficient reverse proxy and load balancing service. It has the performance matching Lighttpd, and there is no Lighttpd memory leakage problem, and the mod_proxy of Lighttpd also has some problems and has not been updated for a long time.

Therefore, I plan to use it to replace Apache on a Linux server. However, Nginx does not support running in cgi mode because it can reduce some program vulnerabilities. Then we must use FastCGI to execute the PHP program.

The following describes how to successfully Configure Nginx + PHP5 FastCGI.

Install or compile Nginx

Install Nginx

You can download the source code package on the official homepage. Ubuntu 7.10 can be directly installed through apt or download the latest deb package from here:

sudo apt-get install nginx

If you need to compile your own data, make sure that you have a compiler and a PCRE library (for the rewrite module of Nginx. if you do not need this module, you can use it in configure. /configure-without-rewrite), the compilation method is as follows:

Wget http://sysoev.ru/nginx/nginx-0.5.34.tar.gztar zxvf nginx-0.5.34.tar.gzcd nginx-0.5.34. /configure # the default installation path is/usr/local/nginx. you can append -- prefix =/usr to/usrmake & make install # install requires the root permission.

After Ubuntu is installed, the file structure is roughly as follows:

  • All configuration files are under/etc/nginx, and each virtual host is already under/etc/nginx/sites-available.
  • Program file in/usr/sbin/nginx
  • Logs are stored in/var/log/nginx.
  • The startup script nginx has been created under/etc/init. d /.
  • The default virtual host directory is/var/www/nginx-default.

If you compile with the default configuration, put it 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 to see the effect (make sure there are no other services in use on port 80 ):

Run Ubuntu:

sudo /etc/init.d/nginx start

For more information, run:

/usr/local/nginx/sbin/nginx

Then you can check the effect through http: // localhost.

To configure automatic nginx running, add/usr/local/nginx/sbin/nginx to/etc/rc. local. Ubuntu can execute

update-rc.d nginx defaults

Install PHP5

As for how to install PHP on Linux, there are many articles, and even many platforms have ready-made software packages, so you do not need to compile them yourself.

1. install the php auxiliary package as follows:
Common packages 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

Extract

tar zxf tar -jxf zlib-1.2.3.tar.bz2

Go

 cd zlib-1.2.3 

Run later

./configure 

Then execute

make make install 

The other installation methods are the same.
2. install the php package after the auxiliary package is installed.
Procedure

tar -zxvf php-5.2.14.tar.gz && cd php-5.2.14

Same as above.

./configure --prefix=/usr/local/php5 

Add the modules to be loaded

./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 not all loaded modules.) press Enter to execute.
The configuration file of Php is php. ini.

One of the major advantages of PHP5 CGI mode is the built-in FastCGI Support. you only need to specify the bound address and port parameters to run it in FastCGI mode, as shown below:

php-cgi -b 127.0.0.1:9000

How can I configure it to run with nginx?

Configure PHP FastCGI for Nginx

Save the following content as a fastcgi_params file and save it under/usr/local/nginx/conf (Ubuntu can be saved under/etc/nginx ), he set basic environment variables for our FastCGI module:

#fastcgi_paramsfastcgi_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-redirectfastcgi_param REDIRECT_STATUS  200;

Pay special attention to the "fastcgi_script_name" line, the PHP-CGI especially needs this line of information to determine the location of the PHP file.

In addition, you need to open the cgi. fix_pathinfo option in the PHP-CGI configuration file (which is located in/etc/php5/cgi/php. ini on Ubuntu:

cgi.fix_pathinfo=1;

In this way, the SCRIPT_FILENAME variable can be used normally in php-cgi.

In the nginx configuration, configure the php file and execute it using the FastCGI process:

Server {index. php; root/usr/local/nginx/html; location ~ . *. Php $ {include/usr/local/nginx/conf/fastcgi_params; # set fastcgi_index index according to your saved path. php; fastcgi_pass 127.0.0.1: 9000; # configure the address and port bound to FastCGI }}

Notify Nginx to re-load the 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 in the root directory of the document. php, and contains "phpinfo ();". now let's look at http: // localhost/index. php should be able to see the debugging information of php.

Configure the php process

There are two problems with the php-cgi FastCGI running method directly (it seems that there should be a solution, if you know it, you can teach me ):

1. if the process crashes, it is difficult to configure a restart
2. low efficiency of a single process
Therefore, we can use Lighttpd's spawn-fcgi to control the running of processes. The method for getting spawn-fcgi is as follows:

Wget http://www.lighttpd.net/download/lighttpd-1.4.18.tar.bz2 # get Lighttpd source package tar-xvjf lighttpd-1.4.18.tar.bz2cd lighttpd-1.4.18. /configure # compile the makecp src/spawn-fcgi/usr/local/bin/spawn-fcgi # extract the spawn-fcgi program

Next 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 parameter description is as follows:

  • -F Specifies the location of the execution program of the process that calls FastCGI. it is set according to the PHP installed on the system.
  • -A is bound to the addr address.
  • -P Bind to port
  • -S Path bound to the unix socket
  • -C Number of FastCGI processes generated. the default value is 5 (for PHP only)
  • -P PID file path of the generated process
  • -U and-g FastCGI use the identity (-u user-g user group) to run. www-data can be used in Ubuntu, and other configuration based on the situation, such as nobody and apache.

Then we can add this line of code to the bottom of the/etc/rc. local file, so that the system can start the FastCGI process of PHP at the same time.

Articles you may be interested in:
  • View nginx apache mysql php compilation parameters in Linux
  • Install Memcache in CentOS 5.4 (Linux + Nginx + PHP + Memcached)
  • Set up high-performance WEB servers in Linux + Nginx + Php

Ingress, Nginx is a lightweight HTTP server compiled by Russians and written in event-driven mode. Therefore, it is very good...

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.