前提介绍
基础环境:Docker for Mac 17.12.0-ce
CentOS版本:CentOS 7.4.1708
Nginx版本:nginx-1.13.12
Nginx安装目录:/usr/local/nginx
源码包存放目录:/usr/local/src
1、Docker 相关操作
// 获取CentOS镜像
// 命令说明 docker pull [docker 镜像仓库地址] 仓库名[:标签]
// 命令格式 docker pull [<域名/IP>[:端口号]] <用户名>/<软件名>[:版本号]
// 镜像仓库地址不写,则从Docker Hub上获取镜像
// 用户名不写,则默认为官方 Library 用户名
➜ ~ docker pull centos:7.4.1708
7.4.1708: Pulling from library/centos
18b8eb7e7f01: Pull complete
Digest: sha256:2a61f8abd6250751c4b1dd3384a2bdd8f87e0e60d11c064b8a90e2e552fee2d7
Status: Downloaded newer image for centos:7.4.1708
// 创建并启用容器
// 命令说明 docker run [配置容器] 镜像 [容器内部执行的命令]
// 命令格式 docker run [OPTIONS] IMAGE [COMMAND]
// -it :交互式操作
// -d :以守护进程运行
// -p :设置宿主机与容器内部的端口映射 <宿主机端口:容器端口>
// 可使用命令: lsof -i:5000 ;事先判断端口是否为空闲状态
// --name :设置容器名称(若无,则由docker自动分配名称)
➜ ~ docker run -it -d -p 5000:80 --name centos7 centos:7.4.1708
558c672d7d151502b52b15c9efa93cef579ce327fcd7e3443b41ed04dca3b24c
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
558c672d7d15 centos:7.4.1708 "/bin/bash" 8 seconds ago Up 7 seconds 0.0.0.0:5000->80/tcp centos7
// 进入容器
➜ ~ docker exec -it centos7 /bin/bash
[root@558c672d7d15 /]#
2、Nginx
(1)配置安装环境
// Nginx 依赖包
// zlib :Nginx提供gzip模块,需要zlib库支持。
// pcre :支持地址重写rewrite功能
// openssl :Nginx提供SSL功能
[root@558c672d7d15 ~]# yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
// 查看依赖包是否安装成功
// rpm -q <包名> :查询一个包是否被安装
// rpm -qa [包名] :列出所有被安装的包
[root@558c672d7d15 ~]# rpm -q openssl
openssl-1.0.2k-8.el7.x86_64
// Nginx 编译环境
// 还需要 make,因为系统自带,故不再安装
[root@558c672d7d15 ~]# yum -y install gcc gcc-c++ autoconf automake
(2)下载及解压
// 安装下载工具
[root@558c672d7d15 ~]# yum -y install wget
// Nginx源码选择地址:http://nginx.org/en/download.html
[root@558c672d7d15 ~]# cd /usr/local/src
[root@558c672d7d15 src]# wget http://nginx.org/download/nginx-1.13.12.tar.gz
// tar 解压命令
// -x:解压
// -z:有gzip属性的
// -v:显示所有过程
// -f:使用档案名字,这个参数是最后一个参数,后面只能接档案名。
[root@558c672d7d15 src]# tar -zxvf nginx-1.13.12.tar.gz
(3)创建用户与组
// 创建用户作为用于启动nginx进程的用户
[root@558c672d7d15 src]# groupadd nginx
[root@558c672d7d15 src]# useradd -r -g nginx nginx
(4)生成预编译配置文件
// configure 配置说明
// --prefix=path 代表安装路径
// --sbin-path=path 设置nginx可执行文件的路径和名称
// --conf-path=path 设置nginx.conf配件文件的路径
// --pid-path=path 设置存储主进程id的文件名称
// --error-log-path=path 设置主请求的错误、警告、诊断的日志文件的名称
// --http-log-path=path 设置HTTP服务器的主请求的日志文件的名称
// --user 启动进程用户
// --group 启动进程用户所在组
// --with-http_ssl_module 启用添加HTTPS协议支持到HTTP服务器的模块
// 其他还有很多属性,可./configure --help 去查看
[root@558c672d7d15 src]# cd nginx-1.13.12
[root@558c672d7d15 nginx-1.13.12]#./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/nginx.conf \(不建议配置。这里配置是为了直观的看到目录分布)
--error-log-path=/usr/local/nginx/logs/error.log \
--pid-path=/usr/local/nginx/nginx.pid \
--user=nginx \
--group=nginx \
--with-http_ssl_module


(5)编译并安装
// 也可以分成两条命令执行
[root@558c672d7d15 nginx-1.13.12]# make && make install

安装完成,如下图:

目录结构,如下图:

(6)运行测试
[root@558c672d7d15 nginx]# cd /usr/local/nginx/
[root@558c672d7d15 nginx]# sbin/nginx
// 查看Nginx 主、子进程是否启动
[root@558c672d7d15 nginx]# ps aux | grep nginx
测试过程,如下图:

再通过浏览器访问:http://localhost:5000/

(7)配置环境变量
// 进入配置环境文件中
[root@558c672d7d15 nginx]# vi /etc/profile
// 再该文件末尾处,加入以下语句
# Nginx
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin
// 令配置文件立即生效
[root@558c672d7d15 nginx]# source /etc/profile

(8)Nginx相关命令
// 以下 nginx 开头的命令,如果未配置环境变量,需要加上nginx执行文件所在路径
// 例:/usr/local/nginx/sbin/nginx -s reload
nginx -s reload :修改配置后重新加载生效
nginx -s reopen :重新打开日志文件
nginx -t -c /path/to/nginx.conf :测试nginx配置文件是否正确
指定配置文件,启动nginx:
nginx -c /path/to/nginx.conf
关闭nginx:
nginx -s stop :快速停止nginx
quit :完整有序的停止nginx
其他的停止nginx 方式:
ps -ef | grep nginx
kill -QUIT 主进程号 :从容停止Nginx
kill -TERM 主进程号 :快速停止Nginx
pkill -9 nginx :强制停止Nginx
3、将Nginx加入到系统服务中(可以不配置)
[root@558c672d7d15 nginx]#service --version
// 如果上条命令抛出:
// bash: service: command not found
// 则需要执行下面的这条命令,安装启动管理器
[root@558c672d7d15 nginx]# yum install initscripts
// 编辑配置文件
[root@558c672d7d15 nginx]# vi /etc/init.d/nginx
// 将以下内容输入到文档内
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /usr/local/nginx/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
killall -9 nginx
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
保存并退出后
// 授予该脚本可执行权限:
[root@558c672d7d15 nginx]# chmod +x /etc/init.d/nginx
// service nginx start 启动
// service nginx stop 停止
// service nginx reload 重启
// 设置开机自启
// chkconfig是管理系统服务(service)的命令行工具。
[root@558c672d7d15 nginx]# chkconfig --add nginx

网友评论