Linux install from zero start configuration Nginx Server +php development environment _php Instance

Source: Internet
Author: User
Tags bz2 install php mcrypt php development environment nginx server

Nginx is a very lightweight HTTP server written by the Russians, written in event-driven fashion, so it has very good performance and is also a very efficient reverse proxy, load balancing. It has the performance of matching lighttpd while there is no lighttpd memory leak problem, and lighttpd Mod_proxy has some problems and has not been updated for a long time.

So I'm going to use it instead of Apache to apply to Linux servers. However, Nginx does not support CGI operations because it can reduce some of the procedural vulnerabilities that are caused. Then we have to use the FastCGI method to execute the PHP program.

Here is the process I successfully configured Nginx + PHP5 fastcgi

First install or compile Nginx

Install Nginx

The source pack can be downloaded on the official home page. Ubuntu 7.10 can be installed directly via 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 that you already have compilers and pcre libraries (rewrite modules for nginx, if you don't need this module to use./configure–without-rewrite at configure time), 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 can append--prefix=/usr settings to/usr makes
&& make install # install requires root permissions

After Ubuntu installation, the file structure is roughly:

    • All configuration files are under/etc/nginx, and each virtual host is scheduled under/etc/nginx/sites-available
    • Program Files in/usr/sbin/nginx
    • The log is in the/var/log/nginx.
    • and a startup script has been created under/etc/init.d/nginx
    • The default virtual host's directory setting is in the/var/www/nginx-default

If you compile it with your default configuration, it is placed under/usr/local/nginx, and the following is the directory structure:

    • /usr/local/nginx/conf Configuration Directory
    • /usr/local/nginx/html the default Web site root directory
    • /usr/local/nginx/logs log and PID files directory
    • /usr/local/nginx/sbin Execution File directory

The following can start Nginx to see the effect (please make sure that 80 ports are not using any other service):

Ubuntu Please run:

Sudo/etc/init.d/nginx start

Please run:

/usr/local/nginx/sbin/nginx

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

To configure Nginx Autorun, you can add/usr/local/nginx/sbin/nginx to/etc/rc.local and Ubuntu can perform

UPDATE-RC.D Nginx Defaults

Install PHP5

As for how to install PHP on Linux, there are a lot of articles, and even many platforms have off-the-shelf packages, no need to compile themselves.

1. The first installation of the PHP helper package is as follows
The usual packages have

    • 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

Cases:

TAR-JXF zlib-1.2.3.tar.bz2

Extract

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

Enter the

 

After execution

 
 

And then execute

 
 

The rest of the installation methods are the same.
2. After the installation of the above auxiliary package, the installation of the PHP package
Steps

TAR-ZXVF php-5.2.14.tar.gz && CD php-5.2.14

Methods Ibid Execution

 
 

followed by a module 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- Enable-ftp--enable-zip--enable-mbstring--with-mcrypt=/usr/local/libmcrypt 

(These are loaded modules, not all) press ENTER to perform.
PHP's configuration file is php.ini.

One of the great advantages of PHP5 's CGI is the built-in support for fastcgi, which can be run as fastcgi by simply specifying the address and port parameters of the binding, as follows:

Php-cgi-b 127.0.0.1:9000

How do I configure it to run with Nginx?

Configure the Nginx PHP FastCGI

Save the following as a fastcgi_params file, saved under/usr/local/nginx/conf (Ubuntu can be saved under/etc/nginx), and he sets the 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 is built with--enable-force-cgi-redirect Fastcgi_param Redirect_status 200; 

Pay special attention to the "Fastcgi_script_name" line, where php-cgi specifically needs this 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 (this profile is located in/etc/php5/cgi/php.ini) on Ubuntu:

Cgi.fix_pathinfo=1;

This allows the php-cgi to normally use the script_filename variable.

Next, configure the PHP file in the Nginx configuration to use the fastcgi process to execute:

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 a index.php in the document root directory and include "Phpinfo ();" Content, now look at http://localhost/index.php should be able to see the debugging of PHP information.

Configuring the PHP Process

There are two problems with the fastcgi running directly using php-cgi (seemingly there should be a solution, if you know it can teach me):

1. If the process crashes, it is difficult to configure a reboot
2. Low efficiency of single process
Therefore, we can use the LIGHTTPD spawn-fcgi to control the running of the process. The methods for obtaining spawn-fcgi are 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/ Procedures for spawn-fcgi #取出spawn-fcgi

Here we can use spawn-fcgi to control php-cgi fastcgi process.

/usr/local/bin/spawn-fcgi-a 127.0.0.1-p 9000-c 5-u www-data-g www-data-f
The parameter meaning is as follows

    • -F <fcgiapp> Specifies the location of the executing program of the process that invokes the fastcgi, set according to the situation of the PHP installed on the system
    • -A <addr> bind to address addr
    • -P <port> bind to ports port
    • -S <path> path to a UNIX socket
    • -C <childs> Specifies the number of fastcgi processes generated, default to 5 (PHP only)
    • -P <path> Specifies the PID file path of the resulting process
    • -U and-G fastcgi use what identity (-u user-G user group) to run, Ubuntu can use Www-data, other according to the situation configuration, such as nobody, Apache, etc.

Then we can add this line of code to the bottom of the/etc/rc.local file so that when the system starts, it can also start the PHP fastcgi process.

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.