yum install -y gcc gcc-c++
yum install -y zlib zlib-devel
yum install -y pcre pcre-devel
yum install -y openssl openssl-devel
openssl : [[email protected]] tar zxvf openssl-fips-2.0.9.tar.gz [[email protected]] cd openssl-fips-2.0.9 [[email protected]] ./config && make && make install pcre: [[email protected]] tar zxvf pcre-8.36.tar.gz [[email protected]] cd pcre-8.36 [[email protected]] ./configure && make && make install zlib: [[email protected]] tar zxvf zlib-1.2.8.tar.gz [[email protected]] cd zlib-1.2.8 [[email protected]] ./configure && make && make install
[[email protected]] tar -zvxf nginx-x.x.x.tar.g [[email protected]] cd nginx-x.x.x [[email protected]] ./configure && make && make install
[[email protected]] whereis nginx
[[email protected]] cd /usr/local/nginx/sbin [[email protected]] ./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
[[email protected]] cd /etc/init.d [[email protected]] vim nginx
Nginx腳本文件(官方提供腳本https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/):
#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: NGINX is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` if [ -n "$user" ]; then if [ -z "`grep $user /etc/passwd`" ]; then useradd -M -s /bin/nologin $user fi options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done fi } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
[[email protected]] chmod 755 nginx
[[email protected]] chkconfig --add /etc/init.d/nginx
vim /etc/rc.local 增加一行 /usr/local/nginx/sbin/nginx 設置執行權限: chmod 755 rc.local
準備三臺linux(一臺用作Nginx負載均衡服務器,另外兩臺配置app真實服務器)
[[email protected] html]# cat /usr/local/nginx/conf/nginx.conf #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; upstream serverCluster{ server 192.168.182.131:8080 weight=1; server 192.168.182.133:8080 weight=1; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} server{ listen 80; server_name www.test.com; index index.jsp index.html index.htm; root /data/www; location /{ proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://serverCluster; expires 3d; } } # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # 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; # } #} }
tar -zxvf jdk-8u151-linux-i586.tar.gz //解壓 mv jdk1.8.1_151 /opt //移動文件 mv jdk1.8.1_151 jdk //更名
vim /etc/profile
export JAVA_HOME=/opt/jdk //這裏是你的jdk的安裝目錄 export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
source /etc/profile // 這條命令是讓配置馬上生效。
java –version //顯示java版本信息表示JDK安裝完成
tar -zvxf apache-tomcat-8.5.24.tar.gz //解壓文件 mv apache-tomcat-8.5.24 /opt //轉移文件 mv apache-tomcat-8.5.24 tomcat //文件更名
service iptables stop
chkconfig iptables off
(真實服務器IP1:192.168.182.131;真實服務器IP2:192.168.182.132;nginx服務器IP:192.168.182.130)
因爲兩臺tomcat服務器測試跳轉的頁面都是Tom貓,爲了顯示負載均衡效果,需要更改tomcat服務器的顯示頁面;
顯示頁面位於/opt/tomcat/webapps/ROOT路徑的index.jsp頁面,故只需在此路徑下編輯一個index.html頁面,就會取代原先的index.jsp,因爲/opt/tomcat/conf/web.xml中的歡迎頁面的順序是index.html>index.jsp