实战Nginx源码编译安装与配置

简介:

实验环境: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

网页测试:

wKiom1fUs5zzJAsFAAB47bHrlCQ985.png



    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

测试:

wKioL1fUtVSjZ7iDAAB-wsiNMFk954.png



    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

测试:  


wKiom1fUt_mh0PFBAAAe7eYWgD8760.png


wKioL1fUt_mT8JW1AAAfzYqAJnc717.png



    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;

        }

    }


测试:

wKioL1fUutnhWwlRAAAwW6kZaoA484.png



    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

测试:


wKiom1fUveOx9At8AABmC0KMUc4920.png


wKioL1fUveKTnLWaAAA0Fyo43JM386.png


本文转自willis_sun 51CTO博客,原文链接:http://blog.51cto.com/willis/1851558,如需转载请自行联系原作者

相关文章
|
3月前
|
编解码 应用服务中间件 Linux
centos配置nginx-rtmp实现ffmpeg转码rtsp为rtmp视频流
centos配置nginx-rtmp实现ffmpeg转码rtsp为rtmp视频流
320 1
|
7月前
|
应用服务中间件 Linux 网络安全
Centos 8.0中Nginx配置文件和https正书添加配置
这是一份Nginx配置文件,包含HTTP与HTTPS服务设置。主要功能如下:1) 将HTTP(80端口)请求重定向至HTTPS(443端口),增强安全性;2) 配置SSL证书,支持TLSv1.1至TLSv1.3协议;3) 使用uWSGI与后端应用通信(如Django);4) 静态文件托管路径设为`/root/code/static/`;5) 定制错误页面(404、50x)。适用于Web应用部署场景。
726 87
|
7月前
|
负载均衡 应用服务中间件 nginx
Nginx配置与命令
Nginx 是一款高性能的 HTTP 和反向代理服务器,其配置文件灵活且功能强大。本文介绍了 Nginx 配置的基础结构和常用指令,包括全局块、Events 块、HTTP 块及 Server 块的配置方法,以及静态资源服务、反向代理、负载均衡、HTTPS 和 URL 重写等功能实现。此外,还提供了常用的 Nginx 命令操作,如启动、停止、重载配置和日志管理等,帮助用户高效管理和优化服务器性能。
|
3月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
267 18
|
3月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
334 17
|
4月前
|
数据建模 应用服务中间件 PHP
配置nginx容器和php容器协同工作成功,使用ip加端口的方式进行通信
本示例演示如何通过Docker挂载同一宿主目录至Nginx与PHP容器,实现PHP项目运行环境配置。需注意PHP容器中监听地址修改为0.0.0.0:9000,并调整Nginx配置中fastcgi_pass指向正确的IP与端口。同时确保Nginx容器中/var/www/html权限正确,以避免访问问题。
配置nginx容器和php容器协同工作成功,使用ip加端口的方式进行通信
|
5月前
|
应用服务中间件 网络安全 nginx
配置Nginx以支持Websocket连接的方法。
通过上述配置,Nginx将能够理解WebSocket协议的特殊要求,代理Websocket流量到合适的后端服务器。注意,Websocket并不是HTTP,尽管它最初是通过HTTP请求启动的连接升级,因此保证Nginx了解并能够妥善处理这种升级流程是关键。
1122 10
|
4月前
|
Ubuntu 应用服务中间件 Linux
在Ubuntu上配置Nginx实现开机自启功能
至此,Nginx应该已经被正确地设置为开机自启。在Ubuntu中利用 `systemd`对服务进行管理是一种高效的方式,为系统管理员提供了强大的服务管理能力,包括但不限于启动、停止、重启服务,以及配置服务的开机自启动。通过这些简洁的命令,即使是对Linux不太熟悉的用户也能轻松地进行配置。
183 0
|
6月前
|
安全 应用服务中间件 Linux
Debian操作系统如何安装Nginx并开启HTTP2
本指南介绍了在Linux系统中通过源码编译安装Nginx的完整流程。首先更新软件包列表并安装必要的编译依赖,接着下载指定版本的Nginx源码包(如1.24.0),检查文件完整性后解压。随后通过配置脚本指定安装路径与模块(如HTTP SSL模块),执行编译和安装命令。最后创建软链接以便全局调用,并提供启动、停止及重载Nginx的命令,同时提醒注意安全组设置以确保正常访问。
|
6月前
|
安全 应用服务中间件 网络安全
Nginx SSL/TLS协议栈中配置深度解析与实践指南-优雅草卓伊凡
Nginx SSL/TLS协议栈中配置深度解析与实践指南-优雅草卓伊凡
376 0
Nginx SSL/TLS协议栈中配置深度解析与实践指南-优雅草卓伊凡