lnmp 架构 第一篇

nginx 源码安装

nginx的安装包:nginx-1.12.0.tar.gz


建议安装前的修改:

  1. 在nginx的解压包中修改文件nginx-1.12.0/src/core/nginx.h:去掉nginx后面的NGINX_VERSION。此举是为了不显示nginx的版本,提高安全性。

#define NGINX_VER "nginx/"NGINX_VERSION

  1. 在nginx的解压包中修改文件nginx-1.12.0/auto/cc/gcc:注释掉debug的编译方式

# debug

#CLAGS="$CFLAGS -g"


nginx的安装:

  1. # ./configure

没有error即为编译成功

如果有error则可根据error解决依赖性

例如:

./configure: error: SSL modules require the OpenSSL library.

You can either do not enable the modules, or * install the OpenSSL library *into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl= option.

根据error我安装了openssl-devel
  1. # make
  2. # make install

安装结束后,由./configure时--prefix指定的路径确定nginx的安装位置

/usr/local/lnmp/nginx/sbin/nginx

为了使用方便建议创建超连接:(超连接时使用绝对路径)

[root@server1 sbin]# ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin/

nginx的启动:

  1. 检查nginx:(没有error即可启动nginx)

# nginx -t

  1. 启动nginx
  2. 查看nginx的监听端口:80端口
[root@server1 ~]# netstat -antlp |grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:*
LISTEN 6187/nginx
  1. 浏览器访问安装nginx的主机IP(启动浏览器的主机要与启动nginx的主机能相互ping通),显示nginx的默认页

nginx的默认发布目录:/usr/local/lnmp/nginx/html

可在nginx的默认发布目录中创建新的网页文件,浏览器访问* IP/文件名 *及可

nginx的配置文件:/usr/local/lnmp/nginx/conf/nginx.conf

定义nginx运行的用户及用户组
user	nginx nginx;

nginx检查报错--->查看发现没有nginx用户--->建立nginx用户在刷新nginx配置文件

nginx指定用户及用户组的前提是指定用户及用户组必须已经存在

[root@server1 conf]# nginx -t
nginx: [emerg] getpwnam("nginx") failed in /usr/local/lnmp/nginx/conf/nginx.conf:2
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test failed
[root@server1 conf]# id nginx
id: nginx: No such user
[root@server1 conf]# useradd nginx
[root@server1 conf]# id nginx
uid=500(nginx) gid=500(nginx) groups=500(nginx)
[root@server1 conf]# nginx -t
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx -s reload
nginx进程数,建议设置为等于CPU总核心数。
worker_processes 8;

nginx配置文件默认为worker——process 1;查看进程发现启动一个master进程和一个worker进程

 1080 ?        Ss     0:00 nginx: master process nginx
1081 ? S 0:00 nginx: worker process
1090 pts/0 R+ 0:00 ps ax

修改后再次查看进程,发现启动一个master进程和多个worker进程

 1080 ?        Ss     0:00 nginx: master process nginx
1111 ? S 0:00 nginx: worker process
1112 ? S 0:00 nginx: worker process
1113 ? S 0:00 nginx: worker process
1114 ? S 0:00 nginx: worker process
1115 ? S 0:00 nginx: worker process
1116 ? S 0:00 nginx: worker process
1117 ? S 0:00 nginx: worker process
1118 ? S 0:00 nginx: worker process
1119 pts/0 R+ 0:00 ps ax

nginx是以多进程的方式来工作的,nginx启动后会有一个master进程和多个worker进程。master进程用来监控worker进程包括:接受外界信号,并向worker进程发送信号;监控worker的运行状态,当某个worker进程异常退出时,master进程会重新fork出一个新的worker进程。而基本的网络事件则交由worker进程处理,一个请求只能有一个worker进程处理。

nginx的处理过程大概时这样的:master进程会先建立好需要listen的socket,并fork子进程workers来即成相应的socket(注意:每个worker进程的socket并不是同一个socket,只是他们都会监听在同一个IP地址上的同一个端口)

工作模式与连接数上限,即nginx可以打开的最多文件描述符数目。
events {
worker_connections 1024;
}

worker_connecions和worker_rlimit_nofile

查看日志,有一个[warn]: 3660#0: 20000 worker_connections are more than open file resource limit: 1024 !!

原来安装好nginx之后,默认最大的并发数为1024,如果你的网站访问量过大,已经远远超过1024这个并发数,那你就要修改worker_connecions这个值 ,这个值越大,并发数也有就大。当然,你一定要按照你自己的实际情况而定,也不能设置太大,不能让你的CPU跑满100%。

