CentOS 7安装nginx

CentOS 7安装nginx

参考网上其他文章做的

安装Nginx

我们从nginx官方的RPM源来安装一个预构建的稳定版本的nginx包。

rpm --import http://nginx.org/keys/nginx_signing.key 
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 
yum install nginx  

这样,nginx就安装上了,安装完成后,nginx不会自动启动。现在需要做的是让nginx自动启动,另外还要做些配置让其可以随着操作系统启动而启动。我们也需要在防火墙里打开TCP/80端口,以使得可以远程访问nginx的web服务。所有这些操作、设置都只需要输入如下命令就可实现。

systemctl start nginx 
systemctl enable nginx 
firewall-cmd --zone=public --add-port=80/tcp --permanent 
firewall-cmd --reload  

Nginx已经启动了,现在来测试nginx。nginx在CentOS7下的默认文档要目录是 /usr/share/nginx/html。默认的 index.html 文件一定已经在这目录下了。让我们检测下是否可以访问到这个测试 web 页,输入 http://nginx的ip地址/访问。

启动 PHP-FPM

systemctl start php-fpm 
systemctl enable php-fpm

配置LEMP,让Nginx支持PHP

首先,禁用随PHP包安装的httpd服务。

systemctl disable httpd

接下来,配置nginx虚拟主机,使得nginx可以通过PHP-FPM来处理PHP的任务。用文本编辑器打开/etc/nginx/conf.d/default.conf,然后按如下所示修改。

server { 
    listen       80; 
    server_name  localhost; 
    root /usr/share/nginx/html; 
    index index.php index.html index.htm; 
 
    #charset koi8-r; 
    #access_log  /var/log/nginx/log/host.access.log  main; 
 
    location / { 
    } 
 
    #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 { 
    } 
 
    # 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$ { 
        try_files $uri =404; #这个空格特别重要
        fastcgi_pass 127.0.0.1:9000; 
        fastcgi_index index.php; 
        fastcgi_param SCRIPT_FILENAME     $document_root$fastcgi_script_name; 
        include fastcgi_params; 
    } 
}

然后,配置PHP, 修改/etc/php.ini。

cgi.fix_pathinfo=0 # 有的文章该值为1,我试了不行
date.timezone = PRC

最后,测试nginx是否能处理PHP页面。在测试之前,请确保重启nginx和PHP-FPM。

systemctl restart nginx 
systemctl restart php-fpm

创建一个叫名叫test.php的文件,然后写入如下内容,并放入/usr/share/nginx/html/目录。

<?php 
phpinfo(); 
?>

打开浏览器,输入 http://IP地址/test.php
即可看到php的相关信息

posted @ 2016-12-14 19:47  yuenan  阅读(379)  评论(0)    收藏  举报