lnmp环境中nginx与php是一个重头戏了,很多朋友在配置这一步时都要折腾很多,在此小编也同样是折腾了,下面我整理了一篇lnmp环境中安装配置Nginx与PHP教程,希望例子可以帮助到大家.
安装Nginx的方式有很多种,这里我们还是编译源码进行安装,使用下列命令:
$ 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
如果安装过程中出现如下错误:
./configure: error: the HTTP rewrite module requires the PCRE library.
You 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.
则需要先安装pcre:
$ sudo yum install pcre-devel
安装完成之后,我们的Nginx安装目录在/usr/local/nginx,接下来修改nginx的配置文件(/usr/local/nginx/conf/nginx.conf),使其能够处理php脚本.
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 500 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;
}
}
}
最后,启动Nginx时,需要先启动PHP-FPM.
$ sudo /usr/local/php/sbin/php-fpm
$ sudo /usr/local/nginx/sbin/nginx
对于Nginx的重启以及关闭操作,可以使用以下命令.
$ sudo /usr/local/nginx/sbin/nginx -s [reload|restart|stop]
而PHP-FPM,则麻烦一点,需要先使用ps -ef|grep php-fpm获取master process的进程ID,再使用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
注意:-USR2参数为重启,-INT参数为关闭.
创建虚拟主机:
[root@os11728 httpd-2.2.22]# vi /usr/local/nginx/conf/vhosts/www_finet230_cn.conf
内容如下:
server {
listen 8080;
server_name ng.fine230.cn finet85.cn;
root /var/www/root/ng_finet230_cn;
#激活/关闭自动索引
autoindex on;
#设定索引时文件大小的单位(B,KB, MB 或 GB)
#默认为on,显示出文件的确切大小,单位是bytes。
#改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
autoindex_exact_size off;
#开启以本地时间来显示文件时间的功能。默认为关(GMT时间)
#默认为off,显示的文件时间为GMT时间。
#改为on后,显示的文件时间为文件的服务器时间
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 500 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就是通过这里与php交互的
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#网站的图片较多,更改较少,将它们在浏览器本地缓存30天
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
#网站会加载很多JS、CSS,将它们在浏览器本地缓存1小时
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;
# } //phpfensi.com
#}
server
{
listen 8080;
server_name status.ng.finet230.cn;
location / {
stub_status on;
access_log off;
}
}
将/var/www/root/ng_finet230_cn目录下的所有档案与子目录的拥有者皆设为www群体的使用者www:
[root@os11728 ~]# chown -R www:www /var/www/root/ng_finet230_cn
1.5.Nginx的启动与关闭
启动Nginx:
[root@os11728 ~]# ulimit -SHn 65535
root@os11728 ~]# /usr/local/nginx/sbin/nginx
停止Nginx:
[root@os11728 ~]# /usr/local/nginx/sbin/nginx -s stop
//或
[root@os11728 ~]# /usr/local/nginx/sbin/nginx -s quit
重启Nginx:
[root@os11728 ~]# /usr/local/nginx/sbin/nginx -s reload
//或
[root@os11728 ~]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
配置开机自动启动Nginx + PHP,代码如下:
[root@os11728 ~]# vi /etc/rc.local
在末尾增加以下内容:
ulimit -SHn 65535
/usr/local/php/sbin/php-fpm
/usr/local/nginx/sbin/nginx。
大型站长资讯类网站! https://www.nzzz.com.cn