前言

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

小插曲

如果你直接执行命令

 
  1. # 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 安装包),所以我们得配置下,让系统自动去帮你去下载,执行如下代码

 
  1. # vim /etc/yum.repos.d/nginx.repo

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

 
  1. [nginx]
  2. name=nginx repo
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
  4. gpgcheck=0
  5. enabled=1

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

 
  1. # yum install nginx

nginx 安装配置

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

 
  1. # yum install nginx

安装记录

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

 
  1. [root@niaoyun53098 ~]# yum install nginx
  2. Loaded plugins: fastestmirror
  3. nginx | 2.9 kB 00:00
  4. nginx/7/x86_64/primary_db | 14 kB 00:01
  5. Loading mirror speeds from cached hostfile
  6. * base: mirrors.yun-idc.com
  7. * extras: mirrors.tuna.tsinghua.edu.cn
  8. * rpmforge: mirrors.neusoft.edu.cn
  9. * updates: mirrors.tuna.tsinghua.edu.cn
  10. Resolving Dependencies
  11. --> Running transaction check
  12. ---> Package nginx.x86_64 1:1.10.1-1.el7.ngx will be installed
  13. --> Finished Dependency Resolution
  14. Dependencies Resolved
  15. ================================================================================
  16. Package Arch Version Repository Size
  17. ================================================================================
  18. Installing:
  19. nginx x86_64 1:1.10.1-1.el7.ngx nginx 640 k
  20. Transaction Summary
  21. ================================================================================
  22. Install 1 Package
  23. Total download size: 640 k
  24. Installed size: 2.1 M
  25. Is this ok [y/d/N]: y
  26. Downloading packages:
  27. nginx-1.10.1-1.el7.ngx.x86_64.rpm | 640 kB 01:03
  28. Running transaction check
  29. Running transaction test
  30. Transaction test succeeded
  31. Running transaction
  32. Installing : 1:nginx-1.10.1-1.el7.ngx.x86_64 1/1
  33. ----------------------------------------------------------------------
  34. Thanks for using nginx!
  35. Please find the official documentation for nginx here:
  36. * http://nginx.org/en/docs/
  37. Commercial subscriptions for nginx are available on:
  38. * http://nginx.com/products/
  39. ----------------------------------------------------------------------
  40. Verifying : 1:nginx-1.10.1-1.el7.ngx.x86_64 1/1
  41. Installed:
  42. nginx.x86_64 1:1.10.1-1.el7.ngx
  43. Complete!

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

安装详情

 
  1. # yum install nginx

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

 
  1. Is this ok [y/d/N]: y

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

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

 
  1. # 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.

配置命令

随服务器启动

 
  1. # systemctl enable nginx.service

重启 nginx 服务

 
  1. # systemctl restart nginx.service

停止 nginx 服务

 
  1. # systemctl stop nginx.service

查看 nginx 运行状态

 
  1. # systemctl status nginx.service

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

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

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

 
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>nginx 配置</title>
  6. </head>
  7. <body>
  8. 我是站点默认根目录下的测试页面
  9. </body>
  10. </html>

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

 
  1. # vi /usr/share/nginx/html/index.html

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

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

1.创建站点目录

 
  1. # mkdir -p /home/www/nginx.yunkus.com

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

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

