1.1 如何选择web服务器

在实际工作中,我们需要根据业务需求来选择合适的业务服务软件,有关web服务,选择建议如下:

  • 静态业务:若是高并发场景,尽量采用nginx或lighttpd,二者首选nginx
  • 动态业务:理论上采用nginx和apache均可,建议选择nginx,避免相同的业务服务软件多样化,增加额外维护成本。动态业务可以由nginx兼职做前端代理,在根据页面元素的类型或目录,转发到后端相应的服务器处理
  • 既有静态业务又有动态业务:采用nginx

1.2 安装nginx所需要的pcre库

安装pcre库是为了使nginx支持具备URI重写功能的rewrite模块,如果不安装pcre库,则nginx无法使用rewrite模块功能,nginx的rewrite模块功能几乎是企业应用必须的。安装pcre库的过程如下。 采用yum安装方式,推荐方法:

# yum install pcre pcre-devel -y

yum安装后检查版本

# rpm -qa pcre pcre-devel
pcre-devel-8.32-15.el7_2.1.x86_64
pcre-8.32-15.el7_2.1.x86_64

1.3 安装openssl-devel

nginx在使用HTTPS服务的时候要用到此模块,如果不安装openssl相关包,安装nginx的过程中会报错。

# yum install -y openssl openssl-devel

1.4 安装nginx

在实际工作中,选择稳定版时,尽量避免使用最新版本,选择比已出来的最新晚6-10个月的版本比较好。编译nginx软件时,可以使用./configure --help查看相关参数帮助。

# useradd nginx -s /sbin/nologin -M
# tar xf nginx-1.12.0.tar.gz
# cd nginx-1.12.0
# ./configure \
--user=nginx --group=nginx \
--prefix=/application/nginx-1.12.0 \
--with-http_stub_status_module \
--with-http_ssl_module
# make
# make install # ln -s /application/nginx-1.12.0 /application/nginx

1.5 启动并检查安装结果

启动前检查配置文件语法

# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.12.0/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.12.0/conf/nginx.conf test is successful

在启动前检查语法非常重要,可以防止因配置错误导致网站重启或重新加载配置等对用户的影响 启动nginx服务

# /application/nginx/sbin/nginx

可以通过curl命令检车是否浏览正常

# curl 127.0.0.1

也可以通过netstat -lnt|grep 80、lsof -i:80检查nginx服务器端口(默认80)来判断nginx是否已启动,或者通过ps -ef|grep nginx检查nginx服务进程来判断,当然最稳妥的方法还是通过url地址来检查。 

1.6 查看nginx编译时的参数

# /application/nginx/sbin/nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/application/nginx-1.12.0 --with-http_stub_status_module --with-http_ssl_module

1.7 systemctl启动配置

# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target [Service]
Type=forking
PIDFile=/application/nginx/logs/nginx.pid
ExecStartPre=/application/nginx/sbin/nginx -t -c /application/nginx/conf/nginx.conf
ExecStart=/application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true [Install]
WantedBy=multi-user.target

管理

# chmod +x /usr/lib/systemd/system/nginx.service
# systemctl enable nginx.service
# systemctl start nginx.service
# systemctl stop nginx.service
# systemctl reload nginx.service

2.1 配置虚拟主机的步骤

  1. 增加一个完整的server标签段到结尾处。注意要放在http的结束大括号前,也就是将server标签段放入http标签。
  2. 更改server_name及对应网页的root根目录,如果需要其他参数,可以增加或修改。
  3. 创建server_name域名对应网页的根目录,并建立测试文件,如果没有index首页,访问会出现403错误。
  4. 检查nginx配置文件语法,平滑重启nginx服务,快速检查启动结果。
  5. 域名做dns解析,并检查(ping域名看返回的IP是否正确)。
  6. 在浏览器中输入地址访问,用wget或curl接地址访问。

2.2 规范优化nginx配置文件

nginx的主配置文件为nginx.conf,主配置文件包含的所有虚拟主机的子配置文件会统一放入到vhosts目录中,虚拟主机的配置文件安装网站的域名或功能取名,例如www.conf、bbs.conf、blog.conf等。

