准备工作
-
打开虚拟机,使用Xshell6连接到CentOS6.8操作系统
-
到官网下载nginx官网下载软件http://nginx.org/
Nginx安装
(1)安装pcre依赖
终端切换到/usr/src
目录下:
[root@localhost ~]# cd /usr/src
[root@localhost src]#
- 联网下载pcre压缩文件依赖
[root@localhost src]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
[root@localhost src]# ls
debug kernels pcre-8.37.tar.gz
- 解压压缩文件
[root@localhost src]# tar -xvf pcre-8.37.tar.gz
[root@localhost src]# ls
debug kernels pcre-8.37 pcre-8.37.tar.gz
- 进入解压之后的文件,执行
./configure
[root@localhost src]# cd pcre-8.37
[root@localhost pcre-8.37]# ./configure
注意:在执行./configure
时需要系统安装gcc,否则会出错,如下:
[root@localhost pcre-8.37]# ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking whether make supports nested variables... (cached) yes
checking for style of include used by make... GNU
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/usr/src/pcre-8.37':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
此时需要安装gcc,如下:
[root@localhost pcre-8.37]# yum -y install gcc
再次执行./configure
命令。
[root@localhost pcre-8.37]# ./configure
此时又出现了一个错误,如下:
configure: error: You need a C++ compiler for C++ support.
原因是系统缺少c++,那就在线安装c++,如下:
[root@localhost pcre-8.37]# yum install -y gcc gcc-c++
再次执行./configure
命令。不出意外,就Okay了。
- 编译并安装pcre
执行make && make install
[root@localhost pcre-8.37]# make && make install
查看pcre版本:
[root@localhost pcre-8.37]# pcre-config --version
8.37
至此,nginx的第一个依赖pcre就安装完成了。
(2)安装zlib及openssl依赖
通过yum方式安装,不再使用下载压缩包,编译安装的方式,且自动下载依赖。执行下面一条命令即可。
[root@localhost pcre-8.37]# yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
(3)安装Nginx
通过wget方式下载nginx压缩文件包,如下:
[root@localhost src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
[root@localhost src]# ls
debug kernels nginx-1.12.2.tar.gz pcre-8.37 pcre-8.37.tar.gz
[root@localhost src]#
解压下载的nginx压缩包,如下:
[root@localhost src]# tar -xvf nginx-1.12.2.tar.gz
[root@localhost src]# ls
debug kernels nginx-1.12.2 nginx-1.12.2.tar.gz pcre-8.37 pcre-8.37.tar.gz
进入到解压后的nginx目录中,执行./configure
命令。
[root@localhost src]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]# ./configure
如上检查完成之后执行编译,安装命令即可,如下:
[root@localhost nginx-1.12.2]# make && make install
至此,Nginx就安装完成了。安装成功之后,在 usr 目录中 多出来一个目录 local/nginx,在 nginx 目录下 有 sbin 目录,有启动脚本。如下:
[root@localhost nginx-1.12.2]# cd /usr
[root@localhost usr]# ls
bin etc games include lib lib64 libexec local sbin share src tmp
[root@localhost usr]# cd local
[root@localhost local]# ls
bin etc games include lib lib64 libexec nginx sbin share src
[root@localhost local]# cd nginx
[root@localhost nginx]# ls
conf html logs sbin
[root@localhost nginx]#
(4)测试Nginx
进入到 usr/local/nginx/sbin
目录下,启动nginx,如下:
[root@localhost /]# cd /usr/local/nginx/sbin
[root@localhost sbin]# ls
nginx
# 启动nginx
root@localhost sbin]# ./nginx
很遗憾,出现了一个错误,如下:
[root@localhost sbin]# ./nginx
./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
[root@localhost sbin]#
但是一开始我们就安装了pcre依赖,那么现在就建立依赖。如下:
首先查看 libpcre.so
文件位置,如下:
[root@localhost sbin]# find / -type f -name *libpcre.so.*
/lib64/libpcre.so.0.0.1
/usr/src/pcre-8.37/.libs/libpcre.so.1.2.5
/usr/lib64/firefox/bundled/lib64/libpcre.so.1.2.0
/usr/local/lib/libpcre.so.1.2.5
[root@localhost sbin]#
其次,建立软连接,如下:
[root@localhost sbin]# ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.1
[root@localhost sbin]#
再次启动Nginx,如下:
[root@localhost sbin]# ./nginx
[root@localhost sbin]#
(5)查看Nginx进程
启动nginx之后,查看nginx进程,如下:
# 查看nginx进程
[root@localhost sbin]# ps -ef | grep nginx
root 57188 1 0 22:26 ? 00:00:00 nginx: master process ./nginx
nobody 57189 57188 0 22:26 ? 00:00:00 nginx: worker process
root 57198 24575 0 22:29 pts/1 00:00:00 grep nginx
[root@localhost sbin]#
可以看到,此时nginx已经在运行了。
(6)访问Nginx
由于nginx中默认的端口是80端口,但是出于linux系统安全方面考虑,所以系统中默认没有开放80端口,关于是否开启80端口,我们可以通过下面的命令查看:
[root@localhost sbin]# firewall-cmd --list-all
如果显示有80端口就不用管它,如果没有就需要执行下面的命令,开启80端口。如下:
[root@localhost sbin]# sudo firewall-cmd --add-port=80/tcp --permanent
重新加载防火墙,如下:
[root@localhost sbin]# firewall-cmd --reload
再次查看防火墙列表:
[root@localhost sbin]# firewall-cmd --list-all
至于nginx中默认监听80端口,我们可以在/usr/local/nginx/conf
下的nginx.conf
中看到。
[root@localhost /]# cd /usr/local/nginx/conf
# 查看conf目录下有nginx目录
[root@localhost conf]# ls
fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf
fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default
# 查看nginx中文本信息
[root@localhost conf]# cat nginx.conf
浏览器输入IP地址访问:
启动nginx的命令为 /usr/local/nginx/sbin/nginx
停止nginx的命令为 /usr/local/nginx/sbin/nginx -s stop
重启nginx的命令为 /usr/local/nginx/sbin/nginx -s reload
本篇完结!
评论区