接下来我们就得配置下 nginx 的配置文件了

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

 
  1. # cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/nginx.yunkus.com.conf

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

 
  1. vi /etc/nginx/conf.d/nginx.yunkus.com.conf

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

 
  1. server {
  2. listen 80;
  3. server_name localhost;
  4. #charset koi8-r;
  5. #access_log /var/log/nginx/log/host.access.log main;
  6. location / {
  7. root /usr/share/nginx/html;
  8. index index.html index.htm;
  9. }
  10. #error_page 404 /404.html;
  11. # redirect server error pages to the static page /50x.html
  12. #
  13. error_page 500 502 503 504 /50x.html;
  14. location = /50x.html {
  15. root /usr/share/nginx/html;
  16. }
  17. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  18. #
  19. #location ~ \.php$ {
  20. # proxy_pass http://127.0.0.1;
  21. #}
  22. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  23. #
  24. #location ~ \.php$ {
  25. # root html;
  26. # fastcgi_pass 127.0.0.1:9000;
  27. # fastcgi_index index.php;
  28. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  29. # include fastcgi_params;
  30. #}
  31. # deny access to .htaccess files, if Apache's document root
  32. # concurs with nginx's one
  33. #
  34. #location ~ /\.ht {
  35. # deny all;
  36. #}
  37. }

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{ ….. }里重复上面的步骤就可以了。

 
  1. server {
  2. listen 80;
  3. server_name yunkus.com;
  4. #charset koi8-r;
  5. #access_log /var/log/nginx/log/host.access.log main;
  6. location / {
  7. root /home/www/nginx.yunkus.com;
  8. index index.html index.htm;
  9. }
  10. #error_page 404 /404.html;
  11. # redirect server error pages to the static page /50x.html
  12. #
  13. error_page 500 502 503 504 /50x.html;
  14. location = /50x.html {
  15. root /usr/share/nginx/html;
  16. }
  17. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  18. #
  19. #location ~ \.php$ {
  20. # proxy_pass http://127.0.0.1;
  21. #}
  22. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  23. #
  24. #location ~ \.php$ {
  25. # root html;
  26. # fastcgi_pass 127.0.0.1:9000;
  27. # fastcgi_index index.php;
  28. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  29. # include fastcgi_params;
  30. #}
  31. # deny access to .htaccess files, if Apache's document root
  32. # concurs with nginx's one
  33. #
  34. #location ~ /\.ht {
  35. # deny all;
  36. #}
  37. }

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

 
  1. # vi /etc/nginx/nginx.conf

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

 
  1. user nginx;
  2. worker_processes 1;
  3. error_log /var/log/nginx/error.log warn;
  4. pid /var/run/nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. }
  8. http {
  9. include /etc/nginx/mime.types;
  10. default_type application/octet-stream;
  11. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  12. '$status $body_bytes_sent "$http_referer" '
  13. '"$http_user_agent" "$http_x_forwarded_for"';
  14. access_log /var/log/nginx/access.log main;
  15. sendfile on;
  16. #tcp_nopush on;
  17. keepalive_timeout 65;
  18. #gzip on;
  19. include /etc/nginx/conf.d/*.conf;
  20. }

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

 
  1. # vi /etc/sysconfig/iptables

按 i 进入编辑模式

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

后面追加一行

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

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

 
  1. # systemctl restart iptables.service
备注:

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

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

nginx配置多个虚拟主机

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

 
  1. server{
  2. listen 80;
  3. server_name yunkus.com;
  4. root /home/www/nginx.yunkus.com;
  5. location ~ \.php$ {
  6. fastcgi_pass 127.0.0.1:9000;
  7. fastcgi_index index.php;
  8. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  9. include fastcgi_params;
  10. }
  11. }

前面三行代码就是设置监听端口为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就可以了。

 
  1. // 禁止某个网段
  2. deny 91.200.12.0/24;
  3. deny 178.122.94.0/24;
  4. // 禁止指定 ip
  5. deny 27.150.247.255;
  6. // 禁止所有
  7. deny all;

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

注意

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

 
  1. systemctl restart nginx.service

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

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

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

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

nginx 安装配置最总结

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

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

步骤一:安装

 
  1. # yum install nginx

步骤二:配置:

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

 
  1. server{
  2. listen 80;
  3. server_name yunkus.com;
  4. root /home/www/nginx.yunkus.com;
  5. index index.php index.html;
  6. location / {
  7. # This is cool because no php is touched for static content.
  8. # include the "?$args" part so non-default permalinks doesn't break when using query string
  9. try_files $uri $uri/ /index.php?$args;
  10. }
  11. location = /favicon.ico {
  12. log_not_found off;
  13. access_log off;
  14. }
  15. location = /robots.txt {
  16. allow all;
  17. log_not_found off;
  18. access_log off;
  19. }
  20. location ~ \.php$ {
  21. fastcgi_pass 127.0.0.1:9000;
  22. fastcgi_index index.php;
  23. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  24. include fastcgi_params;
  25. }
  26. }
提示:

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

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

重启nginx服务就可以了:

 
  1. systemctl restart nginx.service

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

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

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

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

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

 
  1. // 禁止 91.200.12.1~91.200.12.255 网段访问网站
  2. deny 91.200.12.0/24;

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

 
  1. systemctl restart nginx.service

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

  1. [乐意黎转载]CentOS yum 源的配置与使用

    http://www.cnblogs.com/mchina/archive/2013/01/04/2842275.html 一.yum 简介 yum,是Yellow dog Updater, Modi ...

  2. linux离线安装Nginx依耐环境,Linux Centos 7 - Nginx离线安装

    Linux Centos 7 - Nginx离线安装 dy.huang • 2020 年 03 月 31 日 一.参考文章 二.环境准备 可以以下网址下载rpm包,上传到# rpm包 gcc-4.8. ...

  3. [乐意黎转载]一个治愈 JavaScript 疲劳的学习计划

    像其他人一样,我最近偶然看到 Jose Aguinaga 的文章<在 2016 年学 JavaScript 是一种什么样的体验>". 译者注:中文翻译在此. 很显然,这篇文章触到 ...

  4. Centos 6.3 x86_64安装与配置bacula

    Centos 6.3 x86_64安装与配置bacula 一,简介 Bacula是一款开源的跨平台的网络备份工具,基于c/s架构,通过它,管理员可以对数据进行备份,恢复,以及完整性验证等操作. 适合业 ...

  5. nginx的安装与配置

    1.nginx的安装与配置 编译安装nginx需要实现安装开发包组"Development tools"和"Server Platform Development&quo ...

  6. Centos/Red Hat6.8 安装、配置、启动Gitlab (内网环境)心得分享

    文章目录 一.Gitlab下载 1. Gitlab官网下载 二.分析思路 2.1. 分析 2.2. 代码托管平台 2.3. 镜像站 二.Gitlab 重新加载配置异常 2.1. 异常现象 2.2. 分 ...

  7. CentOS 6.3 samba安装及配置

    Samba 简介 在Unix 系统中,samba是通过服务器消息块协议(SMB)在网络上的计算机之间共享文件和打印服务的软件包. SMB(server Message Block)协议是一种客户端服务 ...

  8. Nginx的安装基础配置(windows、linux)以及搭建图片服务器(windows、阿里云),文件上传

    Nginx的安装基础配置(windows.linux)以及搭建图片服务器(windows.阿里云),文件上传 本文目录 文章目录 Nginx的安装基础配置(windows.linux)以及搭建图片服务 ...

  9. Centos7下Nginx的安装与配置

    Centos7下Nginx的安装与配置 一.安装 1.下载 nginx官网下载地址,下载nginx-1.20.2.tar.gz,并将其上传至centos,我这里上传到了/usr/local/src/目 ...

最新文章

  1. VMware vSphere/vCenter/ESX(i)介绍
  2. 使用移动自适应布局+easy mock实现移动界面的简单实现
  3. 指定端口传输_一段话告诉你什么是端口
  4. git.exe init#timeout = 10错误:克隆远程repo'origin'时出错hudson.plugins.git
  5. POJ 1692 Crossed Matchings dp[][] 比较有意思的dp
  6. 组织c语言程序的是什么,C程序在内存中的组织方式
  7. 启牛商学院计算机,启牛商学院达摩老师教你三招成为跨界理财达人
  8. 信贷评分卡的顶层设计思路
  9. FailSafe双机方案
  10. oracle异常:主动抛出自定义异常+捕获指定异常
  11. 小京东商城用什么版本的php,最新2018小京东多用户通用商城网源码商城带手机版ecshop源码程序...
  12. 微信公众号测试号申请及访问(图详解)
  13. 计算机系统应用属于ei,2018年度中心成员发表论文清单(SCI、EI收录)
  14. 写bat文件,报系统找不到指定文件的处理方式
  15. java定时从数据库抓取数据库,java查询数据库java如何实现定时从数据库查询新增的数据?...
  16. 国外甲醇汽油的应用概况
  17. 看门狗watchdog的理解
  18. 助力传统游戏转型GameFi,Web3Games推动游戏发展新航向
  19. 盘点英特尔、苹果、高通、AMD 处理器重大 Bug,硬件的坑软件能填?
  20. 微软Skype开始采用全新图标 淘汰云朵图案

热门文章

  1. sql_递归sql_explan详情
  2. java创建文件 权限_JAVA创建文件后权限不足的问题,无法设置权限的问题
  3. [Lisp]用 sbcl, asdf 和 cl-launch 编写可分发的 lisp 程序 -- 转载
  4. JavaScript 观察者模式
  5. WaitForSingleObject SetEvent
  6. VR全景看房线上3D住宅商品房鉴赏
  7. 【转载】正确解决:坑爹的0xc000007b——应用程序无法正常启动
  8. odoo 自动生成编号
  9. ws协议与http协议的异同
  10. Angular5 Component通信