记录一次CentOs7安装Nginx
说明: 在学习CentOs安装Nginx都是参考网上的一些文档,文章,一步步操作过来的,这里只是将学习的过程记录下来,记录的内容是个人觉得写得很不错的文章的综合
文档参考转自:
一. 下载Nginx安装包
- Nginx官网地址https://nginx.org/en/download.html ,下载Nginx的稳定版本
- 使用wget命令下载
#下载版本号可根据目前官网最新稳定版自行调整 wget -c https://nginx.org/download/nginx-1.18.0.tar.gz
二. 安装依赖环境
#gcc安装,nginx源码编译需要,安装 nginx;
#需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装
yum install gcc-c++
#PCRE pcre-devel 安装,nginx 的 http 模块使用 pcre 来解析正则表达式;
#PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:
yum install -y pcre pcre-devel
#zlib安装,nginx 使用zlib对http包的内容进行gzip
#zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
yum install -y zlib zlib-devel
#OpenSSL 安装,强大的安全套接字层密码库,nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http)
#OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
#nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
yum install -y openssl openssl-devel
三. 安装
#根目录使用ls命令可以看到下载的nginx压缩包,然后解压
tar -zxvf nginx-1.16.1.tar.gz
#解压后进入目录
cd nginx-1.16.1
#使用默认配置
./configure
#那么我们的新配置信息就应该这样写 (在原有基础上增加 --with-http_ssl_modul):
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
#编译安装
make
make install
#查找安装路径,默认都是这个路径
[root@VM_0_12_centos ~]# 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 5984 0.0 0.0 112708 976 pts/1 R+ 14:41 0:00 grep --color=auto nginx
root 18198 0.0 0.0 20552 612 ? Ss 11:28 0:00 nginx: master process ./nginx
nobody 18199 0.0 0.0 23088 1632 ? S 11:28 0:00 nginx: worker process
四. 设置开机自启动
#在rc.local增加启动代码即可
vi /etc/rc.local
#增加一行 /usr/local/nginx/sbin/nginx,增加后保存
#设置执行权限
cd /etc
chmod 755 rc.local
五. 配置域名映射
#进入nginx配置文件目录,找到nginx的配置文件nginx.conf
cd /usr/local/nginx/conf/
#直接修改
vi nginx.conf
#listen为监听的端口
listen 80;
#server_name为域名
server_name www.ayding.top;
#location是访问地址的设置,locahost也可以用服务器ip代替
location / {
proxy_pass http://localhost:8080;
}
#修改完成后,重新加载配置文件
cd /usr/local/nginx/sbin/
./nginx -s reload