lnmp环境配置之安装配置Nginx与PHP教程
lnmp环境中nginx与php是一个重头戏了,很多朋友在配置这一步时都要折腾很多,在此小编也同样是折腾了,下面我整理了一篇lnmp环境中安装配置Nginx与PHP教程,希望例子可以帮助到大家.
安装Nginx的方式有很多种,这里我们还是编译源码进行安装,使用下列命令:
- $wgethttp://nginx.org/download/nginx-1.6.2.tar.gz
- $tar-zxvfnginx-1.6.2.tar.gz
- $cdnginx-1.6.2
- $./configure--prefix=/usr/local/nginx
- $make
- $sudomakeinstall
如果安装过程中出现如下错误:
- ./configure:error:theHTTPrewritemodulerequiresthePCRElibrary.
- Youcaneitherdisablethemodulebyusing--without-http_rewrite_module
- option,orinstallthePCRElibraryintothesystem,orbuildthePCRElibrary
- staticallyfromthesourcewithnginxbyusing--with-pcre=<path>option.
则需要先安装pcre:
$ sudo yum install pcre-devel
安装完成之后,我们的Nginx安装目录在/usr/local/nginx,接下来修改nginx的配置文件(/usr/local/nginx/conf/nginx.conf),使其能够处理php脚本.
- worker_processes1;
- events{
- worker_connections1024;
- }
- http{
- includemime.types;
- default_typeapplication/octet-stream;
- sendfileon;
- keepalive_timeout65;
- server{
- listen80;
- server_name_;
- root/vagrant;
- location/{
- indexindex.htmlindex.htmindex.php;
- }
- location/demo{
- indexindex.php;
- if(!-e$request_filename){
- rewrite^/demo/(.*)$/demo/index.php?$1last;
- break;
- }
- }
- error_page500502503504/50x.html;
- location=/50x.html{
- roothtml;
- }
- location~\.php${
- fastcgi_pass127.0.0.1:9000;
- fastcgi_indexindex.php;
- fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
- includefastcgi_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|grepphp-fpm
- root62211002:17?00:00:00php-fpm:masterprocess(/usr/local/php/etc/php-fpm.conf)
- nobody62226221002:17?00:00:00php-fpm:poolwww
- nobody62236221002:17?00:00:00php-fpm:poolwww
- vagrant62331623002:18pts/000:00:00grepphp-fpm
- $sudokill-USR26221
注意:-USR2参数为重启,-INT参数为关闭.
创建虚拟主机:
- [root@os11728httpd-2.2.22]#vi/usr/local/nginx/conf/vhosts/www_finet230_cn.conf
内容如下:
- server{
- listen8080;
- server_nameng.fine230.cnfinet85.cn;
- root/var/www/root/ng_finet230_cn;
- #激活/关闭自动索引
- autoindexon;
- #设定索引时文件大小的单位(B,KB,MB或GB)
- #默认为on,显示出文件的确切大小,单位是bytes。
- #改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
- autoindex_exact_sizeoff;
- #开启以本地时间来显示文件时间的功能。默认为关(GMT时间)
- #默认为off,显示的文件时间为GMT时间。
- #改为on后,显示的文件时间为文件的服务器时间
- autoindex_localtimeon;
- #charsetkoi8-r;
- location/{
- indexindex.htmlindex.htmindex.php;
- }
- #error_page404/404.html;
- #redirectservererrorpagestothestaticpage/50x.html
- #
- error_page500502503504/50x.html;
- location=/50x.html{
- root/var/www/root/ng_finet230_cn;
- }
- #proxythePHPscriptstoApachelisteningon127.0.0.1:80
- #
- #location~\.php${
- #proxy_passhttp://127.0.0.1;
- #}
- #passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000
- #
- #location~\.php${
- #roothtml;
- #fastcgi_pass127.0.0.1:9000;
- #fastcgi_indexindex.php;
- #fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
- #includefastcgi_params;
- #}
- #denyaccessto.htaccessfiles,ifApache’sdocumentroot
- #concurswithnginx’sone
- #
- #location~/\.ht{
- #denyall;
- #}
- #将客户端的请求转交给fastcgi
- location~.*\.(php|php5|shtml)?${
- #roothtml;
- fastcgi_pass127.0.0.1:9000;#这里指定了fastcgi进程侦听的端口,nginx就是通过这里与php交互的
- fastcgi_indexindex.php;
- fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
- includefastcgi_params;
- }
- #网站的图片较多,更改较少,将它们在浏览器本地缓存30天
- location~.*\.(gif|jpg|jpeg|png|bmp|swf)$
- {
- expires30d;
- }
- #网站会加载很多JS、CSS,将它们在浏览器本地缓存1小时
- location~.*\.(js|css)?$
- {
- expires1h;
- }
- }
- #anothervirtualhostusingmixofIP-,name-,andport-basedconfiguration
- #
- #server{
- #listen8000;
- #listensomename:8080;
- #server_namesomenamealiasanother.alias;
- #location/{
- #roothtml;
- #indexindex.htmlindex.htm;
- #}
- #}
- #HTTPSserver
- #
- #server{
- #listen443;
- #server_namelocalhost;
- #sslon;
- #ssl_certificatecert.pem;
- #ssl_certificate_keycert.key;
- #ssl_session_timeout5m;
- #ssl_protocolsSSLv2SSLv3TLSv1;
- #ssl_ciphersHIGH:!aNULL:!MD5;
- #ssl_prefer_server_cipherson;
- #location/{
- #roothtml;
- #indexindex.htmlindex.htm;
- #}//phpfensi.com
- #}
- server
- {
- listen8080;
- server_namestatus.ng.finet230.cn;
- location/{
- stub_statuson;
- access_logoff;
- }
- }
将/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-SHn65535
- root@os11728~]#/usr/local/nginx/sbin/nginx
停止Nginx:
- [root@os11728~]#/usr/local/nginx/sbin/nginx-sstop
- //或
- [root@os11728~]#/usr/local/nginx/sbin/nginx-squit
重启Nginx:
- [root@os11728~]#/usr/local/nginx/sbin/nginx-sreload
- //或
- [root@os11728~]#kill-HUP`cat/usr/local/nginx/logs/nginx.pid`
配置开机自动启动Nginx + PHP,代码如下:
[root@os11728 ~]# vi /etc/rc.local
在末尾增加以下内容:
- ulimit-SHn65535
- /usr/local/php/sbin/php-fpm
- /usr/local/nginx/sbin/nginx
热门评论