1. 安装依赖
#gcc安装,nginx源码编译需要
yum install gcc-c++
#PCRE pcre-devel 安装,nginx 的 http 模块使用 pcre 来解析正则表达式
yum install -y pcre pcre-devel
#zlib安装,nginx 使用zlib对http包的内容进行gzip
yum install -y zlib zlib-devel
#OpenSSL 安装,强大的安全套接字层密码库,nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http)
yum install -y openssl openssl-devel
2. 使用wget命令下载(推荐)
# 版本自行选择稳定版即可
wget -c https://nginx.org/download/nginx-1.16.1.tar.gz
3. 安装
# 解压到指定目录
tar -zxvf nginx-1.16.1.tar.gz -C /opt/bin
# 进入目录
cd /opt/bin/nginx-1.16.1
#使用默认配置
./configure
#编译安装
make
make install
# 查找安装路径,默认都是这个路径
[root@VM_0_3_centos nginx-1.16.1]# whereis nginx
nginx: /usr/local/nginx
# 启动、停止nginx
cd /usr/local/nginx/sbin/
./nginx #启动
./nginx -s stop #停止,直接查找nginx进程id再使用kill命令强制杀掉进程
./nginx -s quit #退出停止,等待nginx进程处理完任务再进行停止
./nginx -s reload #重新加载配置文件,修改nginx.conf后使用该命令,新配置即可生效
#重启nginx,建议先停止,再启动
./nginx -s stop
./nginx
#查看nginx进程
[root@VM_0_12_centos ~]# ps aux|grep nginx
root 21888 0.0 0.0 20556 616 ? Ss 21:24 0:00 nginx: master process ./nginx
nobody 21889 0.0 0.0 23092 1636 ? S 21:24 0:00 nginx: worker process
root 25687 0.0 0.0 112712 960 pts/0 R+ 21:52 0:00 grep --color=auto nginx
4. 设置开机自启动
# 第一步
vi /etc/rc.local
# 增加一行
/usr/local/nginx/sbin/nginx
#设置执行权限
cd /etc
chmod 755 rc.local
此时浏览器输入服务器ip即可访问 nginx 页面。
5. 配置域名映射
nginx 配置文件都在 /usr/local/nginx/conf/ 目录下,文件名称是 nginx.conf。
# 进入nginx配置文件目录
cd /usr/local/nginx/conf
# 修改配置文件
vi nginx.conf
域名配置:
只需要修改 server_name 和 location 里面的内容即可。
listen 80;
# 域名
server_name www.example.com;
# 访问地址的设置
location / {
proxy_pass http://localhost:9000;
}
修改完成后,重新加载配置文件:
cd /usr/local/nginx/sbin/
./nginx -s reload
本文使用 mdnice 排版