nginx安装配置、Nginx支持php

来源:互联网 发布:自学编程用什么语言 编辑:程序博客网 时间:2024/05/16 10:33

认识Nginx

Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。

1. 安装pcre

该文件为正则表达式库。让nginx支持rewrite需要安装这个库。
请确保系统中已经安装了编译器(rpm -qa | grep gcc),如果没有安装请执行以下命令:

[root@lockey ~]# yum install gcc-c++

1.1 下载pcre并解压然后进行安装配置:

[root@lockey ~]# wget https://ftp.pcre.org/pub/pcre/pcre2-10.30.tar.gz[root@lockey ~]# gunzip pcre2-10.30.tar.gz [root@lockey ~]# tar xf pcre2-10.30.tar [root@lockey ~]# cd pcre2-10.30/[root@lockey pcre2-10.30]# ./configure 

1.2 编译安装pcre

[root@lockey pcre2-10.30]# make[root@lockey pcre2-10.30]# make install

2. 安装Nginx(源码编译安装)

首先建立nginx用户并加入相应的组中,后续的配置中会用到这些信息:

[root@lockey ~]# useradd -g  www www -s /sbin/nologin #创建nginx运行账户www并加入到www组,不允许www用户直接登录系统

2.1下载源码(版本尽量选择稳定版)

[root@lockey ~]# wget http://nginx.org/download/nginx-1.12.1.tar.gz

2.2 解压源码并进行配置

[root@lockey ~]# tar zxvf nginx-1.12.1.tar.gz[root@lockey ~]# cd nginx-1.12.1[root@lockey nginx-1.12.1]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module

注意:配置和安装过程中可能会出现一些依赖缺少的问题,为了程序能够正常安装运行,请先解决依赖问题!

2.3 编译安装

[root@lockey nginx-1.12.1]# make && make install

2.4 检查是否安装成功

[root@lockey ~]# cd  /usr/local/nginx/sbin[root@lockey sbin]# ./nginx -t

结果如下则基本安装成功:
这里写图片描述

2.5 配置防火墙80端口

   iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

2.6 服务的启动停止重启与测试

1)启动    #方法1    # /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf    #方法2    # cd /usr/local/nginx/sbin    # ./nginx2) 停止    #查询nginx主进程号    ps -ef | grep nginx    #停止进程    kill -QUIT 主进程号    #快速停止    kill -TERM 主进程号    #强制停止    pkill -9 nginx3) 重启(首次启动需:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf)    /usr/local/nginx/sbin/nginx -s reload4)测试    #测试端口    netstat -na | grep 80    #浏览器中测试    http://ip:80

结果如下则为正常启动:
这里写图片描述

3. 配置Nginx支持php

在配置Nginx支持php支持php之前需要对php-fpm有所了解:

PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的,可以在 http://php-fpm.org/download下载得到.PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用。新版PHP已经集成php-fpm了,不再是第三方的包了,推荐使用。PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置,比spawn-fcgi具有更多优点,所以被PHP官方收录了。在./configure的时候带 –enable-fpm参数即可开启PHP-FPM

3.1 编译安装php,并且启用php-fpm

下载以及编译安装的过程不再详述,这里只讲一个常见编译安装错误的解决方法:

configure: error: mcrypt.h not found. Please reinstall libmcrypt

其实也就是缺少依赖模块,但是这个模块一般的路子不好安装,需要下载源码进行编译安装

解决方法步骤:

wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gzgunzip libmcrypt-2.5.7.tar.gz tar -xf libmcrypt-2.5.7.tar cd libmcrypt-2.5.7/./configure --prefix=/usr/local/libmcrypt make && make install

在php的配置中需要指明启用php-fpm(此动作仅限已经集成php-fpm的新版PHP,老旧版本需要单独安装)

./configure --prefix=/usr/local/php --enable-fpm

3.2 配置php-fpm

第一步:将安装好的php目录下的php-fpm.conf.default模板配置拷贝一份当做php-fpm的配置文件

