一、安装和启动 nginx
1.1、安装 nginx

[root@hexingxing ~]# yum install -y nginx
// ...

Installed:
nginx.x86_64 0:1.10.2-1.el6

Dependency Installed:
GeoIP.x86_64 0:1.6.5-1.el6                                             GeoIP-GeoLite-data.noarch 0:2016.07-1.el6
GeoIP-GeoLite-data-extra.noarch 0:2016.07-1.el6                        gd.x86_64 0:2.0.35-11.el6
geoipupdate.x86_64 0:2.2.1-2.el6                                       libXpm.x86_64 0:3.5.10-2.el6
libxslt.x86_64 0:1.1.26-2.el6_3.1                                      nginx-all-modules.noarch 0:1.10.2-1.el6
nginx-filesystem.noarch 0:1.10.2-1.el6                                 nginx-mod-http-geoip.x86_64 0:1.10.2-1.el6
nginx-mod-http-image-filter.x86_64 0:1.10.2-1.el6                      nginx-mod-http-perl.x86_64 0:1.10.2-1.el6
nginx-mod-http-xslt-filter.x86_64 0:1.10.2-1.el6                       nginx-mod-mail.x86_64 0:1.10.2-1.el6
nginx-mod-stream.x86_64 0:1.10.2-1.el6

Complete!

1.2.1、启动 nginx

[root@hexingxing ~]# service nginx start
Starting nginx: nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)
[FAILED]

// 启动失败,看错误提示描述 “ [::]:80 failed ”,到 nginx 配置里修改下 [::]:80 端口的监听

1.2.2、修改 [::]:80 端口的监听

[root@hexingxing ~]# vi /etc/nginx/conf.d/default.conf

#
# The default server
#

server {
listen       80 default_server;
# listen       [::]:80 default_server;
server_name  _;
root         /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location / {
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}

}

// 将 listen       [::]:80 default_server; 行注释,使其不生效
// 在这之前,正常的 Linux 操作习惯为:把 default.conf 文件备份后再修改

1.3、再次启动 nginx

[root@hexingxing ~]# service nginx start
Starting nginx:                                            [  OK  ]

// 启动 nginx 成功

1.4、设置 nginx 开机启动

[root@hexingxing ~]# chkconfig --list nginx
nginx           0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@hexingxing ~]# chkconfig nginx on
[root@hexingxing ~]# chkconfig --list nginx
nginx           0:off 1:off 2:on 3:on 4:on 5:on 6:off

1.5、验证 nginx 页面
至此,已经成功安装和启动 nginx,在浏览器上输入你的服务器 IP 即可打开你的 nginx 欢迎页面,但是在这之前你可能还需要配置防火墙和 SELINUX 才能访问。

[web view]

二、安装和配置 php

2.1 安装 php

[root@hexingxing ~]# yum install -y php
// ...

Installed:
php.x86_64 0:5.3.3-48.el6_8

Dependency Installed:
apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1     httpd.x86_64 0:2.2.15-55.el6.centos.2     httpd-tools.x86_64 0:2.2.15-55.el6.centos.2
php-cli.x86_64 0:5.3.3-48.el6_8            php-common.x86_64 0:5.3.3-48.el6_8

Complete!

2.2 安装 php-fpm
// PHP-FPM(FastCGI Process Manager) ,使 nginx 支持 php

[root@hexingxing ~]# yum install -y php-fpm
// ...

Installed:
php-fpm.x86_64 0:5.3.3-48.el6_8

Complete!

2.3 启动 php-fpm

[root@hexingxing ~]# service php-fpm start
Starting php-fpm:                                          [  OK  ]

2.4 设置 php-fpm 为开机启动

[root@iZuf6e4ja95z77dnvzmssrZ conf.d]# chkconfig --list php-fpm
php-fpm         0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@iZuf6e4ja95z77dnvzmssrZ conf.d]# chkconfig php-fpm on
[root@iZuf6e4ja95z77dnvzmssrZ conf.d]# chkconfig --list php-fpm
php-fpm         0:off 1:off 2:on 3:on 4:on 5:on 6:off

2.5 修改 nginx 支持 php

2.5.1、先将需要修改的文件备份

[root@hexingxing ~]# cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
[root@hexingxing ~]# ls /etc/nginx/conf.d/
default.conf  default.conf.bak  ssl.conf  virtual.conf

2.5.2、修改文件

#
# The default server
#

server {
listen       80 default_server;
# listen       [::]:80 default_server;
server_name  _;
root         /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location / {
index index.html index.htm index.php;
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}

# 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;
fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
include        fastcgi_params;
}

}

// 在以上文件中修改以下两处:
2.5.3、将默认的以下字段:

location / {
}

修改为:

location / {
index index.html index.htm index.php;
}

2.5.4、在最后一个 “}” 之前添加以下字段:

# 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;
fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
include        fastcgi_params;
}

注意: fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name; 字段中的 “/usr/share/nginx/html” 为你的默认或自定义 web 文件存放处,如果没有修改即是此位置。
注意: location ~ \.php$ {        root           html;        fastcgi_pass   127.0.0.1:9000; 处的 root           html; 为存放 web 文件的文件夹,可以写调用值函数或绝对值位置。

2.5.5、如果你不只一个配置文件,将字段:

listen       80 default_server;

修改为:

listen       80;

// 以上修改完成后,重启 php-fpm 和 nginx ,service php-fpm restart | service nginx restart

2.5.6、重名默认的 index.html 为 index.html.bak

[root@hexingxing ~]# mv /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.bak
[root@hexingxing ~]# ls /usr/share/nginx/html/
404.html  50x.html  index.html.bak  nginx-logo.png  poweredby.png

2.5.7、写个 php 的验证文件

[root@hexingxing ~]# echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/index.php
[root@hexingxing ~]# ls /usr/share/nginx/html/
404.html  50x.html  index.html.bak  index.php  nginx-logo.png  poweredby.png

3、测试 php 版本页面
打开默认的首页,如果显示为 php 版本页面,那说明已经配置成功。

4、配置文件参考
由于环境和配置以及版本不同,可能你参考本文配置后显示的结果不一样,以下为可能是你需要用到的配置默认或能帮助到你找到问题。

“/etc/nginx/conf.d/default.conf” 文件 1

server {
listen       80;
server_name  localhost;
#charset koi8-r;
#access_log  /var/log/nginx/log/host.access.log  main;
location / {
root   /etc/nginx/html;
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   /etc/nginx/html;
}
# 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;
fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
include        fastcgi_params;
}
}

“/etc/nginx/conf.d/default.conf” 文件 2

server {
listen 80;
server_name yourdomain.com;
location / {
root   /opt/app/yourdomain-dir;
index  index.php index.html index.htm;
}
# 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;
fastcgi_param  SCRIPT_FILENAME  /opt/app/yourdomain-dir$fastcgi_script_name;
include        fastcgi_params;
}
}

友情提示:本站所有文章,如无特殊说明或标注,均为何星星原创发布。与此同时,趋于近年来本站的文章内容频繁被他站盗用与机器采集,现已全局禁用网站文字内容操作,了解详情或转载文章请 点此 继续!
分类: 系统运维

0 条评论

发表回复

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用 * 标注