lnmp架构之nginx的安装

来源:互联网 发布:淘宝的机器人 编辑:程序博客网 时间:2024/05/29 17:02
#######nginx安装:#####注意nginx下有html目录!
[root@server2 nginx-1.12.0]# cd src/core/
[root@server2 core]# vim nginx.h
14 #define NGINX_VER          "nginx"
:wq
[root@server2 nginx-1.12.0]# vim auto/cc/gcc
171 # debug
172 #CFLAGS="$CFLAGS -g"
:wq
[root@server2 nginx-1.12.0]# ./configure --help #可查看./configure命令的可选参数
[root@server2 nginx-1.12.0]# ./configure --prefix=/usr/local/lamp/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
error总结:
1.checking for OS
 + Linux 2.6.32-431.el6.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found  #报错未安装gcc
2./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option. #未安装pcre-devel
3./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=<path> option.#未安装openssl-devel
安装成功后提示
...
creating objs/Makefile
...
[root@server2 nginx-1.12.0]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[root@server2 nginx-1.12.0]# make
[root@server2 nginx-1.12.0]# make install
[root@server2 nginx-1.12.0]# cd /usr/local/lnmp/nginx/
[root@server2 nginx]# ls
conf  html  logs  sbin
[root@server2 nginx]# ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin/ #软连接
[root@server2 nginx]# nginx #开启服务
[root@server2 nginx]# 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@server2 nginx]# netstat -antlpe | grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                 LISTEN      0          17362      6081/nginx     
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 13 May 2017 15:08:32 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sat, 13 May 2017 15:02:45 GMT
Connection: keep-alive
ETag: "59172015-264"
Accept-Ranges: bytes
测试:在浏览器中输入ip
####nginx 的基本操作#######
[root@server2 nginx]# vim conf/nginx.conf
  3 worker_processes  2; #nginx要开启的进程数
  4 worker_cpu_affinity 01 10; #为每个worker进程绑定一个固定的cpu
[root@server2 nginx]# ps aux
 nobody    6082  0.0  0.4  45656  2084 ?        S    23:05   0:00 nginx: wo
[root@server2 nginx]# useradd -u 800 nginx
[root@server2 nginx]# vim conf/nginx.conf
 2 user    nginx nginx;
[root@server2 nginx]# nginx -s reload #重新加载nginx配置文件
[root@server2 nginx]# ps aux
root      6081  0.0  0.3  45212  1884 ?        Ss   23:05   0:00 nginx: master p
nginx     6101  0.0  0.3  45676  1836 ?        S    23:22   0:00 nginx: worker p
nginx     6102  0.0  0.3  45676  1856 ?        S    23:22   0:00 nginx: worker p
[root@server2 nginx]# vim conf/nginx.conf
 13     worker_connections  1024;#最大连接数
可通过ulimit -a 命令查看系统最大连接数
###########################
[root@server2 nginx]# ulimit -a
[root@server2 nginx]# free -m
[root@server2 nginx]# :(){ :|: & };:
[root@server2 nginx]# vim /etc/security/limits.conf
 51 nginx           -       nproc   4096
 52 nginx           -       nofile  4096

###########################
[root@server2 nginx]# vim conf/nginx.conf
116 server {
117         listen 80;
118         server_name www.redhat.org;
119         location / {
120                 root /web1;
121                 index index.html;
122                 }
123         }
:wq
[root@server2 nginx]# nginx -s reload
[root@server2 nginx]# mkdir /web1
[root@server2 nginx]# vim /web1/index.html
 hello world
:wq
[root@foundation76 ~]# vim /etc/hosts #
 172.25.76.2 www.redhat.org (172.25.76.2 nginx server ip)
test :
    input www.redhat.org in firefox

####https代理###
[root@server2 nginx]# vim conf/nginx.conf
96     # HTTPS server
 97     #
 98     server {
 99         listen       443 ssl;
100         server_name  localhost;
101
102         ssl_certificate      cert.pem;
103         ssl_certificate_key  cert.pem;   #修改
104
105         ssl_session_cache    shared:SSL:1m;
106         ssl_session_timeout  5m;
107
108         ssl_ciphers  HIGH:!aNULL:!MD5;
109         ssl_prefer_server_ciphers  on;
111         location / {
112             root   html;
113             index  index.html index.htm;
114         }
115     }

[root@server2 nginx]# cd /etc/pki/tls/certs/
[root@server2 certs]# ls
ca-bundle.crt  ca-bundle.trust.crt  make-dummy-cert  Makefile  renew-dummy-cert
[root@server2 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]:westos
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:server2
Email Address []:qq.com
[root@server2 certs]# mv cert.pem /usr/local/lamp/nginx/conf/
[root@server2 nginx]# nginx -s reload
test:
    input https://172.25.76.2 in firefox

#####重写与负载均衡###
[root@server2 nginx]# vim conf/nginx.conf
17 http {
 18         upstream westos{
 19                 server 172.25.76.1:80;    ##打开172.25.76.1虚拟机安装apache并在/var/www/html/index.html中写上server1,172.25.76.3主机同理写server3 ,方便测试
 20                 server 172.25.76.3:80;
            #server 172.25.76.2:8080 backup; 当2台server挂掉时启用
21         }   
 22     include       mime.types;
120 server {
121         listen 80;
122         server_name www.redhat.org;
123         rewrite ^(.*) http://www.linux.org$1 permanent;
124 }
125 server {
126         listen 80;
127         server_name www.linux.org;
128         location / {
129                 proxy_pass http://westos;
130 }
131 }
132 }
[root@server2 nginx]# nginx -s reload
[root@foundation76 ~]# vim /etc/hosts #客户端(真机)
172.25.76.2 www.redhat.org www.linux.org

test:
[root@foundation76 ~]# for i in {1..10}; do curl www.linux.org;done
server1
server1
server3
server3
server1
server1
server3
server3
server1
server1

原创粉丝点击