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. java基础系列--集合类库(一)

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/7229478.html 1.概述 Java的集合类库很是丰富,囊括了大部分的常见数据结构形式 ...

  2. (转)java中对集合对象list的几种循环访问总结

    Java集合的Stack.Queue.Map的遍历   在集合操作中,常常离不开对集合的遍历,对集合遍历一般来说一个foreach就搞定了,但是,对于Stack.Queue.Map类型的遍历,还是有一 ...

  3. 【HTML】DocType

    一.docType是什么 <!DOCTYPE> 声明不是 HTML 标签:它是指示 web 浏览器关于页面使用哪个 HTML 版本进行编写的指令. 在 HTML 4.01 中,<!D ...

  4. python注释中文

    原因: 如果文件里有非ASCII字符,需要在第一行或第二行指定编码声明. 解决办法: 必须是在第一行或是第二行加入 1)加上# -*- coding:utf-8 -*-之后就能成功使用中文注释了 2) ...

  5. Yii框架用ajax提交表单时候报错Bad Request (#400): Unable to verify your data submission.

    提交表单报400错误,提示 "您提交的数据无法验证"原来是csrf验证的问题,因为表单是自己写的,在Yii框架中,为了防止csrf攻击,对post的表单数据封装了CSRF令牌验证. ...

  6. XHTML 相对路径与绝对路径

    文件路径 文件路径就是文件在电脑(服务器)中的位置,表示文件路径的方式有两种:相对路径和绝对路径. 路径标识: 标识符号 说明 / 路径标识 . 当前目录 .. 上一层目录 "." ...

  7. BeanFactory VS FactoryBean

    1. BeanFactory BeanFactory定义了 IOC 容器的最基本形式,并提供了 IOC 容器应遵守的的最基本的接口,也就是Spring IOC 所遵守的最底层和最基本的编程规范.在   ...

  8. 谈谈渲染,玩玩nginx——前后端分离,转发请求到Tomcat的尝试

    一.谈谈"渲染" 相信好多人都挺听过"渲染"这个词,但不清楚它是什么意思?前端开发以为这是后端的活儿,后端开发以为是前端的事儿,推着推着就不了了之.其实渲染很简 ...

  9. Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    这个问题当然是找不到mysql的驱动类,可能是环境CLASSPATH有问题或者就是那个人没有加载jdbc的驱动.我在网上下载mysql-connector-java-5.0.8-bin.jar一个这个 ...

  10. LinkQueue(链队列)

    关于Node.h,请参考LinkStack #include"Node.h" template<typename ElemType> class LinkQueue { ...