一脸懵逼学习Nginx及其安装,Tomcat的安装
1:Nginx的相关概念知识:
1.1:反向代理:
反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。
1.2:负载均衡:
负载均衡,英文名称为Load Balance,是指建立在现有网络结构之上,并提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。其原理就是数据流量分摊到多个服务器上执行,减轻每台服务器的压力,多台服务器共同完成工作任务,从而提高了数据的吞吐量。
2:Nginx的安装操作:
Nginx的官网:http://nginx.org/
2.1:将下载好的Nginx上传到虚拟机上面,然后进行解压缩操作,上传过程省略,请自行脑补:
1 [root@master package]# tar -zxvf nginx-1.8.1.tar.gz -C /home/hadoop/
2.2:编译Nginx源码目录:
进入Nginx源码目录:[root@master hadoop]# cd /home/hadoop/nginx-1.8.1/
检查安装环境,并指定将来要安装的路径:
1 #缺包报错 2 3 checking for OS 4 + Linux 2.6.32-696.10.1.el6.i686 i686 5 checking for C compiler ... found 6 + using GNU C compiler 7 + gcc version: 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) 8 checking for gcc -pipe switch ... found 9 checking for gcc builtin atomic operations ... found 10 checking for C99 variadic macros ... found 11 checking for gcc variadic macros ... found 12 checking for unistd.h ... found 13 checking for inttypes.h ... found 14 checking for limits.h ... found 15 checking for sys/filio.h ... not found 16 checking for sys/param.h ... found 17 checking for sys/mount.h ... found 18 checking for sys/statvfs.h ... found 19 checking for crypt.h ... found 20 checking for Linux specific features 21 checking for epoll ... found 22 checking for EPOLLRDHUP ... found 23 checking for O_PATH ... not found 24 checking for sendfile() ... found 25 checking for sendfile64() ... found 26 checking for sys/prctl.h ... found 27 checking for prctl(PR_SET_DUMPABLE) ... found 28 checking for sched_setaffinity() ... found 29 checking for crypt_r() ... found 30 checking for sys/vfs.h ... found 31 checking for nobody group ... found 32 checking for poll() ... found 33 checking for /dev/poll ... not found 34 checking for kqueue ... not found 35 checking for crypt() ... not found 36 checking for crypt() in libcrypt ... found 37 checking for F_READAHEAD ... not found 38 checking for posix_fadvise() ... found 39 checking for O_DIRECT ... found 40 checking for F_NOCACHE ... not found 41 checking for directio() ... not found 42 checking for statfs() ... found 43 checking for statvfs() ... found 44 checking for dlopen() ... not found 45 checking for dlopen() in libdl ... found 46 checking for sched_yield() ... found 47 checking for SO_SETFIB ... not found 48 checking for SO_ACCEPTFILTER ... not found 49 checking for TCP_DEFER_ACCEPT ... found 50 checking for TCP_KEEPIDLE ... found 51 checking for TCP_FASTOPEN ... not found 52 checking for TCP_INFO ... found 53 checking for accept4() ... found 54 checking for eventfd() ... found 55 checking for int size ... 4 bytes 56 checking for long size ... 4 bytes 57 checking for long long size ... 8 bytes 58 checking for void * size ... 4 bytes 59 checking for uint64_t ... found 60 checking for sig_atomic_t ... found 61 checking for sig_atomic_t size ... 4 bytes 62 checking for socklen_t ... found 63 checking for in_addr_t ... found 64 checking for in_port_t ... found 65 checking for rlim_t ... found 66 checking for uintptr_t ... uintptr_t found 67 checking for system byte ordering ... little endian 68 checking for size_t size ... 4 bytes 69 checking for off_t size ... 8 bytes 70 checking for time_t size ... 4 bytes 71 checking for setproctitle() ... not found 72 checking for pread() ... found 73 checking for pwrite() ... found 74 checking for sys_nerr ... found 75 checking for localtime_r() ... found 76 checking for posix_memalign() ... found 77 checking for memalign() ... found 78 checking for mmap(MAP_ANON|MAP_SHARED) ... found 79 checking for mmap("/dev/zero", MAP_SHARED) ... found 80 checking for System V shared memory ... found 81 checking for POSIX semaphores ... not found 82 checking for POSIX semaphores in libpthread ... found 83 checking for struct msghdr.msg_control ... found 84 checking for ioctl(FIONBIO) ... found 85 checking for struct tm.tm_gmtoff ... found 86 checking for struct dirent.d_namlen ... not found 87 checking for struct dirent.d_type ... found 88 checking for sysconf(_SC_NPROCESSORS_ONLN) ... found 89 checking for openat(), fstatat() ... found 90 checking for getaddrinfo() ... found 91 checking for PCRE library ... not found 92 checking for PCRE library in /usr/local/ ... not found 93 checking for PCRE library in /usr/include/pcre/ ... not found 94 checking for PCRE library in /usr/pkg/ ... not found 95 checking for PCRE library in /opt/local/ ... not found 96 97 ./configure: error: the HTTP rewrite module requires the PCRE library. 98 You can either disable the module by using --without-http_rewrite_module 99 option, or install the PCRE library into the system, or build the PCRE library 100 statically from the source with nginx by using --with-pcre=<path> option.
然后安装一下缺少的包:
1 [root@master nginx-1.8.1]# yum -y install gcc pcre-devel openssl openssl-devel
解决完错误以后再次执行,检查安装环境,并指定将来要安装的路径:
[root@master nginx-1.8.1]# ./configure --prefix=/home/hadoop/nginx
1 [root@master nginx-1.8.1]# ./configure --prefix=/home/hadoop/nginx 2 checking for OS 3 + Linux 2.6.32-696.10.1.el6.i686 i686 4 checking for C compiler ... found 5 + using GNU C compiler 6 + gcc version: 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) 7 checking for gcc -pipe switch ... found 8 checking for gcc builtin atomic operations ... found 9 checking for C99 variadic macros ... found 10 checking for gcc variadic macros ... found 11 checking for unistd.h ... found 12 checking for inttypes.h ... found 13 checking for limits.h ... found 14 checking for sys/filio.h ... not found 15 checking for sys/param.h ... found 16 checking for sys/mount.h ... found 17 checking for sys/statvfs.h ... found 18 checking for crypt.h ... found 19 checking for Linux specific features 20 checking for epoll ... found 21 checking for EPOLLRDHUP ... found 22 checking for O_PATH ... not found 23 checking for sendfile() ... found 24 checking for sendfile64() ... found 25 checking for sys/prctl.h ... found 26 checking for prctl(PR_SET_DUMPABLE) ... found 27 checking for sched_setaffinity() ... found 28 checking for crypt_r() ... found 29 checking for sys/vfs.h ... found 30 checking for nobody group ... found 31 checking for poll() ... found 32 checking for /dev/poll ... not found 33 checking for kqueue ... not found 34 checking for crypt() ... not found 35 checking for crypt() in libcrypt ... found 36 checking for F_READAHEAD ... not found 37 checking for posix_fadvise() ... found 38 checking for O_DIRECT ... found 39 checking for F_NOCACHE ... not found 40 checking for directio() ... not found 41 checking for statfs() ... found 42 checking for statvfs() ... found 43 checking for dlopen() ... not found 44 checking for dlopen() in libdl ... found 45 checking for sched_yield() ... found 46 checking for SO_SETFIB ... not found 47 checking for SO_ACCEPTFILTER ... not found 48 checking for TCP_DEFER_ACCEPT ... found 49 checking for TCP_KEEPIDLE ... found 50 checking for TCP_FASTOPEN ... not found 51 checking for TCP_INFO ... found 52 checking for accept4() ... found 53 checking for eventfd() ... found 54 checking for int size ... 4 bytes 55 checking for long size ... 4 bytes 56 checking for long long size ... 8 bytes 57 checking for void * size ... 4 bytes 58 checking for uint64_t ... found 59 checking for sig_atomic_t ... found 60 checking for sig_atomic_t size ... 4 bytes 61 checking for socklen_t ... found 62 checking for in_addr_t ... found 63 checking for in_port_t ... found 64 checking for rlim_t ... found 65 checking for uintptr_t ... uintptr_t found 66 checking for system byte ordering ... little endian 67 checking for size_t size ... 4 bytes 68 checking for off_t size ... 8 bytes 69 checking for time_t size ... 4 bytes 70 checking for setproctitle() ... not found 71 checking for pread() ... found 72 checking for pwrite() ... found 73 checking for sys_nerr ... found 74 checking for localtime_r() ... found 75 checking for posix_memalign() ... found 76 checking for memalign() ... found 77 checking for mmap(MAP_ANON|MAP_SHARED) ... found 78 checking for mmap("/dev/zero", MAP_SHARED) ... found 79 checking for System V shared memory ... found 80 checking for POSIX semaphores ... not found 81 checking for POSIX semaphores in libpthread ... found 82 checking for struct msghdr.msg_control ... found 83 checking for ioctl(FIONBIO) ... found 84 checking for struct tm.tm_gmtoff ... found 85 checking for struct dirent.d_namlen ... not found 86 checking for struct dirent.d_type ... found 87 checking for sysconf(_SC_NPROCESSORS_ONLN) ... found 88 checking for openat(), fstatat() ... found 89 checking for getaddrinfo() ... found 90 checking for PCRE library ... found 91 checking for PCRE JIT support ... not found 92 checking for md5 in system md library ... not found 93 checking for md5 in system md5 library ... not found 94 checking for md5 in system OpenSSL crypto library ... found 95 checking for sha1 in system md library ... not found 96 checking for sha1 in system OpenSSL crypto library ... found 97 checking for zlib library ... found 98 creating objs/Makefile 99 100 Configuration summary 101 + using system PCRE library 102 + OpenSSL library is not used 103 + md5: using system crypto library 104 + sha1: using system crypto library 105 + using system zlib library 106 107 nginx path prefix: "/home/hadoop/nginx" 108 nginx binary file: "/home/hadoop/nginx/sbin/nginx" 109 nginx configuration prefix: "/home/hadoop/nginx/conf" 110 nginx configuration file: "/home/hadoop/nginx/conf/nginx.conf" 111 nginx pid file: "/home/hadoop/nginx/logs/nginx.pid" 112 nginx error log file: "/home/hadoop/nginx/logs/error.log" 113 nginx http access log file: "/home/hadoop/nginx/logs/access.log" 114 nginx http client request body temporary files: "client_body_temp" 115 nginx http proxy temporary files: "proxy_temp" 116 nginx http fastcgi temporary files: "fastcgi_temp" 117 nginx http uwsgi temporary files: "uwsgi_temp" 118 nginx http scgi temporary files: "scgi_temp"
2.3:编译安装(make是编译,make install是安装):
make && make install安装不是一帆风顺的,开始将make && make install写成了make && made install,肯定没有安装成功了,然后我再执行make && make install就出现下面的情况了,然后我重新./configure --prefix=/usr/local/nginx检查安装环境,并指定将来要安装的路径,最后再make && made install,貌似正常编译,安装了,虽然我也不是很清楚,这里贴一下吧先,安装好以后可以测试是否正常:
1 [root@master hadoop]# make && make install 2 make: *** No targets specified and no makefile found. Stop. 3 [root@master hadoop]# make install 4 make: *** No rule to make target `install'. Stop. 5 [root@master hadoop]# make && make install 6 make: *** No targets specified and no makefile found. Stop. 7 [root@master hadoop]# ./configure --prefix=/home/hadoop/nginx 8 bash: ./configure: No such file or directory 9 [root@master hadoop]# cd /home/hadoop/nginx-1.8.1/ 10 [root@master nginx-1.8.1]# ls 11 auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src 12 [root@master nginx-1.8.1]# ./configure --prefix=/home/hadoop/nginx
2.4:安装好以后测试是否正常:
安装好以后指定的目录会生成一些文件,如我的/home/hadoop/nginx目录下面:
启动Nginx的命令:[root@master sbin]# ./nginx
查看端口是否有Nginx进程监听:[root@master sbin]# netstat -ntlp | grep 80
3:配置Nginx:
3.1:配置反向代理:
修改Nginx配置文件:
[root@master conf]# cd /home/hadoop/nginx/conf/
[root@master conf]# vim nginx.conf
server { listen 80; server_name master; #nginx所在服务器的主机名称 #charset koi8-r; #access_log logs/host.access.log main; #反向代理的配置 location / { #拦截所有请求 root html; #index index.html index.htm; #这里是代理走向的目标服务器:tomcat proxy_pass http://192.168.199.130:8080; }
具体配置如下所示:
下面贴图这句话后面proxy_pass http://192.168.199.130:8080;
少了一个分号导致后面启动nginx的时候出现错误:
自己都操点心就可以了:
1 [root@master sbin]# ./nginx 2 nginx: [emerg] unexpected "}" in /home/hadoop/nginx/conf/nginx.conf:48
4:安装Tomcat,将下载好的tomcat安装包上传到虚拟机,过程省略,然后解压缩操作:
[root@slaver1 package]# tar -zxvf apache-tomcat-7.0.68.tar.gz -C /home/hadoop/
解压缩好以后启动Tomcat:
然后没启动起来,貌似说我的jdk没有配置啥的,现在配置一下,配置过程省略,大概如上传压缩包,解压缩,然后配置环境变量:
vim /etc/profile配置好以后使其立即生效:source /etc/profile,最后检查一下是否安装成功:java/javac/java -version
然后启动tomcat,如下所示:
启动好,可以检查一下是否启动成功:
浏览器输入自己的http://192.168.199.131:8080/
如果无法访问,可能是防火墙的原因:service iptables stop关闭防火墙;service iptables status查看防火墙是否关闭成功;
5:现在体现Nginx的功能了,我在master节点安装的Nginx,然后在slaver1节点安装的tomcat:
然后访问master节点,会跳转到slaver1的tomcat页面:
http://192.168.199.130/自己的master节点的名称;
6:Nginx的动静分离:
1 动态资源 index.jsp 2 location ~ .*\.(jsp|do|action)$ { 3 proxy_pass http://ip地址:8080; 4 } 5 6 7 8 静态资源: 9 location ~ .*\.(html|js|css|gif|jpg|jpeg|png)$ { 10 expires 3d; 11 } 12 13 负载均衡: 14 在http这个节下面配置一个叫upstream的,后面的名字可以随意取,但是要和location下的proxy_pass http://后的保持一致。 15 http { 16 是在http里面的, 已有http, 不是在server里,在server外面 17 upstream tomcats { 18 server 192.168.199.130:8080 weight=1;#weight表示多少个 19 server 192.168.199.131:8080 weight=1; 20 server 192.168.199.132:8080 weight=1; 21 } 22 23 #卸载server里 24 #~代表是大小写敏感,.代表是任何非回车字符,*代表多个。 25 location ~ .*\.(jsp|do|action) { 26 proxy_pass http://tomcats; #tomcats是后面的tomcat服务器组的逻辑组号 27 } 28 29 }