CentOS7部署轻量级web服务器nginx+php

    xiaoxiao2021-03-26  27

    版权说明 :《CentOS7部署轻量级web服务器nginx+php》于当前博客和乘月网属同一原创,转载请说明出处,谢谢。

    对于nginx早有耳闻,但是一直没用过,这段时间我的个人网站频繁出现死机,数据库也频繁异常关闭,考虑到服务器配置比较低且apache太耗资源,遂正式转投nginx的怀抱。

    运行环境:阿里云服务器

    CPU: 1核

    内存: 1024 MB

    操作系统: CentOS 7.2 64位

    CentOS内置yum库没有nginx包和依赖,咱自食其力,自己配个库:

     vim /etc/yum.repos.d/nginx.repo 复制粘贴以下内容:

    [nginx]

    name=nginx repobaseurl=http://nginx.org/packages/centos/$releasever/$basearch/gpgcheck=0

    enabled=1

    :wq!保存内容,清理和新生成yum缓存:

    yum clean all yum -y makecache

    安装nginx,php和php-fpm:

    yum -y install nginx php php-fpm

    安装完毕,开启nginx服务,这里注意一点,开启之前应关闭apache服务器(nginx和apache都是监听占用80端口导致端口冲突,没有安装或开启apache服务器请忽略 systemctl stop httpd.service 这一步):

    systemctl stop httpd.service systemctl start nginx systemctl enable nginx开启php-fpm服务: systemctl start php-fpm.service systemctl enable php-fpm.service编辑nginx配置文件,使其支持执行php脚本: vim /etc/nginx/conf.d/default.conf编辑以下内容(注意文本背景高亮区域为修改的内容,  为了起到备份作用这里复制了location ~ \.php$区块内容,同时大家也可以用来做对比):

    server {    listen       80;    server_name  localhost;    #charset koi8-r;    #access_log  /var/log/nginx/log/host.access.log  main;    location / {        root   /usr/share/nginx/html;        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   /usr/share/nginx/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  /scripts$fastcgi_script_name;    #    include        fastcgi_params;    #}     location ~ \.php$ {         root           html;         fastcgi_pass   127.0.0.1:9000;         fastcgi_index  index.php;         fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$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;    #}}

    为了简单化,这里不做网站根目录更改,虚拟主机,域名绑定等配置了,默认为主,能跑通就行。

    由上面的配置可知,网站的根目录为/usr/share/nginx/html。OK,咱做一个php网页跑一下,以最著名最简单的phpinfo代码为例:

    echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/index.php为防止index.php文件涉及权限问题,对/usr/share/nginx/html/目录下所有文件重新配置属主和读写权限: chown -R nginx:nginx /usr/share/nginx/html/ chmod -R 777 /usr/share/nginx/html/

    重新加载nginx配置:

    systemctl reload nginx

    OK,用ifconfig命令拿到主机ip,在浏览器上输入主机ip或网址访问index.php

    你的ip或网址/index.php

    以上测试跟apache+php测试差不多,如果出现的是php配置信息界面,说明nginx+php可以跑通了,部署完成。及时把index.php文件删掉,防止主机配置等相关信息泄露,被黑客或不法之徒有机可乘。

    如果没有出现php配置信息界面,检查下selinux和firewall两者是否开启,是否开放80端口,具体请参考 CentOS7增加或修改SSH端口号 这一篇文章。

    另外拓展 ------------------------------>

    1.很多朋友说上传文件会提示:“413 Request Entity Too Large”错误,那么只需要在做出以下配置:

    vim /etc/nginx/nginx.conf 找到到nginx.conf文件内容中的http{}段, 修改nginx上传文件大小限制配置(nginx默认给定的值太小了),直接在该段最后添加以下配置(具体数值视自身服务器情况而定):

    client_max_body_size 32M; client_body_buffer_size 512k;

    保存后执行:systemctl reload nginx 重新加载nginx配置就好了。

    2.关于nginx伪静态、固定链接造成的404错误(如,wordpress的固定链接),解决方案如下:

    需要在nginx的配置文件default.conf下的   location  /  {  }  模块添加一行规则“ try_files $uri $uri/ /index.php?$args ; ”,执行代码:

    vim /etc/nginx/conf.d/default.conf

    高亮区域为添加的配置规则:

        location / {        root   /usr/share/nginx/html;        index  index.php index.html index.htm;

            try_files $uri $uri/ /index.php?$args ;

        }

    保存后执行:systemctl restart nginx 重新运行nginx。

    转载请注明原文地址: https://ju.6miu.com/read-662222.html

    最新回复(0)