Centos安装Nginx

依赖安装: yum install gcc-c++ yum install pcre pcre-devel yum install zlib zlib-devel yum install openssl openssl-devel

压缩包安装步骤

新建安装路径

  1. cd /usr/local && mkdir nginx

下载Nginx包 & 解压

  1. # 通过 http://nginx.org/download/下载指定版本压缩包
  2. cd nginx && wget http://nginx.org/download/nginx-1.2.0.tar.gz
  3. # 解压
  4. tar -zxvf nginx-1.2.0.tar.gz

编译 & 安装

  1. # 进入解压目录编译
  2. cd nginx-1.2.0 && ./configure
  3. # 执行安装命令
  4. make
  5. make install

启动

  1. # 设置nginx执行权限,可非root用户启动
  2. chmod u+s /usr/local/nginx/sbin/nginx

更新配置

  1. # 修改配置文件
  2. cd /usr/local/nginx/conf && vim nginx.conf
  3. # 重载更新配置
  4. /usr/local/nginx/sbin/nginx -s reload

测试配置文件是否无误

  1. /usr/local/nginx/sbin/nginx -t

yum直接安装

  1. yum install nginx

安装后目录结构

  1. ├── conf.d # 子配置的配置项存放处
  2. ├── default.d
  3. # cgi配置相关
  4. ├── fastcgi.conf
  5. ├── fastcgi.conf.default
  6. ├── fastcgi_params
  7. ├── fastcgi_params.default
  8. # 编码转换映射转化文件
  9. ├── koi-utf
  10. ├── koi-win
  11. # 设置http协议的content-type和扩展名对应关系
  12. ├── mime.types
  13. ├── mime.types.default
  14. ├── nginx.conf # 主配置文件会默认把conf.d这个文件夹中所有子配置项都引入
  15. ├── nginx.conf.default
  16. # scgi配置相关
  17. ├── scgi_params
  18. ├── scgi_params.default
  19. ├── uwsgi_params
  20. ├── uwsgi_params.default
  21. └── win-utf
  22. # 存放静态文件
  23. /usr/share/nginx/html

检查防火墙和开放端口

  1. # 安装之后开启 Nginx,如果系统开启了防火墙,那么需要设置一下在防火墙中加入需要开放的端口
  2. systemctl start firewalld # 开启防火墙
  3. systemctl stop firewalld # 关闭防火墙
  4. systemctl status firewalld # 查看防火墙开启状态,显示running则是正在运行
  5. firewall-cmd --reload # 重启防火墙,永久打开端口需要reload一下
  6. # 添加开启端口,--permanent表示永久打开,不加是临时打开重启之后失效
  7. firewall-cmd --permanent --zone=public --add-port=8888/tcp
  8. # 查看防火墙,添加的端口也可以看到
  9. firewall-cmd --list-all
  10. # 设置 Nginx 开机启动
  11. systemctl enable nginx
  12. # 遇到问题:nginx: [error] invalid PID number "" in "/run/nginx.pid"
  13. # 解决办法:指定启动配置文件
  14. /usr/sbin/nginx -c /etc/nginx/nginx.conf

Mac 安装Nginx

安装步骤

  1. brew search nginx
  2. brew install nginx
  3. # Docroot is: /usr/local/var/www
  4. # The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
  5. # nginx can run without sudo.
  6. # nginx will load all files in /usr/local/etc/nginx/servers/.
  7. # To restart nginx after an upgrade:
  8. # brew services restart nginx
  9. # Or, if you don't want/need a background service you can just run:
  10. # /usr/local/opt/nginx/bin/nginx -g daemon off;

Nginx 常用命令

  1. nginx -s reload # 向主进程发送信号,重新加载配置文件,热重启
  2. nginx -s reopen # 重启 Nginx
  3. nginx -s stop # 快速关闭
  4. nginx -s quit # 等待工作进程处理完成后关闭
  5. nginx -T # 查看当前 Nginx 最终的配置
  6. nginx -t -c <配置路径> # 检查配置是否有问题,如果已经在配置目录,则不需要-c
  7. # -------------------或者使用systemctl命令--------------------
  8. # systemctl 是 Linux 系统应用管理工具 systemd 的主命令,用于管理系统
  9. systemctl start nginx # 启动 Nginx
  10. systemctl stop nginx # 停止 Nginx
  11. systemctl restart nginx # 重启 Nginx
  12. systemctl reload nginx # 重新加载 Nginx,用于修改配置后
  13. systemctl enable nginx # 设置开机启动 Nginx
  14. systemctl disable nginx # 关闭开机启动 Nginx
  15. systemctl status nginx # 查看 Nginx 运行状态