在安装之前我们来了解一下nginx
nginx(“engine x”)是俄罗斯人编写的十分轻量级的HTTP服务器。
是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP代理服务器。
官方网站:http://nginx.org/
访问官方网站下载nginx源码包。
http://nginx.org/en/download.html
选择Stable version下面的稳定版本,选择源码包下载。
或者复制链接地址在命令行下载。
[root@server ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@server ~]# mkdir /tools
[root@server ~]# mv nginx-1.14.2.tar.gz /tools/
[root@server ~]# cd /tools/
[root@server tools]# ls
nginx-1.14.2.tar.gz
创建nginx用户和组
[root@server tools]# groupadd nginx
[root@server tools]# useradd -g nginx nginx -M -s /sbin/nologin
解压源码包安装依赖软件
[root@server tools]# yum -y install gcc pcre-devel openssl-devel
[root@server tools]# tar -xzvf nginx-1.14.2.tar.gz
[root@server tools]# cd nginx-1.14.2/
[root@server nginx-1.14.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
编译前准备工作
更改nginx版本号和服务器名称
将服务器名称更改为666,版本号更改为8.8.8
[root@server nginx-1.14.2]# vim src/core/nginx.h
#define NGINX_VERSION "8.8.8"
#define NGINX_VER "666/" NGINX_VERSION
修改HTTP头信息中的connection字段,防止回显具体版本号。
修改下面的配置文件第49行
[root@server nginx-1.14.2]# vim src/http/ngx_http_header_filter_module.c
static u_char ngx_http_server_string[] = "Server: 666" CRLF;
更改错误返回信息。
修改下面的配置文件第22行
[root@server nginx-1.14.2]# vim src/http/ngx_http_special_response.c
"<hr><center>666</center>" CRLF
开始编译安装
[root@server nginx-1.14.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_ssl_module
安装选项说明:
> --prefix=/usr/local/nginx \ #指定安装路径
> --user=nginx \ #指定用户
> --group=nginx \ #指定组
> --with-http_ssl_module #开启SSL加密功能
指定CPU核数进行编译并安装
[root@server nginx-1.14.2]# make -j `cat /proc/cpuinfo | grep "physical id" | uniq | wc -l` && make install
[root@server nginx-1.14.2]# ls /usr/local/nginx/
conf html logs sbin
编写几个测试文件进行测试
启动nginx
[root@server nginx-1.14.2]# /usr/local/nginx/sbin/nginx
[root@server nginx-1.14.2]# netstat -nutpl|grep -w 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 28283/nginx: master
命令行查看版本号和服务器名称
[root@server nginx-1.14.2]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: 666/8.8.8
Date: Sat, 05 Jan 2019 06:32:53 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sat, 05 Jan 2019 06:29:31 GMT
Connection: keep-alive
ETag: "5c304ecb-264"
Accept-Ranges: bytes
访问一个不存在的页面查看信息
[root@server nginx-1.14.2]# curl -I http://127.0.0.1/b.txt
HTTP/1.1 404 Not Found
Server: 666/8.8.8
Date: Sat, 05 Jan 2019 06:37:15 GMT
Content-Type: text/html
Content-Length: 160
Connection: keep-alive
编写一个没有权限的页面进行访问
[root@server nginx-1.14.2]# echo "hello world" > /usr/local/nginx/html/a.txt
[root@server nginx-1.14.2]# chmod 000 /usr/local/nginx/html/a.txt
[root@server nginx-1.14.2]# ls -l /usr/local/nginx/html/a.txt
----------. 1 root root 12 1月 5 14:41 /usr/local/nginx/html/a.txt
[root@server nginx-1.14.2]# curl -I http://127.0.0.1/a.txt
HTTP/1.1 403 Forbidden
Server: 666/8.8.8
Date: Sat, 05 Jan 2019 06:41:59 GMT
Content-Type: text/html
Content-Length: 160
Connection: keep-alive