实验环境:RHEL7.0 server1.example.com 172.25.254.1
实验内容: 1.准备
2. 安装
3.配置
4.添加https
5.虚拟主机
6.<<nginx 监控小插件>>网站信息统计
7.网页重写(自动转换到HTTPS)
源码包:nginx-1.9.14.tar.gz
1.准备
[root@server1 ~]# yum remove httpd
[root@server1 ~]# yum install gcc
[root@server1 ~]# cd /mnt/
[root@server1 mnt]# ls
nginx-1.9.14.tar.gz ### 下载源码包(nginx-1.9.14.tar.gz)
[root@server1 mnt]# tar -zxf nginx-1.9.14.tar.gz #解压
[root@server1 mnt]# ls
nginx-1.9.14 nginx-1.9.14.tar.gz
[root@server1 mnt]# vim nginx-1.9.14/src/core/nginx.h
#define NGINX_VER "steven/" #(版本隐藏)
[root@server1 mnt]# vim nginx-1.9.14/auto/cc/gcc
# debug
#CFLAGS="$CFLAGS -g" #关闭debug(由于使用gcc编译器,所以关闭gcc编译时安装的debug功能)
[root@server1 mnt]# groupadd -g 666 nginx
[root@server1 mnt]# useradd -s /sbin/nologin -d /opt/lnmp/ nginx -u 666 -g 666 #新建用户身份
2. 安装
[root@server1 mnt]# yum install pcre-devel openssl-devel -y
[root@server1 mnt]# cd nginx-1.9.14/
[root@server1 nginx-1.9.14]# ./configure \
> --prefix=/opt/lnmp/nginx \
> --with-http_ssl_module \
> --with-http_sub_module \
> --with-http_stub_status_module
[root@server1 nginx-1.9.14]# make ##编译
[root@server1 nginx-1.9.14]# make install ##安装
[root@server1 nginx-1.9.14]# ls /opt/lnmp/nginx/ #安装完成查看
conf html logs sbin
3.配置
[root@server1 nginx-1.9.14]# cd /opt/lnmp/nginx/
[root@server1 nginx]# vim conf/nginx.conf
2 user nginx nginx;##用户和组,可以只写用户
3 worker_processes 2;##cpu个数,不能超过lscpu显示cpu个数
12 events {
13 use epoll; ##nginx epoll 采用异步非阻塞模式 apache --select 同步阻塞机制 io复用模型类型
14 worker_connections 4096;##连接数
15 }
[root@server1 nginx]# vim /etc/profile
export PATH=$PATH:/opt/lnmp/nginx/sbin #添加nginx执行路径
[root@server1 nginx]# source /etc/profile
[root@server1 nginx]# nginx -t #检查nginx有无错误
nginx: the configuration file /opt/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/lnmp/nginx/conf/nginx.conf test is successful
[root@server1 nginx]# nginx #启动nginx
nginx -s reload #重起
ngnix -s stop #关闭
测试:
[root@server1 nginx]# curl -I localhost ##检测http协议提供程序
HTTP/1.1 200 OK
Server: willis/
Date: Sun, 11 Sep 2016 01:26:39 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sun, 11 Sep 2016 01:19:44 GMT
Connection: keep-alive
ETag: "57d4b130-264"
Accept-Ranges: bytes
[root@server1 nginx]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@server1 nginx]# cd html/ #默认发布目录
[root@server1 html]# ls
50x.html index.html
网页测试:
4.添加https
[root@server1 nginx]# pwd
/opt/lnmp/nginx
[root@server1 nginx]# vim conf/nginx.conf
# HTTPS server #开启HTTPS功能
server {
listen 443 ssl;
server_name localhost;
ssl_certificate cert.pem;
ssl_certificate_key cert.pem; #修改证书名
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
}
[root@server1 nginx]# cd /etc/pki/tls/certs/
[root@server1 certs]# make cert.pem #新建证书
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:shaanxi
Locality Name (eg, city) [Default City]:xi'an
Organization Name (eg, company) [Default Company Ltd]:redhat
Organizational Unit Name (eg, section) []:Linux
Common Name (eg, your name or your server's hostname) []:localhost
Email Address []:upsun_sun@163.com
[root@server1 certs]# cp cert.pem /opt/lnmp/nginx/conf/
[root@server1 certs]# nginx -t
nginx: the configuration file /opt/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/lnmp/nginx/conf/nginx.conf test is successful
[root@server1 certs]# nginx -s reload
测试:
5.虚拟主机
[root@server1 nginx]# pwd
/opt/lnmp/nginx
[root@server1 nginx]# vim conf/nginx.conf
在http{}中添加
server {
listen 80;
server_name www.willis.com;
location / {
root /virtual/willis/html;
index index.html;
}
}
server {
listen 80;
server_name www.linux.com;
location / {
root /virtual/linux/html;
index index.html;
}
}
[root@server1 nginx]# mkdir -p /virtual/willis/html
[root@server1 nginx]# mkdir -p /virtual/linux/html
[root@server1 nginx]# echo www.willis.com>/virtual/willis/html/index.html
[root@server1 nginx]# echo www.linux.com>/virtual/linux/html/index.html
[root@server1 nginx]# vim /etc/hosts
172.25.254.1 www.willis.com
172.25.254.1 www.linux.com
[root@server1 nginx]# nginx -t
nginx: the configuration file /opt/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/lnmp/nginx/conf/nginx.conf test is successful
[root@server1 nginx]# nginx -s reload
测试:
6.<<nginx 监控小插件>>网站信息统计
[root@server1 nginx]# pwd
/opt/lnmp/nginx
[root@server1 nginx]# vim conf/nginx.conf
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /message { # 添加
stub_status on;
access_log off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
测试:
7.网页重写(自动转换到HTTPS)
[root@server1 nginx]# pwd
/opt/lnmp/nginx
[root@server1 nginx]# vim conf/nginx.conf
server {
listen 80;
server_name login.willis.com;
rewrite ^(.*)$ https://$host$1 permanent;
location / {
root /virtual/login/html;
index index.html;
}
}
[root@server1 nginx]# mkdir -p /virtual/login/html
[root@server1 nginx]# echo login.willis.com>/virtual/login/html/index.html
[root@server1 nginx]# vim /etc/hosts
login.willis.com
[root@server1 nginx]# nginx -t
nginx: the configuration file /opt/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/lnmp/nginx/conf/nginx.conf test is successful
[root@server1 nginx]# nginx -s reload
测试:
本文转自willis_sun 51CTO博客,原文链接:http://blog.51cto.com/willis/1851558,如需转载请自行联系原作者