这里使用参数include,把它放置在nginx中的http区块中,用法如下:

http{
...
include vhosts/*.conf; #包含vhosts下所有以conf结尾的文件
}

实施步骤如下:

# cd /application/nginx/conf/
# mkdir vhosts
# vim nginx.conf
...
http{
...
include vhosts/*.conf;
} # cd vhosts
# vim www.heboan.conf
server {
listen 80;
server_name www.heboan.com;
location / {
root html/www;
index index.html index.htm;
}
} server {
listen 80;
server_name bbs.heboan.com;
location / {
root html/bbs;
index index.html index.htm;
}
}

2.3 虚拟主机的别名配置

虚拟主机别名,就是为了虚拟主机设置 除了主域名以外的一个或多个域名名字,这样就能实现用户访问的多个域名对应同一个虚拟主机网站的功能。以www.heboan.com域名的虚拟主机为例,为其增加一个别名heboan.com,使得访问heboan.com时,在该域名出现的网站内容和访问www.heboan.com得到的结果是一样的,这是企业场景中活生生的基本需求配置:

server {
listen 80;
server_name www.heboan.com heboan.com;
location / {
root html/www;
index index.html index.htm;
}
}

3.1 添加模块(非覆盖安装)

nginx已经安装好了,现在需要添加一个未被编译安装的模块,需要重新编译,这里以nginx-module-vts模块为例

nginx-module-vts可查询配置的虚拟主机通讯状态模块,类似stub_status_module模块,并且比这个统计的粒度更细,默认未包含在nginx的发布包中,需要单独下载:

# git clone git://github.com/vozlt/nginx-module-vts.git

查看原来编译时都带了那些参数

# /application/nginx/sbin/nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx
--prefix=/application/nginx-1.12.0
--with-http_stub_status_module
--with-http_ssl_module

添加模块nginx-module-vts

# cd nginx-1.12.0
# ./configure --user=nginx --group=nginx \
--prefix=/application/nginx-1.12.0 \
--with-http_stub_status_module \
--with-http_ssl_module \
--add-module=/root/tools/nginx-module-vts # make
# 不要make install,否则会覆盖安装

关闭nginx

# systemctl stop nginx.service

替换二进制文件

# cp /application/nginx/sbin/nginx /application/nginx/sbin/nginx.bak
# cp ./objs/nginx /application/nginx/sbin/

启动nginx,查看模块

# systemctl start nginx.service
# /application/nginx/sbin/nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx
--prefix=/application/nginx-1.12.0
--with-http_stub_status_module
--with-http_ssl_module
--add-module=/root/tools/nginx-module-vts

模块添加成功

配置nginx-module-vts

# vim /application/nginx/conf/nginx.conf
http {
...
vhost_traffic_status_zone;
} # vim /application/nginx/conf/vhosts/status.heboan.conf
server {
listen 80;
server_name status.heboan.com; location /{
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
access_log off;
}
}

检查配置是否正确,重载配置

# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.12.0/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.12.0/conf/nginx.conf test is successful # /application/nginx/sbin/nginx -s reload

浏览器访问http://status.heboan.com

如果我们只是想使用官方的status,只需要改下status.heboan.conf

server {
listen 80;
server_name status.heboan.com; location /{
stub_status on;
access_log off;
}
}

  

1、编译安装Nginx的更多相关文章

  1. centos系统编译安装nginx+php环境另加独立mysql教程

    以前看过的安装nginx+php环境都带了mysql数据库了,这个是因为很多站长都是nginx+php+mysql都在同一台服务器了,那么今天我们是单独处理了,一个是nginx+php环境,然后mys ...

  2. Centos7 编译安装 Nginx PHP Mariadb Memcached 扩展 ZendOpcache扩展 (实测 笔记 Centos 7.3 + Mariadb 10.1.20 + Nginx 1.10.2 + PHP 7.1.0 + Laravel 5.3 )

    环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7-x86_64-Minimal-1611.iso 安装步骤: 1.准备 1.0 查看硬 ...

  3. CentOS7 编译安装 Nginx (实测 笔记 Centos 7.0 + nginx 1.6.2)

    环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7.0-1406-x86_64-DVD.iso 安装步骤: 1.准备 1.1 显示系统版 ...

  4. linux软件管理之------编译安装nginx服务器并手动编写自动化运行脚本

    红帽系列的 linux软件管理分为三类:1. rpm 安装软件.2. yum 安装软件.3. 源码包编译安装.前面两种会在相关专题给出详细讲解.源码包的编译安装是非常关键的,我们知道linux的相关版 ...

  5. Mac Pro 编译安装 Nginx 1.8.1

    #下载相关源码包,统一放到 /usr/local/src 目录下: http://nginx.org/download/nginx-1.8.1.tar.gz http://zlib.net/zlib- ...

  6. 【转】linux 编译安装nginx,配置自启动脚本

    linux 编译安装nginx,配置自启动脚本 本文章来给各位同学介绍一篇关于linux 编译安装nginx,配置自启动脚本教程,希望有需要了解的朋友可一起来学习学习哦. 在公司的suse服务器装ng ...

  7. linux 编译安装nginx,配置自启动脚本

    本文章来给各位同学介绍一篇关于linux 编译安装nginx,配置自启动脚本教程,希望有需要了解的朋友可一起来学习学习哦. 在公司的suse服务器装nginx,记录下安装过程: 参照这篇文章:Linu ...

  8. 编译安装nginx并修改版本头信息—参考实例

    今天做实验的时候,想起我那台yum安装的nginx+php-fpm+mysql服务器上的nginx版本有点低了,并且还要加两个第3方模块,就去nginx官网下载了最新稳定版nginx-1.0.6,好了 ...

  9. ubuntu 12.04 编译安装 nginx

    下载源码包 nginx 地址:http://nginx.org/en/download.html 编译前先安装两个包: 直接编译安装会碰到缺少pcre等问题,这时候只要到再安装两个包就ok sudo ...

  10. ubuntu 14.04 编译安装 nginx

    下载源码包 nginx 地址:http://nginx.org/en/download.html  下载nginx 1.4.7 编译前先安装两个包: 直接编译安装会碰到缺少pcre等问题,这时候只要到 ...

随机推荐

  1. tomcat优化总结【持续更新】

    配置优化 <Connector port=" maxThreads=" URIEncoding="UTF-8" maxKeepAliveRequests= ...

  2. 第八周 yukun 20155335

  3. 基本控件文档-UIButton属性---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/Ch ...

  4. vue清空input file

    input file是只读的,给form一个id,用form.reset()干掉里面input的值 document.getElementById("uploadForm")&am ...

  5. JAVA Frame 响应窗口关闭事件

    /* * To change this license header, choose License Headers in Project Properties. * To change this t ...

  6. js_layer弹窗的使用和总结

    2018-04-10 一张呈现给用户的网页,会有很多种交互,比如连不上网络,用户点击按钮时向后台请求数据不成功等等.像这些情况,用户是看不见的, 要给用户更好的体验,在特定的时间,给客户反馈内容.实时 ...

  7. Python eval 函数说明

    eval(str [,globals [,locals ]]) -- 函数将字符串str当成有效Python表达式来求值,并返回计算结果. 例 :  eval('3+4')         ==> ...

  8. hdu 2795 Billboard(线段树+单点更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 Billboard Time Limit: 20000/8000 MS (Java/Others ...

  9. Win7蓝屏代码0X0000007B可能是SATA mode问题

    Win7蓝屏代码0X0000007B可能是硬盘模式的问题,我进入BIOS把SATA的mode从Enhanced改为Compatible(及IDE兼容模式)结果系统可以顺利启动没有问题.       从 ...

  10. Makefile系列之三 : 变量

    一.变量的基础 变量在声明时需要给予初值,而在使用时,需要给在变量名前加上“$”符号,但最好用小括号“()”或是大括号“{}”把变量给包括起来.如果你要使用真实的“$”字符,那么你需要用“$$”来表示 ...