centos7 安装 Nginx、使用 nginx 记录

#编程技术 2021-03-18 18:09:41 | 全文 871 字,阅读约需 2 分钟 | 加载中... 次浏览

👋 相关阅读


centos7 安装 Nginx、使用 nginx 记录

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、下载

1)直接官网下再,官网链接:https://www.oschina.net/action/GoToLink?url=https%3A%2F%2Fnginx.org%2Fen%2Fdownload.html

图片alt

如图,可以看到当前稳定版本是:1.18.0,可以直接点击下载。

2)使用 wget 命令下载(推荐)

** 下载版本号可根据目前官网最新稳定版自行调整 **

wget -c https://nginx.org/download/nginx-1.16.1.tar.gz

3、安装

1)找到下载的 nginx 压缩包,然后解压

tar -zxvf nginx-1.18.0.tar.gz

2)解压后进入目录

cd nginx-1.18.0

3)使用默认配置

./configure

4)编译安装

make
make install

5)查找安装路径,默认都是这个路径

[root@VM_0_12_centos ~]## whereis nginx
nginx: /usr/local/nginx

6)启动、停止 nginx

cd /usr/local/nginx/sbin/
./nginx     #启动
./nginx -s stop  #停止,直接查找nginx进程id再使用kill命令强制杀掉进程
./nginx -s quit  #退出停止,等待nginx进程处理完任务再进行停止
./nginx -s reload  #重新加载配置文件,修改nginx.conf后使用该命令,新配置即可生效

7)重启 nginx,建议先停止,再启动

./nginx -s stop
./nginx

8)查看 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

浏览器输入服务器 ip 即可看到 nginx 欢迎界面

4、开机自启动

1)在 rc.local 增加启动代码即可

vi /etc/rc.local

## 增加一行
/usr/local/nginx/sbin/nginx

## 增加后保存
:wq

## 设置执行权限
cd /etc
chmod 755 rc.local

图片alt

5、配置修改

1)进入 nginx 配置文件目录,找到 nginx 的配置文件 nginx.conf

cd /usr/local/nginx/conf/

2)直接修改

vi nginx.conf

3)示例说明

#listen为监听的端口
listen       80;
#server_name为域名
server_name  www.test.com;
#location是访问地址的设置,locahost也可以用服务器ip代替
location / {
proxy_pass http://localhost:8080; 
}

4)修改完成后,重新加载配置文件

cd /usr/local/nginx/sbin/
./nginx -s reload

via:

centos7安装Nginx、使用nginx记录 - 夜的隐为者 - OSCHINA - 中文开源技术交流社区 https://my.oschina.net/yueshengwujie/blog/3099219

Edit | Last updated on 2024-04-29 22:20:38




×