cd /usr/local/php/etccp php-fpm.d/www.conf.default www.confcp php-fpm.conf.default /etc/php-fpm.conf

第二步:编辑配置文件
vim /etc/php-fpm.conf
#以下内容中未注释为新加或者消注

[global]; Pid file; Note: the default prefix is /usr/local/php/var; Default Value: nonepid = run/php-fpm.pid   #########user = www   #########group = www   #########; Error log file

这里写图片描述

3.3 配置nginx对php的支持
vim /usr/local/nginx/conf/nginx.conf

修改内容部分如下:

 2 user  www www; #指明用户和组 43         location / {#指定网站根目录 44             root   /nginxweb; 45             index index.php  index.html index.htm;#添加php默认页 46         }65         location ~ \.php$ {##重要的配置 66             root           html; 67             fastcgi_pass   127.0.0.1:9000; 68             fastcgi_index  index.php; 69             fastcgi_param  SCRIPT_FILENAME /nginxweb/$fastcgi_script_name; #/nginxweb/为你自己的网站发布目录,我的为/nginxweb/ 70                 # /scripts$fastcgi_script_name; 71             include        fastcgi_params; 72         }

配置完成后在网站发布目录下新建一个php文件用来测试:

[root@lockey nginxweb]# cat index.php <?php    echo 'goooooooooooood';    echo phpinfo();?>[root@lockey nginxweb]# 

3.4 重启php-fpm与nginx

/usr/sbin/php-fpm/usr/local/nginx/sbin/nginx -s reload

注意如果在启动php-fpm时出现以下错误:

failed to load configuration file '/usr/local/php/etc/php-fpm.conf'原因为没有拷贝配置文件ERROR: [pool www] cannot get uid for user 'www'原因是没有添加用户,或者说配置中没有指定用户和组信息

3.5 访问http://你的服务器ip/index.php

这里写图片描述

4. Nginx配置虚拟主机

  虚拟主机是将一台服务器主机分成一台台“虚拟”的主机,每台虚拟主机都可以具有独立的域名,具有完整的Intemet服务器功能(WWW、FTP、Email等),同一台主机上的虚拟主机之间是完全独立的。从网站访问者来看,每一台虚拟主机和一台独立的主机完全一样。   利用虚拟主机,不必为每个要运行的网站提供一台单独的Nginx服务器或单独运行一组Nginx进程。虚拟主机提供了在同一台服务器、同一组Nginx进程上运行多个网站的功能。

Nginx实现虚拟主机有以下三种方法,但是我将只演示其中常用的基于域名的虚拟主机

  • Nginx基于IP的虚拟主机配置:同一台主机的不同ip对应不同的网站,ip占位在server_name
  • Nginx基于端口的虚拟主机配置:同一主机的不同端口对应不同网站
  • Nginx基于域名的虚拟主机配置:每一个server_name对应一个网站的发布目录

接下来我将以两个虚拟主机为例进行配置:

vim /usr/local/nginx/conf/nginx.conf

server {#虚拟主机 1        listen       80;        server_name  www.lockey.com;#主机名称,也即域名        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            root   /nginxweb;            #虚拟主机的发布目录            index index.php  index.html index.htm;        }        #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   html;        }        # 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 /nginxweb/$fastcgi_script_name;        # /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;        #}    }    server {#虚拟主机2        listen       80;        server_name  www.halo.com;#虚拟主机2的主机名,也即域名        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            root   /nginxweb1;            #虚拟主机2的网站发布目录            index index.php  index.html index.htm;        }        #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   html;        }        # 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 /nginxweb1/$fastcgi_script_name;        # /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;        #}    }    # another virtual host using mix of IP-, name-, and port-based configuration

配置完成后需要重载配置:

/usr/local/nginx/sbin/nginx -s reload

运行结果示例:

虚拟主机www.halo.com
这里写图片描述

虚拟主机www.lockey.com
这里写图片描述

原创粉丝点击