服务器搭建2
程序员文章站
2022-05-02 09:43:07
...
1.将nginx安装包拷贝到/opt/nginx目录下,修改权限,解压缩,同时编译安装.
root@iZ25n0utdnmZ:/opt/nginx# chmod 777 nginx-1.9.3.tar.gz
root@iZ25n0utdnmZ:/opt/nginx# ls -l
total 848
-rwxrwxrwx 1 root root 864430 Aug 318:37 nginx-1.9.3.tar.gz
root@iZ25n0utdnmZ:/opt/nginx# tar -zxvf nginx-1.9.3.tar.gz
root@iZ25n0utdnmZ:/opt/nginx/nginx-1.9.3# ./configure
......(很多输出)......
checking for PCRE libraryin /usr/include/pcre/ ... not found
checking for PCRE libraryin /usr/pkg/ ... not found
checking for PCRE libraryin /opt/local/ ... not found
./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= option.
提示缺少pcre,下载pcre并拷贝到/opt/pcre目录下,修改权限,解压缩,同时编译安装,
root@iZ25n0utdnmZ:/opt/pcre# chmod 777 pcre-8.37.tar.gz
root@iZ25n0utdnmZ:/opt/pcre# ls -l
total 1996
-rwxrwxrwx 1 root root 2041593 Aug 518:31 pcre-8.37.tar.gz
root@iZ25n0utdnmZ:/opt/pcre# tar -zxvf pcre-8.37.tar.gz
root@iZ25n0utdnmZ:/opt/pcre/pcre-8.37# ./configure
root@iZ25n0utdnmZ:/opt/pcre/pcre-8.37# make
root@iZ25n0utdnmZ:/opt/pcre/pcre-8.37# make install
安装完pcre后,继续安装nginx.
root@iZ25n0utdnmZ:/opt/nginx/nginx-1.9.3# ./configure
......(很多输出)......
checking for sha1 in system md library... not found
checking for sha1 in system OpenSSL crypto library... not found
checking for zlib library... not found
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib= option.
提示缺少zlib,下载zlib并拷贝到/zlib/zlib目录下,修改权限,解压缩,同时编译安装,
root@iZ25n0utdnmZ:/opt/zlib# chmod 777 zlib-1.2.8.tar.gz
root@iZ25n0utdnmZ:/opt/zlib# ls -l
total 560
-rwxrwxrwx 1 root root 571091 Aug 518:39 zlib-1.2.8.tar.gz
root@iZ25n0utdnmZ:/opt/zlib# tar -zxvf zlib-1.2.8.tar.gz
root@iZ25n0utdnmZ:/opt/zlib/zlib-1.2.8# ./configure
root@iZ25n0utdnmZ:/opt/zlib/zlib-1.2.8# make
root@iZ25n0utdnmZ:/opt/zlib/zlib-1.2.8# make install
安装完zlib后,继续安装nginx.
root@iZ25n0utdnmZ:/opt/nginx/nginx-1.9.3# ./configure
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using builtin md5 code
+ sha1 library is not found
+ using system zlib library nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
root@iZ25n0utdnmZ:/opt/nginx/nginx-1.9.3# make
root@iZ25n0utdnmZ:/opt/nginx/nginx-1.9.3# make install
注意:ginx默认是在安在/usr/local/nginx下.
2.启动,重启和停止nginx.
root@iZ25n0utdnmZ:/usr/local/nginx/sbin# ./nginx
root@iZ25n0utdnmZ:/usr/local/nginx/sbin# ./nginx -s reload
root@iZ25n0utdnmZ:/usr/local/nginx/sbin# ./nginx -s stop3.nginx和tomcat集成配置.
打开nginx的配置文件nginx.conf,在其中加入如下配置:参考配置1.conf.
location ~ \.mvc {
proxy_pass http://101.201.89.5:8080;
}
其中重要的配置如下:
server {
listen 80;
server_name localhost;
location ~ \.mvc {
proxy_pass http://101.201.89.5:8080;
}
}
这段配置的意思是:服务器监听80端口,当有以.mvc结尾的请求时,服务器将请求交给代理处理.其余配置先不做详细介绍.
注意可以使用nginx -t指令校验配置文件配置的是否正确.
root@iZ25n0utdnmZ:/usr/local/nginx/sbin# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
4.nginx和tomcat负载均衡配置.
打开nginx的配置文件nginx.conf,在其中加入如下配置:参考配置2.conf.
upstream myserver {
server 101.201.89.5:8080 weight=1;
server 101.201.89.5:8081 weight=1;
}
server {
listen 80;
server_name localhost;
location ~ \.mvc {
proxy_pass http://myserver;
}
}
这段配置的意思是:服务器监听80端口,当有以.mvc结尾的请求时,服务器将请求交给代理处理,而代理指向的是一个服务器列表,通过upstream配置,
通过名字myserver匹配,在服务器列表中有两个服务器,端口分别是8080,8081,权重都是1,即nginx会将发过来的请求按1:1的比例交给两个服务器处理,
注意:如果某个服务器瘫痪了或者根本不存在,也不会影响业务,另一个服务器会处理所有请求.
另一个tomcat的server.xml文件配置需要修改的地方如下:
将8005改为8006:"8006"