设为首页 收藏本站
查看: 695|回复: 0

[经验分享] 实战Nginx源码编译安装与配置

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-9-12 09:48:01 | 显示全部楼层 |阅读模式
实验环境: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.准备
[iyunv@server1 ~]# yum remove httpd
[iyunv@server1 ~]# yum install gcc
[iyunv@server1 ~]# cd /mnt/
[iyunv@server1 mnt]# ls
nginx-1.9.14.tar.gz            ### 下载源码包(nginx-1.9.14.tar.gz)
[iyunv@server1 mnt]# tar -zxf nginx-1.9.14.tar.gz         #解压
[iyunv@server1 mnt]# ls
nginx-1.9.14  nginx-1.9.14.tar.gz
[iyunv@server1 mnt]# vim nginx-1.9.14/src/core/nginx.h
    #define NGINX_VER       "steven/"  #(版本隐藏)
[iyunv@server1 mnt]# vim nginx-1.9.14/auto/cc/gcc
   # debug
   #CFLAGS="$CFLAGS -g"    #关闭debug(由于使用gcc编译器,所以关闭gcc编译时安装的debug功能)
[iyunv@server1 mnt]# groupadd -g 666 nginx
[iyunv@server1 mnt]# useradd -s /sbin/nologin -d /opt/lnmp/ nginx -u 666 -g 666      #新建用户身份

    2. 安装
[iyunv@server1 mnt]# yum install pcre-devel openssl-devel -y
[iyunv@server1 mnt]# cd nginx-1.9.14/
[iyunv@server1 nginx-1.9.14]# ./configure \
> --prefix=/opt/lnmp/nginx \
> --with-http_ssl_module \
> --with-http_sub_module \
> --with-http_stub_status_module
[iyunv@server1 nginx-1.9.14]# make           ##编译
[iyunv@server1 nginx-1.9.14]# make install    ##安装
[iyunv@server1 nginx-1.9.14]# ls /opt/lnmp/nginx/       #安装完成查看           
conf  html  logs  sbin

    3.配置
[iyunv@server1 nginx-1.9.14]# cd /opt/lnmp/nginx/
[iyunv@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 }
[iyunv@server1 nginx]# vim /etc/profile
   export PATH=$PATH:/opt/lnmp/nginx/sbin    #添加nginx执行路径
[iyunv@server1 nginx]# source /etc/profile
[iyunv@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
[iyunv@server1 nginx]# nginx       #启动nginx
nginx -s reload     #重起
ngnix -s  stop      #关闭
测试:
[iyunv@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
[iyunv@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>
[iyunv@server1 nginx]# cd html/        #默认发布目录
[iyunv@server1 html]# ls
50x.html  index.html
网页测试:

wKiom1fUs5zzJAsFAAB47bHrlCQ985.jpg


    4.添加https

[iyunv@server1 nginx]# pwd
/opt/lnmp/nginx
[iyunv@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;
          }
        }
      }
[iyunv@server1 nginx]# cd /etc/pki/tls/certs/
[iyunv@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
[iyunv@server1 certs]# cp cert.pem /opt/lnmp/nginx/conf/
[iyunv@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
[iyunv@server1 certs]# nginx -s reload
测试:
wKioL1fUtVSjZ7iDAAB-wsiNMFk954.jpg


    5.虚拟主机
[iyunv@server1 nginx]# pwd
/opt/lnmp/nginx
[iyunv@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;
        }
        }
[iyunv@server1 nginx]# mkdir -p /virtual/willis/html
[iyunv@server1 nginx]# mkdir -p /virtual/linux/html
[iyunv@server1 nginx]# echo www.willis.com>/virtual/willis/html/index.html
[iyunv@server1 nginx]# echo www.linux.com>/virtual/linux/html/index.html
[iyunv@server1 nginx]# vim /etc/hosts
    172.25.254.1  www.willis.com
    172.25.254.1  www.linux.com                             
[iyunv@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
[iyunv@server1 nginx]# nginx -s reload
测试:  

wKiom1fUt_mh0PFBAAAe7eYWgD8760.jpg

wKioL1fUt_mT8JW1AAAfzYqAJnc717.jpg



    6.<<nginx 监控小插件>>网站信息统计
[iyunv@server1 nginx]# pwd
/opt/lnmp/nginx
[iyunv@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;
        }
    }

测试:

wKioL1fUutnhWwlRAAAwW6kZaoA484.jpg


    7.网页重写(自动转换到HTTPS)
[iyunv@server1 nginx]# pwd
/opt/lnmp/nginx
[iyunv@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;
    }
    }
[iyunv@server1 nginx]# mkdir -p /virtual/login/html
[iyunv@server1 nginx]# echo login.willis.com>/virtual/login/html/index.html
[iyunv@server1 nginx]# vim /etc/hosts
   login.willis.com
[iyunv@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
[iyunv@server1 nginx]# nginx -s reload
测试:

wKiom1fUveOx9At8AABmC0KMUc4920.jpg


wKioL1fUveKTnLWaAAA0Fyo43JM386.jpg




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-271084-1-1.html 上篇帖子: 配置nginx防止被盗链,提高资源利用率 下篇帖子: Nginx基本安全优化
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表