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. 使用python脚本配置zabbix发送报警邮件

    #前提得在zabbix_server配置文件中配置触发脚本的目录,例如,我配置的在/usr/local/zabbix/server/scripts目录下 编写python脚本如下 因为我的服务器在腾讯 ...

  2. Value does not fall within the expected range 值不在预期的范围内

    用vs2012 打开web.config时,提示如下错误:“Value does not fall within the expected range”; 中文提示:“值不在预期的范围内” 解决方案: ...

  3. 在Linux系统里运行shutdown.sh命令关闭Tomcat时出现错误提示

    服务器:linnux 5.5 64位,已安装好 jdk: Tomcat版本:apache-tomcat-7.0.53 操作软件:Xshell 4(Free for Home / School) 刚开始 ...

  4. ubuntu 玩转 nodejs

    安装nginx 首先添加nginx_signing.key(必须,否则出错) $ wget http://nginx.org/keys/nginx_signing.key $ sudo apt-key ...

  5. Git 常用命令(二)

    用 git init 在目录中创建新的 Git 仓库.  $ mkdir test $ cd test/ $ git init Initialized empty Git repository in ...

  6. 修改ES使用root用户运行

    默认ES不允许使用root用户运行,如果使用root会报如下图的错误: ,通常建议创建elsearch用户并使用该用户运行ES.但如果必须使用root用户时,按如下设置即可: 1.启动是使用如下命令 ...

  7. Django【设计】settings方案

      配置文件: 目标:配置文件,默认配置和手动配置分开,参考django的配置文件方案,默认配置文件放在内部,只让用户做常用配置   /bin/settings.py(手动配置) PLUGIN_ITE ...

  8. C++ Primer读书笔记

    以前阅读学习C++ Primer时的习题代码(当时代码风格格式比较渣): https://github.com/liyuan989/exercise/tree/master/c%2B%2B%20pri ...

  9. 10 个打造 React.js App 的最佳 UI 框架

    10 个打造 React.js App 的最佳 UI 框架 在本文中,我们将分享一些助你打造 React.js App 最佳的 UI 框架.它们具备你所需要的基本 React 组件,以及易用的 API ...

  10. MD5加密学习

    MD5(Message Digest --消息摘要算法)算法是一种散列(hash)算法(摘要算法,指纹算法),不是一种加密算法(易错),任何长度的任意内容都可以用MD5计算出散列值.主要作用是[验明“ ...