centos7安装nginx
参考:https://www.cnblogs.com/boonya/p/7907999.html
1. 安装依赖
一、gcc 安装:yum install gcc-c++
二、PCRE pcre-devel 安装:yum install -y pcre pcre-devel
三、zlib 安装:yum install -y zlib zlib-devel
四、OpenSSL 安装:yum install -y openssl openssl-devel
2. 下载nginx压缩包、解压
wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
tar -zxvf nginx-1.12.0.tar.gz
3. 配置
cd nginx-1.12.0
./configure
4. 编译安装
make
make instal
5. 查找nginx的安装路径
whereis nginx
如下:
[root@192 ~]# whereis nginx
nginx: /usr/local/nginx
6. 启动nginx
[root@192 ~]# cd /usr/local/nginx # 进入安装目录
[root@192 nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@192 nginx]# cd sbin/
[root@192 sbin]# ls
nginx
[root@192 sbin]# ./nginx
7. ip:80访问
8. 如果访问不了,可能需要开放防火墙端口
[root@192 conf]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports: # 80端口没有开放,故此访问不了
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[root@192 conf]# firewall-cmd --add-port=80/tcp --permanent
success
[root@192 conf]# firewall-cmd --reload
success
9. 配置nginx的环境变量
vim /etc/profile
在最后面加入:
export NGINX_HOME='/usr/local/nginx' # 配置nginx的安装路径
export PATH=$PATH:$NGINX_HOME/sbin
保存后执行:
source /etc/profile
结果:
[root@192 ~]# nginx -v
nginx version: nginx/1.12.0
10. nginx常用命令
##配置环境变量之后,可以直接使用如下命令:
nginx : 启动nginx
nginx -s stop : 关闭nginx
nginx -s reload :重新加载nginx,可用于配置文件修改后重新加载,无需停止nginx
##如果没配置环境变量,则需要进入/usr/local/nginx/sbin下执行命令
11. nginx配置文件路径:/usr/local/nginx/conf
docker安装nginx
1. 拉取nginx镜像 : docker pull nginx
2. 查看nginx镜像 : docker images
3. 启动nginx容器
docker run --name nginx-jarvis -p 80:80 -d nginx
ip:80:
docker挂载nginx目录
1. 要挂载的目录和文件
## 进入已启动的容器
[root@localhost ~]# docker exec -it 5faf /bin/bash
root@5faf4d7eeb24:/#
## 配置文件nginx.conf
root@5faf4d7eeb24:/# cd /etc/nginx
root@5faf4d7eeb24:/etc/nginx# ls
conf.d ...... nginx.conf scgi_params uwsgi_params win-utf
## 配置文件default.conf
root@5faf4d7eeb24:/etc/nginx# cd conf.d/
root@5faf4d7eeb24:/etc/nginx/conf.d# ls
default.conf
## 静态网页
root@5faf4d7eeb24:/etc/nginx/conf.d# cd /usr/share/nginx/html
root@5faf4d7eeb24:/usr/share/nginx/html# ls
50x.html index.html
## 日志文件
root@5faf4d7eeb24:/usr/share/nginx/html# cd /var/log/nginx
root@5faf4d7eeb24:/var/log/nginx# ls
access.log error.log
可以挂载如下目录/文件:
/usr/share/nginx/html # 静态文件目录
/etc/nginx/nginx.conf # nginx.conf是个文件
/etc/nginx/conf.d # conf.d是个目录,存放了default.conf
/var/log/nginx # 日志目录
2. 创建nginx要挂载的文件存储目录
[root@192 ~]# mkdir -p /root/nginx/www /root/nginx/logs /root/nginx/conf /root/nginx/conf.d
[root@192 ~]# cd nginx
[root@192 nginx]# ls
conf conf.d logs www
www:本机存放的网页目录
logs:本机存放的日志目录
conf和conf.d:本机存放的配置文件目录
3. 拷贝容器内配置文件到本机
## 拷贝nginx.conf 5faf是刚才已启动的nginx容器
docker cp 5faf:/etc/nginx/nginx.conf /root/nginx/conf
## 拷贝default.conf
docker cp 5faf:/etc/nginx/conf.d/default.conf /root/nginx/conf.d
4. 在www目录下创建一个index.html
<html>
<head></head>
<body>
<h1>hello,world!</h1>
</body>
</html>
5. 启动容器
docker run -d -p 9090:80 --name nginx-jarvis02 -v /root/nginx/www:/usr/share/nginx/html -v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /root/nginx/conf.d:/etc/nginx/conf.d -v /root/nginx/logs:/var/log/nginx nginx
## 这样启动容器无法成功,在docker ps命令中无法查到存活的这个容器。
## 查询资料后,需要加上--privileged=true
docker run --privileged=true -d -p 9090:80 --name nginx-jarvis02 -v /root/nginx/www:/usr/share/nginx/html -v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /root/nginx/conf.d:/etc/nginx/conf.d -v /root/nginx/logs:/var/log/nginx nginx
[root@localhost logs]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
10327d887a7c nginx "/docker-entrypoin..." 3 seconds ago Up 2 seconds 0.0.0.0:9090->80/tcp nginx-jarvis02
5faf4d7eeb24 nginx "/docker-entrypoin..." 23 hours ago Up 30 minutes 0.0.0.0:80->80/tcp nginx-jarvis
6. ip:9090
7. 验证挂载正确
## 进入1032容器内部
[root@localhost logs]# docker exec -it 1032 /bin/bash
## 容器内部的index.html和容器外的静态文件已关联
root@10327d887a7c:/# cd /usr/share/nginx/html/
root@10327d887a7c:/usr/share/nginx/html# cat index.html
<html>
<head></head>
<body>
<h1>hello,world!</h1>
</body>
</html>
## 验证容器内外conf.d目录已关联
## 容器内conf.d目录新增a.txt
root@10327d887a7c:/usr/share/nginx/html# cd /etc/nginx/conf.d
root@10327d887a7c:/etc/nginx/conf.d# ls
default.conf
root@10327d887a7c:/etc/nginx/conf.d# touch a.txt
root@10327d887a7c:/etc/nginx/conf.d# echo adfasf>>a.txt
root@10327d887a7c:/etc/nginx/conf.d# exit
exit
## 容器外conf.d目录下多了a.tx
[root@localhost nginx]# cd conf.d
[root@localhost conf.d]# ls
a.txt default.conf
[root@localhost conf.d]# cat a.txt
adfasf
## 其余挂载亦正确
挂载的时候出现了很多问题,在网上找了很多博客也没提到这些问题,所幸找到了一篇很棒的博客,罗列了可能碰到的问题,供参考:
https://blog.csdn.net/qq_42114918/article/details/85238011?ops_request_misc=&request_id=&biz_id=102&utm_term=docker%2520%25E6%258C%2582%25E8%25BD%25BDnginx%2520%25E6%2597%25A0%25E6%25B3%2595%25E6%258C%2582%25E8%25BD%25BD%25E6%2596%2587%25E4%25BB%25B6&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-3-85238011.first_rank_v2_pc_rank_v29