[乐意黎转载]Centos 7 nginx 服务安装及配置

前言

Nginx 跟 Apache 的作用差不多,都是提供 WEB 服务,但Nginx 相对于 Apache 来说,在性能上有更好的体现,而 Apache 有稳定性方面会比Nginx 好一些,所以要用哪一个,自己喜欢就好。下面就给大家分享Nginx 的安装及配置方法。如果你觉得下面记录的东西有点乱,你可以直接看本文后面的总结也是可以的,更省时,更省心。我在这里

小插曲

如果你直接执行命令

 
 
# yum install Nginx

你会得到这样的提示

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.yun-idc.com
* extras: mirrors.tuna.tsinghua.edu.cn
* rpmforge: mirrors.neusoft.edu.cn
* updates: mirrors.tuna.tsinghua.edu.cn
No package Nginx available.
Error: nothing to do

No package Nginx available.(找不到Nginx 安装包),所以我们得配置下,让系统自动去帮你去下载,执行如下代码

# vim /etc/yum.repos.d/Nginx.repo

如果没有这个文件(一般没有),系统会自动为你新建一个,按下键盘i进入编辑模式,复制下面的内容粘贴到文件里,按Esc退出编辑模式,按 :wq 保存退出

  
  
  • [Nginx]name=Nginx repobaseurlhttp://Nginx.org/packages/centos/$releasever/$basearch/gpgcheck=0enabled1

    还果你的系统是centos 7.2以上的,那么你就直接执行如下命令行就可以了

    # yum install Nginx

    Nginx 安装配置

    现在直接输入下面的命令行进行安装,你就会如入无人之境,一路畅通无阻。

    # yum install Nginx

    安装记录

    下面就是 Nginx 的安装过程的全记录

    
      
      
  • root@niaoyun53098 ~]# yum install NginxLoaded plugins: fastestmirrorNginx | 2.9 kB 00:00/7/x86_64primary_db 1401Loading mirror speeds from cached hostfile* base mirrors.yun-idccom extrastunatsinghuaeducn rpmforgeneusoft updatescnResolving Dependencies-->Running transaction check--->Package Nginxx86_64 11.10.-1.el7.ngx will be installedFinishedDependencyResolutionDependenciesResolved================================================================================ArchVersionRepositorySizeInstallingNginx x86_64 Nginx 640 kTransactionSummaryInstall1PackageTotal download sizeInstalled size2.1 MIsthis ok ydN]: yDownloading packages1.el7.ngxrpm 0103 transaction check transaction test test succeeded transactionInstalling ----------------------------------------------------------------------Thanksforusing!Please find the official documentation Nginx here: http//Nginx.org/en/docs/Commercial subscriptions Nginx are available on//Nginx.com/products/Verifying1Installed1.el7.ngxComplete!

    至此 Nginx 已经安装完成,如果你觉得上面的代码太多太乱,也不要紧我为你精炼出来

    安装详情

    # yum install Nginx

    不多久你要你输入 y 确认完成安装

      
      
  • y

    输入完 y 后坐等 “Complete!”就行了。

    浏览器输入主机IP测试,但显示无法访问此网页,为什么?因为你忘记了启动 Nginx 服务了,就下面的一行命令

    # systemctl start Nginx.service

    启动服务之后,你刷新下页面就会出现如下贺电:

    Welcome to Nginx!

    If you see this page,the Nginx web server is successfully installed and working. Further configuration is required.

    For online documentation and support please refer to Nginx.org.
    Commercial support is available at Nginx.com.

    Thank you for using Nginx.

    配置命令

    随服务器启动

    # systemctl enable Nginx.service

    重启 Nginx 服务

    # systemctl restart Nginx.service

    停止 Nginx 服务

    # systemctl stop Nginx.service

    查看 Nginx 运行状态

    # systemctl status Nginx.service

    到这里,你就可以使用 Nginx 服务了。你可以通过如下命令在站点根目录下新建一个index.html文件站点认会有一个,覆盖就好)

    # echo "我是站点认根目录下的测试页面" >> index.html

    浏览器输入 IP 访问时,页面出现乱码了,这是什么原因呢?是我们没有给页面设置编码格式,因为一般页面都会有形如

    
     
     <!DOCTYPE html><html lang="en"><head><Metacharset"UTF-8"<title>Nginx 配置</title></head><body>我是站点认根目录下的测试页面</body></html>

    其中<Meta charset=”UTF-8″> 就是声明页面编码格式。我们先找到刚才编辑的index.html

    # vi/usr/share/Nginx/html/index.html

    i进入编辑模式,先清空刚才的文字,然后把上面一段 HTML 代码复制到 index.html 就可以了。刷新浏览器页面时就不会出现乱码了。

    Nginx 认的根目录为/usr/share/Nginx/html,如果你不喜欢你可以更改成自己设定的目录,例如:/home/www/Nginx.yunkus.com

    1.创建站点目录

    # mkdir -p /home/www/Nginx.yunkus.com

    目录创建好后,我们可以在站点目录里新建一个index.html 作为测试。

    # echo "我是 Nginx 自定义站点根目录" >> /home/www/Nginx.yunkus.com/index.html

    接下来我们就得配置下 Nginx配置文件

    修改配置文件前,我们得先复制一份用于配置的文件

    # cp /etc/Nginx/conf.d/default.conf /etc/Nginx/conf.d/Nginx.yunkus.com.conf

    复制完成后,我们就得去配置下Nginx.yunkus.com.conf (这个文件名可以随便到,只要自己能知道里面是什么就行)了

      
      
  • vi etcconfyunkusconf

    初次打开后,你会看到如下代码

    
     
     server { listen 80; server_name localhost #charset koi8-r;#access_log /var/log/Nginx/log/host.access.log main; location  root usrsharehtml index indexhtml indexhtm}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html# error_page 50050250350450xhtml # 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# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.PHP;# fastcgi_param SCRIPT_FILENAME /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_name:主机名。如果你没有绑定域名,或者只是想测试下,那么server_name localhost; 你可以不用动它,接着对location /{ ….. }里面的内容进行修改修改root /home/www/Nginx.yunkus.com;如果你网站首页是 index.PHP,那么你还得在下面这里添加认打开的文件格式location /{ ..... }追加 index.PHP,配置完成之后如下,如果你想把404页面的根目录也改成/home/www/Nginx.yunkus.com的话,你只需要在对应的location = 50x.html{ ….. }里重复上面的步骤就可以了。

     server_name yunkushomewww}

    但可能还无法成功访问,可能你还得修改下另一个配置文件

    # vi /etc/Nginx/Nginx.conf

    定位到倒数第二行include /etc/Nginx/conf.d/*.conf;

    
      
      
  • user Nginxworker_processes error_log /varlogerrorlog warnpid runpidevents worker_connections 1024;http include mimetypes default_type applicationoctetstream log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"' access_log accesslog main sendfile on#tcp_nopush on; keepalive_timeout 65#gzip on;d/*.conf;}

    include /etc/Nginx/conf.d/*.conf;改成include conf.d/*.conf;,完成这一步后,再试试,应该就没问题了。如果还访问不了,可以是因为防火墙没有添加 80 端口规则。

    # vi /etc/sysconfig/iptables

    i进入编辑模式

      
      
  • A INPUT p tcp m state --state NEW m tcp dport 22j ACCEPT

    后面追加一行

    80j ACCEPT

    加完后,按Esc退出编辑模式,输入:wq保存退出。接着记得重启下防火墙服务

    # systemctl restart iptables.service
    备注:

    文章中涉及到目录操作的都是先 cd 到根目录后再执行命令行的,这样只是为了让你更加直观的看到各个目录的相对位置。

    更新于:21:41 2017/2/7

    Nginx配置多个虚拟主机

    花了这么多钱买个服务器肯定不想让它闲着,比如:配置多个虚拟主机。多个虚拟主机什么意思?就是你可以放多个网站。现在假设我们安装的 Nginx 版本为1.10.2PHP的版本为7.0.15,我们就可以切换到/etc/Nginx/conf.d/目录,然后在此新建一个文件touch Nginx.yunkus.com.conf, 这个文件名随便你起只要是.conf后缀就行,文件弄好之后vimNginx.yunkus.com.conf,打开文件后按i进入编辑模式,把下以这段代码复制进去。Esc即出编辑模式,然后:wq退出保存。

    serverlisten server_name yunkusroot location ~ \.PHP$  fastcgi_pass 127.00.19000 fastcgi_index indexPHP fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name include fastcgi_params}}

    前面三行代码就是设置监听端口为80,这个目录绑定域名为yunkus.com,这个目录位于/home/www/Nginx.yunkus.com。而下面的那一块代码就是让Nginx 能够识别 PHP 文件。如果你不加,当你通过:yunkus.com/index.PHP访问时,就会直接把这个index.PHP文件下载到本地并且无法正常浏览网站页面。如果还想添加一个站点重复上面的步骤就可以了。

    更新于:0:32 2017/4/4

    Nginx 安全篇

    换成了博客自带主题后,垃圾评论一直都停不下来,人为手动打广告还是可以接受的,但是一个晚上几分钟来一条广告评论,一看就是程序直接写入数据库的。见鬼去吧(永别了)!

    我的做法就是直接禁止这个 ip 访问网站服务,具体做法也很简单:

    在 /etc/Nginx/conf.d 目录下新建一个 .conf 文件,命名随意(如:getlost.conf );在这文件添加你想禁止的ip就可以了。

    
      
      
  • // 禁止某个网段deny 91.20012.024178.12294.0;// 禁止指定 ip27.150247.255// 禁止所有deny all;

    这个你可以按需添加,能起到效果就好了,最后一种应该不常用。

    注意

    文件修改完后记得重启 Nginx 服务

    systemctl restart Nginxservice

    重启完后,当这个ip 再访问时,就会返回如下图:

    这个 403 页面返回了Forbidden 字样,还显示Nginx及其版本号,所以在这里我们可以通过配置 Nginx.conf 文件不显示 Nginx 的版本号。

    Nginx.conf 的 http 区块中(http{…… })中添加如下一行代码

    server_tokens off// 关闭版本号显示功能

    Nginx 安装配置最总结

    更新于:22:08 2017/3/5

    上面写了那么多其实都只是自己折腾的过程,但我也相信你看到上面的一大段东西肯会觉得好乱,不知道从何开始。下面我就来总结下。

    步骤一:安装

    # yum install Nginx

    步骤二:配置:

    配置 Nginx方法,非常的简单。在/etc/Nginx/conf.d/目录下新建一个名为Nginx.yunkus.com.conf文件文件名随意,后缀名为.conf就 OK ),文件内容如下:

    
     
     PHP index# This is cool because no PHP is touched for static content.# include the "?$args" part so non-default permalinks doesn't break when using query string try_files $uri $uriindex?$argsfaviconico  log_not_found off access_log offrobotstxt  allow all}
    提示

    server_name yunkus.com; // 改成你自己的域名

    root /home/www/Nginx.yunkus.com; // 改成你自己的站点根目录

    重启Nginx服务就可以了:

    service

    步骤三:若要配置多个站点重复上面的步骤二就可以了。

    如果配置出问题,那现在你就可以往回看了,按照上面提到的问题一个一个进行排查。

    上面只是这次更新顺便把前面乱糟糟的东西的总结,下面才是这次更新的主要内容

    最近博客开启了自带评论,虽然也安装了Akismet 反垃圾评论插件,实现了垃圾评论的判断,但这往不能让人满意。因为你会收到很多垃圾评论,所以在这里我们得学着用 Nginx 来实现配置 ip 段来禁止其不法访问。

    /etc/Nginx/conf.d/目录下新那一个名为denyip.conf文件文件名可以随便取,编辑文件内容如下:

    
     
     // 禁止 91.200.12.1~91.200.12.255 网段访问网站;

    注意:最后的分号;不能省。文件配置好后,重启下 Nginx 服务就可以了。

    service

    相关文章

    Centos下搭建性能监控Spotlight
    CentOS 6.3下Strongswan搭建IPSec VPN
    在CentOS6.5上安装Skype与QQ
    阿里云基于centos6.5主机VPN配置
    CentOS 6.3下配置multipah
    CentOS安装、配置APR和tomcat-native