Centos安装Nginx
依赖安装: yum install gcc-c++ yum install pcre pcre-devel yum install zlib zlib-devel yum install openssl openssl-devel
压缩包安装步骤
新建安装路径
cd /usr/local && mkdir nginx
下载Nginx包 & 解压
# 通过 http://nginx.org/download/下载指定版本压缩包
cd nginx && wget http://nginx.org/download/nginx-1.2.0.tar.gz
# 解压
tar -zxvf nginx-1.2.0.tar.gz
编译 & 安装
# 进入解压目录编译
cd nginx-1.2.0 && ./configure
# 执行安装命令
make
make install
启动
# 设置nginx执行权限,可非root用户启动
chmod u+s /usr/local/nginx/sbin/nginx
更新配置
# 修改配置文件
cd /usr/local/nginx/conf && vim nginx.conf
# 重载更新配置
/usr/local/nginx/sbin/nginx -s reload
测试配置文件是否无误
/usr/local/nginx/sbin/nginx -t
yum直接安装
yum install nginx
安装后目录结构
├── conf.d # 子配置的配置项存放处
├── default.d
# cgi配置相关
├── fastcgi.conf
├── fastcgi.conf.default
├── fastcgi_params
├── fastcgi_params.default
# 编码转换映射转化文件
├── koi-utf
├── koi-win
# 设置http协议的content-type和扩展名对应关系
├── mime.types
├── mime.types.default
├── nginx.conf # 主配置文件会默认把conf.d这个文件夹中所有子配置项都引入
├── nginx.conf.default
# scgi配置相关
├── scgi_params
├── scgi_params.default
├── uwsgi_params
├── uwsgi_params.default
└── win-utf
# 存放静态文件
/usr/share/nginx/html
检查防火墙和开放端口
# 安装之后开启 Nginx,如果系统开启了防火墙,那么需要设置一下在防火墙中加入需要开放的端口
systemctl start firewalld # 开启防火墙
systemctl stop firewalld # 关闭防火墙
systemctl status firewalld # 查看防火墙开启状态,显示running则是正在运行
firewall-cmd --reload # 重启防火墙,永久打开端口需要reload一下
# 添加开启端口,--permanent表示永久打开,不加是临时打开重启之后失效
firewall-cmd --permanent --zone=public --add-port=8888/tcp
# 查看防火墙,添加的端口也可以看到
firewall-cmd --list-all
# 设置 Nginx 开机启动
systemctl enable nginx
# 遇到问题:nginx: [error] invalid PID number "" in "/run/nginx.pid"
# 解决办法:指定启动配置文件
/usr/sbin/nginx -c /etc/nginx/nginx.conf
Mac 安装Nginx
安装步骤
brew search nginx
brew install nginx
# Docroot is: /usr/local/var/www
# The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
# nginx can run without sudo.
# nginx will load all files in /usr/local/etc/nginx/servers/.
# To restart nginx after an upgrade:
# brew services restart nginx
# Or, if you don't want/need a background service you can just run:
# /usr/local/opt/nginx/bin/nginx -g daemon off;
Nginx 常用命令
nginx -s reload # 向主进程发送信号,重新加载配置文件,热重启
nginx -s reopen # 重启 Nginx
nginx -s stop # 快速关闭
nginx -s quit # 等待工作进程处理完成后关闭
nginx -T # 查看当前 Nginx 最终的配置
nginx -t -c <配置路径> # 检查配置是否有问题,如果已经在配置目录,则不需要-c
# -------------------或者使用systemctl命令--------------------
# systemctl 是 Linux 系统应用管理工具 systemd 的主命令,用于管理系统
systemctl start nginx # 启动 Nginx
systemctl stop nginx # 停止 Nginx
systemctl restart nginx # 重启 Nginx
systemctl reload nginx # 重新加载 Nginx,用于修改配置后
systemctl enable nginx # 设置开机启动 Nginx
systemctl disable nginx # 关闭开机启动 Nginx
systemctl status nginx # 查看 Nginx 运行状态