Nginx 搭建

175 阅读1分钟

普通安装

准备工作

下载:

wget http://zlib.net/zlib-1.2.8.tar.gz
wget http://nginx.org/download/nginx-1.6.2.tar.gz
wget http://softlayer-sng.dl.sourceforge.net/project/pcre/pcre/8.36/pcre-8.36.tar.gz
wget http://www.openssl.org/source/openssl-1.0.1m.tar.gz
wget http://www.cpan.org/src/5.0/perl-5.22.0.tar.gz

yum 安装 gcc

yum install -y gcc gcc-c++

安装

安装 perl

./Configure -des -Dprefix=$HOME/localperl
make
make test
make install

安装 pcre

./configure
make && make install

安装 openssl

可选装,支持 https

./config
make && make install

安装 zlib

./configure
make && make install

安装 Nginx

tar -zxvf nginx-1.6.2.tar.gz
cd nginx-1.6.2
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
# 执行上边语句,如果出现 ./configure: error: SSL modules require the OpenSSL library
# 执行 yum -y install openssl openssl-devel
# 再执行 ./configure
# 重新执行 ./configure --with-http_ssl_module
make && make install

安装完成后,默认路径在:

/usr/local/nginx

查看版本信息

/usr/local/nginx/sbin/nginx -v

启动 Nginx

/usr/local/nginx/sbin/nginx

# 如果报错:
# error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

解决办法:

32位系统:

ln -s /usr/local/lib/libpcre.so.1 /lib

64位系统:

ln -s /usr/local/lib/libpcre.so.1 /lib64

再启动 nginx 就 ok 了

Docker 安装

获取 Nginx 镜像

docker search nginx

dockersearchnginx.png

安装 Nginx

拉取 nginx 镜像到本地,此处我们获取排名第一的是官方最新镜像,其它版本可以去 DockerHub 查询

docker pull nginx

查看 Nginx 镜像 IMAGE ID

docker image nginx

创建并启动容器

测试 Nginx 是否可用

docker run -d --name mynginx -p 80:80 nginx

参数说明:

  • -d:指定容器以守护进程方式在后台运行
  • --name:指定容器名称,此处我指定的是mynginx
  • -p:指定主机与容器内部的端口号映射关系,格式 -p [宿主机端口号]:[容器内部端口],此处我使用了主机80端口,映射容器80端口
  • nginx:是nginx的镜像名,也可以写成镜像的IMAGE ID