所以,当你修改提高了配置文件中的worker_connections值,然后重启nginx,你就会在日志里发现,最前面我们讲到的这一个warn警告提示,大概的意思就是: 20000并发连接已经超过了打开文件的资源限制:1024!在这种情况下,我们就要修改配置文件,添加一行来解除这个限制,这就好像是apache中的ServerLimit。

打开配置文件在"event"这行上面添加这一行:

worker_rlimit_nofile xxxxx; ####Specifies the value for maximum file descriptors that can be opened by this process.

注意:设置了这个后,你修改worker_connections值时,是不能超过worker_rlimit_nofile的这个值,不然又会有前面的那个warn提示。

保存配置文件,退出重启nginx。

如果nginx 中worker_connections 值设置是1024,worker_processes 值设置是4,按反向代理模式下最大连接数的理论计算公式:

最大连接数 = worker_processes * worker_connections/4

查看相关资料,生产环境中worker_connections 建议值最好超过9000,计划将一台nginx 设置为10240,再观察一段时间。

注明:这是我在网上看见的一个错误解析,结合这个会很好理解这两个参数
keepalive超时时间
keepalive_timeout  65;
虚拟主机配置
server {
listen 80;
server_name www.test.org; location / {
root /var/www/html;
index index.html;
}
}

这段很简单,但是我要解释的是server_name。server_name指定访问域名,当我修改完配置文件,并在/var/www/html中创建index.html文件之后,浏览器访问www.test.org失败,访问IP则是nginx的默认页。然后我在浏览器坐在主机上进行相应的域名解析之后,在此访问域名,浏览器成功显示了/var/www/html/index.html的内容。与访问IP相同,访问 * 域名/文件 * 则显示/var/www/html目录下相应的网页文件。

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 certs]# pwd
/etc/pki/tls/certs
[root@server1 certs]# ll Makefile
-rw-r--r--. 1 root root 2242 Sep 27 2013 Makefile
[root@server1 certs]# make cert.pem
umask 77 ; \
PEM1=`/bin/mktemp /tmp/openssl.XXXXXX` ; \
PEM2=`/bin/mktemp /tmp/openssl.XXXXXX` ; \
/usr/bin/openssl req -utf8 -newkey rsa:2048 -keyout $PEM1 -nodes -x509 -days 365 -out $PEM2 -set_serial 0 ; \
cat $PEM1 > cert.pem ; \
echo "" >> cert.pem ; \
cat $PEM2 >> cert.pem ; \
rm -f $PEM1 $PEM2
Generating a 2048 bit RSA private key
...................................................................................+++
...............+++
writing new private key to '/tmp/openssl.0nER84'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
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]:school
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:student
Email Address []:960482927@qq.com
[root@server1 certs]# ls
ca-bundle.crt ca-bundle.trust.crt cert.pem make-dummy-cert Makefile renew-dummy-cert
[root@server1 certs]# mv cert.pem /usr/local/lnmp/nginx/conf/
[root@server1 certs]# cd /usr/local/lnmp/nginx/conf/
[root@server1 conf]# ls
cert.pem fastcgi_params koi-win nginx.conf scgi_params.default win-utf
fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params
fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default
[root@server1 conf]# nginx -t
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx -s reload

打开浏览器访问https://172.25.44.1即可。

nginx的重定向
server {
listen 80;
server_name www.black.org; rewrite ^(.*) http://www.write.org$1 permanent;
} server {
listen 80;
server_name www.write.org; location / {
root /web2;
index index.html;
}
}
将所有对www.black.org的访问重定向到www.write.org。例如将www.black.org/test.html重定向为www.write.org/test.html。
nginx的负载均衡
upstream linux {
server 172.25.44.2:80;
server 172.25.44.3:80; server 172.25.44.1:8080 backup;
}
server {
listen 80;
server_name www.write.org; location / {
proxy_pass http://linux;
}
}

172.25.44.2和172.25.44.3上分别开启httpd服务,浏览器访问www.write.org

[root@foundation44 images]# for i in {1..10};do curl www.write.org;done

server3

server3

server3

server2

server2

server3

server2

server2

server3

server2

[root@foundation44 images]# for i in {1..10};do curl www.write.org;done

server2

server3

server3

server3

server2

server2

server3

server2

server2

server3

