安装JDK
下载的jdk文件为:jdk-6u45-linux-x64.bin,执行如下命令进行安装:
#./jdk-6u12-linux-i586.bin
安装tomcat:
#tar zxvf apache-tomcat-6.0.18.tar.gz #mv apache-tomcat-6.0.29 tomcat
这里我将解压后的apache-tomcat-6.0.29重命名为了tomcat方便操作。
配置环境变量:
编辑/etc下的profile文件,加上如下内容:
JAVA_HOME="/opt/app/jdk1.6.0_45" CLASS_PATH="$JAVA_HOME/lib:$JAVA_HOME/jre/lib" PATH=".:$PATH:$JAVA_HOME/bin" CATALINA_HOME="/opt/app/tomcat" export JAVA_HOME CATALINA_HOME
执行下面命令使变更生效:
# source /etc/profile
启动tomcat并输入http://domain:8080,如果看到猫的页面即tomcat和jdk安装成功
新建文件目录/home/www为网站存放目录,设置server.xml文件,在Host name=”localhost”处将appBase=的指向路径改为/home/www/web
创建index.jsp至/home/www/web/ROOT,内容为:“hello!” 重新启动tomcat,重新访问,如果看到index.jsp文件内容hello!表示设置成功。
二、安装Nginx
执行如下命令解压nginx:
# tar zxvf nginx-1.4.4.tar.gz
# mv nginx-1.4.4 nginx
同样重命名了一下。
安装nginx:
# ./configure --prefix=/opt/app/nginx
结果出现了错误:error: C compiler cc is not found,按网上所说安装编译源码所需的工具和库:
#yum install gcc gcc-c++ ncurses-devel perl
再次安装,发现还有错误:the HTTP rewrite module requires the PCRE library.
执行
# yum -y install pcre-devel openssl openssl-devel
终于成功,
# ./configure --prefix=/opt/app/nginx # make # make install
nginx安装成功后的安装目录为/opt/app/nginx
在conf文件夹中新建proxy.conf,用于配置一些代理参数,内容如下:
#!nginx (-) # proxy.conf proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; #获取真实ip #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #获取代理者的真实ip client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k;
编辑安装目录下conf文件夹中的nginx.conf,输入如下内容
#运行nginx所在的用户名和用户组 #user www www; #启动进程数 worker_processes 8; #全局错误日志及PID文件 error_log /opt/app/nginx/logs/nginx_error.log crit; pid /opt/app/nginx/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 65535; #工作模式及连接数上限 events { use epoll; worker_connections 65535; } #设定http服务器,利用它的反向代理功能提供负载均衡支持 http { #设定mime类型 include mime.types; default_type application/octet-stream; include /opt/app/nginx/conf/proxy.conf; #charset gb2312; #设定请求缓冲 server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; # client_max_body_size 8m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; # fastcgi_connect_timeout 300; # fastcgi_send_timeout 300; # fastcgi_read_timeout 300; # fastcgi_buffer_size 64k; # fastcgi_buffers 4 64k; # fastcgi_busy_buffers_size 128k; # fastcgi_temp_file_write_size 128k; # gzip on; # gzip_min_length 1k; # gzip_buffers 4 16k; # gzip_http_version 1.0; # gzip_comp_level 2; # gzip_types text/plain application/x-javascript text/css application/xml; # gzip_vary on; #limit_zone crawler $binary_remote_addr 10m; ###禁止通过ip访问站点 # server{ # server_name _; # return 404; # } server { listen 80; server_name localhost; index index.html index.jsp; root /opt/www/static; #limit_conn crawler 20; location ~ .*.(jsp|shtml)$ #所有shtml的页面均交由tomcat处理 { index index.jsp; proxy_pass http://localhost:8080;#转向tomcat处理 } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #设定访问静态文件直接读取不经过tomcat { expires 30d; } location ~ .*\.(js|css)?$ { expires 1h; } } #定义访问日志的写入格式 log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; access_log /opt/app/nginx/logs/localhost.log access;#设定访问日志的存放路径 }
修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:
#/opt/app/nginx/sbin/nginx -t
如果出现下面两行,说明正确:
the configuration file /opt/app/nginx/conf/nginx.conf syntax is ok the configuration file /opt/app/nginx/conf/nginx.conf was tested successfully
如果提示unknown host,则可在服务器上执行:ping www.baidu.com如果也是同样提示unknown host则有两种可能:
a、服务器没有设置DNS服务器地址,查看/etc/resolv.conf下是否设置,若无则加上
b、防火墙拦截
启动nginx的命令
#/opt/app/nginx/sbin/nginx
这时,输入以下命令查看Nginx主进程号:
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
停止nginx的命令
#/opt/app/nginx/sbin/nginx -s stop
在不停止Nginx服务的情况下平滑变更Nginx配置
输入以下命令查看Nginx主进程号:
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
屏幕显示的即为Nginx主进程号,例如:
6302
这时,执行以下命令即可使修改过的Nginx配置文件生效:
kill -HUP 6302
或者无需这么麻烦,找到Nginx的Pid文件:
kill -HUP `cat /usr/local/nginx/nginx.pid`