Installation configuration Nginx and PHP tutorial for LNMP environment configuration

Source: Internet
Author: User

There are many ways to install nginx, here we still compile the source code for installation, use the following command:

$ wget http://nginx.org/download/nginx-1.6.2.tar.gz
$ TAR-ZXVF nginx-1.6.2.tar.gz
$ CD nginx-1.6.2
$./configure--prefix=/usr/local/nginx
$ make
$ sudo make install

If the following error occurred during installation

./configure:error:the HTTP Rewrite module requires the PCRE library.
Can either disable the module by using--without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
Statically from the source with Nginx by using--with-pcre=<path> option.

You need to install Pcre first

$ sudo yum install Pcre-devel

After the installation is complete, our Nginx installation directory is in/usr/local/nginx. Next, modify the Nginx configuration file (/usr/local/nginx/conf/nginx.conf) to enable it to process the PHP script.

Worker_processes 1;

Events {
Worker_connections 1024;
}
HTTP {
Include Mime.types;
Default_type Application/octet-stream;
Sendfile on;
Keepalive_timeout 65;
server {
Listen 80;
server_name _;
Root/vagrant;

Location/{
Index index.html index.htm index.php;
}

Location/demo {
Index index.php;
if (!-e $request _filename) {
Rewrite ^/demo/(. *) $/demo/index.php?$1 last;
Break
}
}

Error_page 502 503 504/50x.html;
Location =/50x.html {
root HTML;
}

Location ~\.php$ {
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
Fastcgi_param script_filename $document _root$fastcgi_script_name;
Include Fastcgi_params;
}
}
}

Finally, when starting the nginx, you need to start the PHP-FPM first.

$ sudo/usr/local/php/sbin/php-fpm
$ sudo/usr/local/nginx/sbin/nginx

For Nginx reboot and shutdown operations, you can use the following command

$ sudo/usr/local/nginx/sbin/nginx-s [Reload|restart|stop]

And PHP-FPM, you need to first use Ps-ef|grep php-fpm to get the process ID of the master process, and then use KILL-USR2:
$ ps-ef|grep PHP-FPM

Root 6221 1 0 02:17? 00:00:00 Php-fpm:master Process (/usr/local/php/etc/php-fpm.conf)
Nobody 6222 6221 0 02:17? 00:00:00 Php-fpm:pool www
Nobody 6223 6221 0 02:17? 00:00:00 Php-fpm:pool www
Vagrant 6233 1623 0 02:18 pts/0 00:00:00 grep php-fpm

$ sudo kill-usr2 6221

Note: The-USR2 parameter is reset and the-int parameter is off.


Creating a virtual Host

[root@os11728 httpd-2.2.22]# vi/usr/local/nginx/conf/vhosts/www_finet230_cn.conf

The contents are as follows:

server {

Listen 8080;

server_name ng.fine230.cn finet85.cn;

ROOT/VAR/WWW/ROOT/NG_FINET230_CN;

#激活/Turn off automatic indexing

AutoIndex on;

#设定索引时文件大小的单位 (B,KB, MB or GB)

#默认为on, show the exact size of the file, the unit is bytes.

#改为off后, showing the approximate size of the file, in kilobytes or MB or GB

Autoindex_exact_size off;

#开启以本地时间来显示文件时间的功能. Default is Off (GMT time)

#默认为off, the file time displayed is GMT time.

#改为on后, the file time displayed is the file's server time

Autoindex_localtime on;

#charset Koi8-r;

Location/{

Index index.html index.htm index.php;

}

#error_page 404/404.html;

# REDIRECT Server error pages to the static page/50x.html

#

Error_page 502 503 504/50x.html;

Location =/50x.html {

ROOT/VAR/WWW/ROOT/NG_FINET230_CN;

}

# 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 127.0.0.1:9000

#

#location ~\.php$ {

# root HTML;

# Fastcgi_pass 127.0.0.1:9000;

# Fastcgi_index index.php;

# Fastcgi_param Script_filename/scripts$fastcgi_script_name;

# include Fastcgi_params;

#}

# Deny access to. htaccess files, if Apache ' s document Root

# concurs with Nginx ' s one

#

#location ~/\.ht {

# Deny All;

#}

#将客户端的请求转交给fastcgi

Location ~ *\. (php|php5|shtml)? $ {

#root html;

Fastcgi_pass 127.0.0.1:9000 #这里指定了fastcgi进程侦听的端口, Nginx is here to interact with PHP.

Fastcgi_index index.php;

Fastcgi_param script_filename $document _root$fastcgi_script_name;

Include Fastcgi_params;

}

#网站的图片较多, fewer changes, caching them locally in the browser for 30 days

Location ~ *\. (gif|jpg|jpeg|png|bmp|swf) $

{

Expires 30d;

}

#网站会加载很多JS, CSS, cache them in the browser local 1 hours

Location ~ *\. (js|css)? $

{

Expires 1h;

}

}

# 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 5m;

# ssl_protocols SSLv2 SSLv3 TLSv1;

# ssl_ciphers high:!anull:! MD5;

# ssl_prefer_server_ciphers on;

# location/{

# root HTML;

# index index.html index.htm;

#    }

#}

Server

{

Listen 8080;

server_name status.ng.finet230.cn;

Location/{

Stub_status on;

Access_log off;

}

}

All files and subdirectories under the/VAR/WWW/ROOT/NG_FINET230_CN directory are set to the users of www group www:

[root@os11728 ~]# chown-r WWW:WWW/VAR/WWW/ROOT/NG_FINET230_CN

startup and shutdown of 1.5. Nginx

Start Nginx

[root@os11728 ~]# Ulimit-shn 65535
[root@os11728 ~]#/usr/local/nginx/sbin/nginx

Stop Nginx

[root@os11728 ~]#/usr/local/nginx/sbin/nginx-s Stop
Or

[root@os11728 ~]#/usr/local/nginx/sbin/nginx-s quit

Restart Nginx

[root@os11728 ~]#/usr/local/nginx/sbin/nginx-s Reload

Or

[root@os11728 ~]# kill-hup ' cat/usr/local/nginx/logs/nginx.pid '

Configure boot Auto start Nginx + PHP

The code is as follows Copy Code

[root@os11728 ~]# vi/etc/rc.local

Add the following at the end:

Ulimit-shn 65535
/usr/local/php/sbin/php-fpm

/usr/local/nginx/sbin/nginx

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.