Skip to content

Nginx安装

Centos编译安装nginx

一般系统中已经装了make和g++,已经安装的无须再装

shell
# 安装make
yum -y install autoconf automake make
# 安装g++
yum -y install gcc gcc-c++
# 安装nginx依赖的库
yum -y install wget pcre pcre-devel zlib zlib-devel openssl openssl-devel
# 下载nginx
wget http://nginx.org/download/nginx-1.23.0.tar.gz
# 解压nginx
tar -zxvf nginx-1.23.0.tar.gz

安装及配置环境变量,编译配置:

  • –prefix指定安装目录
  • –with-http_ssl_module安装https模块
shell
# 编译安装,具体配置见说明,默认安装路径/usr/local/nginx
cd /usr/local/nginx
./configure  --prefix=/usr/local/nginx
# creating objs/Makefile 代表编译成功
make && make install
# 配置环境变量
vi /etc/profile
export PATH=$PATH:/usr/local/nginx/sbin
# 刷新
source /etc/profile
# 关闭防火墙(可选步骤,本地虚拟机测试时可以关闭,先把环境问题排除,确定Nginx安装成功)
systemctl status firewalld
systemctl stop firewalld
systemctl disable firewalld

yum方式安装

默认情况Centos7中无Nginx的源,最近发现Nginx官网提供了Centos的源地址。

shell
# 添加源
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# 查询源是否添加成功
yum search nginx
# 安装
sudo yum install -y nginx
# 启动Nginx
sudo systemctl start nginx.service
# 设置开机自动运行
sudo systemctl enable nginx.service

Mac使用homebrew安装Nginx

  • 安装路径:/usr/local/Cellar/nginx
  • 配置文件路径:/usr/local/etc/nginx/nginx.conf
shell
# 安装Nginx
brew install nginx
# 查看nginx信息
brew info nginx

# 直接使用Nginx启动
sudo nginx
# 关闭,两种方式选其一
# stop表示立即停止nginx不保存相关信息;
sudo nginx -s stop
# quit表示正常退出nginx,并保存相关信息
nginx -s quit
# 重载
sudo nginx -s reload

使用brew方式启动

shell
# 启动
brew services start nginx
# 重启
brew services restart nginx
# 停止
brew services stop nginx