> ```
---
> 当一台http服务器挂掉后所有的请求就交由其他的http服务器处理。当所有的http服务器都挂掉后我们还可以使用备用web网页即172.25.44.1:8080来向用户说明情况,避免用户的不舒适体验。

lnmp架构(第一篇)的更多相关文章

  1. 分布式架构--第一篇--项目拆分(maven命令生成多模块项目)

    预览生成的项目结构: ying-yue-parent // 顶级总编译控制模块 ying-yue-lib // jar模块 ying-yue-model // 模型对象模块 ying-yue-dao ...

  2. 部署LNMP架构及其应用

    部署企业LNMP架构 (一)首先安装nginx服务,具体请见另一篇关于nginx的博文. (二)安装MySQL数据库 .安装前准备 [root@localhost ~]# rpm -e mysql-s ...

  3. LNMP架构介绍、MySQL和PHP安装、Nginx介绍

     6月6日任务  12.1 LNMP架构介绍12.2 MySQL安装12.3/12.4 PHP安装12.5 Nginx介绍 扩展Nginx为什么比Apache Httpd高效:原理篇 http://w ...

  4. [译]PrestaShop开发者指南 第一篇 基础

    # 第一篇 基础 PS(PrestaShop简称)一开始就设定了能够在它的基础上很简单的构建第三方模块的机制,让它成为一款具有极高定制性的电子商务软件. PS的可以在三个方面进行定制: * 主题 * ...

  5. IIS负载均衡-Application Request Route详解第一篇: ARR介绍(转载)

    IIS负载均衡-Application Request Route详解第一篇: ARR介绍 说到负载均衡,相信大家已经不再陌生了,本系列主要介绍在IIS中可以采用的负载均衡的软件:微软的Applica ...

  6. RabbitMQ学习总结 第一篇:理论篇

    目录 RabbitMQ学习总结 第一篇:理论篇 RabbitMQ学习总结 第二篇:快速入门HelloWorld RabbitMQ学习总结 第三篇:工作队列Work Queue RabbitMQ学习总结 ...

  7. 第一篇 SQL Server安全概述

    本篇文章是SQL Server安全系列的第一篇,详细内容请参考原文. Relational databases are used in an amazing variety of applicatio ...

  8. Winform常用开发模式第一篇

    Winform常用开发模式第一篇 上一篇博客最后我提到“异步编程模型”(APM),之后本来打算整理一下这方面的材料然后总结一下写篇文章与诸位分享,后来在整理的过程中不断的延伸不断地扩展,发现完全偏离了 ...

  9. 微信小程序教程(第一篇)

    目录 第一篇小程序概述 第二篇如何注册接入小程序及搭建开发环境 第三篇小程序的架构及实现机制,信道服务及会话管理 第四篇小程序开发基本框架及其限制与优化 第五篇小程序开发项目实例,测试及发布 .... ...

随机推荐

  1. 【Splay】例题

    营业额统计 题目背景 HNOI2002 DAY2 T2 题目描述 Tiger 最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger 拿出了公司 ...

  2. The request sent by the client was syntactically incorrect问题解决

    The request sent by the client was syntactically incorrect意思嘛,google翻译一下 通过日志不难看出,是由参数不匹配造成的. 所以对于Da ...

  3. 18. leetcode 387. First Unique Character in a String

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  4. 单页面应用(spa)引入百度地图(Cannot read property 'dc' of undefined)

    难点介绍 引入百度地图的时候,用原生的获取不到dom节点. ( var mapEle = document.getElementById(testApi): var map = new BMap.Ma ...

  5. ASP.NET-页面间的数据传递

    暑假期间做项目时遇到相关问题,总结如下,与大家分享 1.通过查询字符串传递 这种方式是将参数附加在网址的后面,传递数据简单,但容易暴露,一般用于传递一些简单的数据. 例如,在Default1.aspx ...

  6. Saltstack的部署及其详解

    https://repo.saltstack.com/ Saltstack简介: salt是一个多平台基础设施管理工具通常只用在linux上,使用那个轻量级的通讯器,ZN用python写成的批量管理工 ...

  7. nstallation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED报这个错的原因???

    [2015-06-05 20:37:51 - 05ListView列表控件] ------------------------------ [2015-06-05 20:37:51 - 05ListV ...

  8. [stm32F429-DISCO-HAL] 1.先说说关于stm32Cube的一些事情。然后,Start with it...

    目前,我觉得STM32CUBE最大的方便在于,可以使用STM32CubeMX软件来图形化配置外设.首先贴出官网的PDF,Getting started with STM32CubeF4 firmwar ...

  9. 前端到后台ThinkPHP开发整站(7)

    今晚我继续这个项目的前台开发,把前台的做出来了,现在项目进行一个收尾工作了,还有栏目页和一个文章页的开发,做完这两个算是完成了.说到这里感觉有点松懈了,把剩下两个功能页面做完在吹吧,先看看今天弄的代码 ...

  10. Python 第五天

    函数式编程 高阶函数 1.变量可指向函数 func = abs print(func(-1)) #1 2.函数可以作为参数传入另外的函数这也就是高阶函数 def add(x,y,func): prin ...