CentOS6-7安裝nginx和Tomcat8

CentOS7看這里 https://www.howtoforge.com/tutorial/how-to-install-tomcat-on-centos/

安裝nginx

第一步 - 安裝EPEL
EPEL代表企業(yè)Linux的額外包。因為yum作為軟件包管理器不在其默認存儲庫中包括nginx的最新版本,安裝EPEL將確保CentOS上的nginx保持最新泳桦。

要安裝EPEL盗迟,請打開終端并輸入:

sudo yum install epel-release

第二步 - 安裝nginx

要安裝nginx,打開終端并輸入:

sudo yum install nginx

這樣就安裝好了惭蹂。用下面這些命令啟動(停止|查看狀態(tài))Nginx
安裝路徑在/etc/nginx

啟動
service nginx start
停止
service nginx stop
查看狀態(tài)
service nginx status

啟動后在瀏覽器輸入
http://server_ip 就能看到nginx 的歡迎頁面了

Nginx

把nginx設(shè)成自動重啟

chkconfig nginx on

打開防火墻
firewall-cmd --zone=public --add-port=80/tcp --permanent 
重啟防火墻
systemctl restart  firewalld.service

安裝Tomcat 8

先安裝JDK

去官網(wǎng)找到最新的JDK下載地址伞插,把下面對應(yīng)的地址替換掉。

wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u161-b12/2f38c3b165be4555a1fa6e98c45e0808/jdk-8u161-linux-x64.rpm"

rpm -ivh jdk-8u161-linux-x64.rpm

裝好了盾碗,驗證一下

java -version

應(yīng)該能看到如下信息

java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

安裝Tomcat 8

First

create a new tomcat group:

sudo groupadd tomcat

Next

create a new tomcat user. We'll make this user a member of the tomcat group, with a home directory of /opt/tomcat (where we will install Tomcat), and with a shell of /bin/false (so nobody can log into the account):

sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

Now that our tomcat user is set up, let's download and install Tomcat.

Step 3:

The best way to install Tomcat 8 is to download the latest binary release then configure it manually.

Find the latest version of Tomcat 8 at the Tomcat 8 Downloads page. At the time of writing, the latest version is 8.5.5, but you should use a later stable version if it is available. Under the Binary Distributionssection, then under the Core list, copy the link to the "tar.gz".

Next, change to the /tmp directory on your server. This is a good directory to download ephemeral items, like the Tomcat tarball, which we won't need after extracting the Tomcat contents:

cd /tmp

Use curl to download the link that you copied from the Tomcat website:

curl -O http://apache.mirrors.ionfish.org/tomcat/tomcat-8/v8.5.5/bin/apache-tomcat-8.5.5.tar.gz

用wget也行媚污,現(xiàn)在最新是8.5.28
wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.28/bin/apache-tomcat-8.5.28.tar.gz

We will install Tomcat to the /opt/tomcat directory. Create the directory, then extract the archive to it with these commands:

sudo mkdir /opt/tomcat
sudo tar xzvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1

Next, we can set up the proper user permissions for our installation.

Step 4: Update Permissions

The tomcat user that we set up needs to have access to the Tomcat installation. We'll set that up now.

Change to the directory where we unpacked the Tomcat installation:

cd /opt/tomcat

Give the tomcat group ownership over the entire installation directory:

sudo chgrp -R tomcat /opt/tomcat

Next, give the tomcat group read access to the conf directory and all of its contents, and execute access to the directory itself:

sudo chmod -R g+r conf
sudo chmod g+x conf

Make the tomcat user the owner of the webapps, work, temp, and logs directories:

sudo chown -R tomcat webapps/ work/ temp/ logs/

Now that the proper permissions are set up, we can create a systemd service file to manage the Tomcat process.

Step 5: Create a systemd Service File

We want to be able to run Tomcat as a service, so we will set up systemd service file.

Tomcat needs to know where Java is installed. This path is commonly referred to as "JAVA_HOME". The easiest way to look up that location is by running this command:

Your JAVA_HOME may be different.

With this piece of information, we can create the systemd service file. Open a file called tomcat in the /etc/init.d/ directory by typing:

sudo nano /etc/init.d/tomcat

Paste the following contents into your service file. Modify the value of JAVA_HOME if necessary to match the value you found on your system. You may also want to modify the memory allocation settings that are specified in CATALINA_OPTS:

注意 腳本中的JAVA_HOMETOMCAT_HOME廷雅,TOMCAT_USER,CATALINA_OPTS根據(jù)自己電腦自行修改耗美,XX:MaxPermSize 參數(shù)根據(jù)內(nèi)存大小自己調(diào)整。

#!/bin/bash
#
# tomcat
#
# chkconfig: - 80 20
#
### BEGIN INIT INFO
# Provides: tomcat8
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Description: Tomcat 8
# Short-Description: start and stop tomcat
### END INIT INFO

## Source function library.
#. /etc/rc.d/init.d/functions
export JAVA_HOME=/usr/java/jdk1.8.0_161
export JAVA_OPTS="-Dfile.encoding=UTF-8 \
  -Dnet.sf.ehcache.skipUpdateCheck=true \
  -XX:+UseConcMarkSweepGC \
  -XX:+CMSClassUnloadingEnabled \
  -XX:+UseParNewGC \
  -XX:MaxPermSize=128m \
  -Xms256m -Xmx256m"
export PATH=$JAVA_HOME/bin:$PATH
TOMCAT_HOME=/opt/tomcat
TOMCAT_USER=tomcat
SHUTDOWN_WAIT=20

tomcat_pid() {
  echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'`
}

start() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ] 
  then
    echo "Tomcat is already running (pid: $pid)"
  else
    # Start tomcat
    echo "Starting tomcat"
    ulimit -n 100000
    umask 007
    /bin/su -p -s /bin/sh $TOMCAT_USER $TOMCAT_HOME/bin/startup.sh
  fi


  return 0
}

stop() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo "Stoping Tomcat"
    /bin/su -p -s /bin/sh $TOMCAT_USER $TOMCAT_HOME/bin/shutdown.sh

    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
      echo -n -e "\nwaiting for processes to exit";
      sleep 1
      let count=$count+1;
    done

    if [ $count -gt $kwait ]; then
      echo -n -e "\nkilling processes which didn't stop after $SHUTDOWN_WAIT seconds"
      kill -9 $pid
    fi
  else
    echo "Tomcat is not running"
  fi
 
  return 0
}

case $1 in
start)
  start
;; 
stop)   
  stop
;; 
restart)
  stop
  start
;;
status)
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo "Tomcat is running with pid: $pid"
  else
    echo "Tomcat is not running"
  fi
;; 
esac    
exit 0

When you are finished, save and close the file.

Next, make the script executable using chmod:

chmod +x /etc/init.d/tomcat

Start the Tomcat service by typing:

service tomcat start

Double check that it started without errors by typing:

service tomcat status

image.png

Add the Tomcat service to system startup:

chkconfig tomcat on

Open in web browser http://server_domain_or_IP:8080

image.png

Configure Tomcat Web Management Interface

In order to use the manager web app that comes with Tomcat, we must add a login to our Tomcat server. We will do this by editing the tomcat-users.xml file:

sudo nano /opt/tomcat/conf/tomcat-users.xml

You will want to add a user who can access the manager-gui and admin-gui (web apps that come with Tomcat). You can do so by defining a user, similar to the example below, between the tomcat-users tags. Be sure to change the username and password to something secure:

tomcat-users.xml — Admin User

<tomcat-users . . .>
    <user username="admin" password="password" roles="manager-gui,admin-gui"/>
</tomcat-users>

Save and close the file when you are finished.

By default, newer versions of Tomcat restrict access to the Manager and Host Manager apps to connections coming from the server itself. Since we are installing on a remote machine, you will probably want to remove or alter this restriction. To change the IP address restrictions on these, open the appropriate context.xml files.

For the Manager app, type:

sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml

For the Host Manager app, type:

sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml

Inside, comment out the IP address restriction to allow connections from anywhere. Alternatively, if you would like to allow access only to connections coming from your own IP address, you can add your public IP address to the list:

context.xml files for Tomcat webapps

<Context antiResourceLocking="false" privileged="true" >
  <!--<Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />-->
</Context>

Save and close the files when you are finished.

To put our changes into effect, restart the Tomcat service:

sudo systemctl restart tomcat

用nginx反向代理tomcat

mkdir /etc/nginx/sites-enabled

新版的nginx中一般都有這個目錄航缀,沒有的自己建一個
目錄下建一個名叫tomcat的文件商架, 如有其它文件請刪掉.

 nano /etc/nginx/sites-enabled/tomcat

寫入一下代碼

upstream tomcat8080  {
        ip_hash;
        server 127.0.0.1:8080;
   } 

# Default server configuration

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        ## try_files $uri $uri/ =404;
        proxy_pass  http://tomcat8080;
    }
}

保存重啟nginx

service nginx restart

輸入 瀏覽器訪問 http://YOU_IP
如果502了(google vm會),看一下error_log芥玉, 解決辦法:
https://stackoverflow.com/questions/23948527/13-permission-denied-while-connecting-to-upstreamnginx

setsebool -P httpd_can_network_connect 1

OK了蛇摸,變成tomcat的歡迎頁了。

附:
nginx服務(wù)器安裝及配置文件詳解

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末灿巧,一起剝皮案震驚了整個濱河市赶袄,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌抠藕,老刑警劉巖饿肺,帶你破解...
    沈念sama閱讀 211,948評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異幢痘,居然都是意外死亡唬格,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,371評論 3 385
  • 文/潘曉璐 我一進店門颜说,熙熙樓的掌柜王于貴愁眉苦臉地迎上來购岗,“玉大人,你說我怎么就攤上這事门粪『盎” “怎么了?”我有些...
    開封第一講書人閱讀 157,490評論 0 348
  • 文/不壞的土叔 我叫張陵玄妈,是天一觀的道長乾吻。 經(jīng)常有香客問我髓梅,道長,這世上最難降的妖魔是什么绎签? 我笑而不...
    開封第一講書人閱讀 56,521評論 1 284
  • 正文 為了忘掉前任枯饿,我火速辦了婚禮,結(jié)果婚禮上诡必,老公的妹妹穿的比我還像新娘奢方。我一直安慰自己,他們只是感情好爸舒,可當我...
    茶點故事閱讀 65,627評論 6 386
  • 文/花漫 我一把揭開白布蟋字。 她就那樣靜靜地躺著,像睡著了一般扭勉。 火紅的嫁衣襯著肌膚如雪鹊奖。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,842評論 1 290
  • 那天涂炎,我揣著相機與錄音忠聚,去河邊找鬼。 笑死璧尸,一個胖子當著我的面吹牛咒林,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播爷光,決...
    沈念sama閱讀 38,997評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼垫竞,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蛀序?” 一聲冷哼從身側(cè)響起欢瞪,我...
    開封第一講書人閱讀 37,741評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎徐裸,沒想到半個月后遣鼓,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,203評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡重贺,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,534評論 2 327
  • 正文 我和宋清朗相戀三年骑祟,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片气笙。...
    茶點故事閱讀 38,673評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡次企,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出潜圃,到底是詐尸還是另有隱情缸棵,我是刑警寧澤,帶...
    沈念sama閱讀 34,339評論 4 330
  • 正文 年R本政府宣布谭期,位于F島的核電站堵第,受9級特大地震影響吧凉,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜踏志,卻給世界環(huán)境...
    茶點故事閱讀 39,955評論 3 313
  • 文/蒙蒙 一阀捅、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧狰贯,春花似錦也搓、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,770評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽幔摸。三九已至摸柄,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間既忆,已是汗流浹背驱负。 一陣腳步聲響...
    開封第一講書人閱讀 32,000評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留患雇,地道東北人跃脊。 一個月前我還...
    沈念sama閱讀 46,394評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像苛吱,于是被迫代替她去往敵國和親酪术。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,562評論 2 349

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,312評論 0 10
  • 今天翠储,陽光明媚绘雁,鳥語花香。嗯援所!真是個郊游的好氣候庐舟,今天就請你和我們一起去美麗的郊外郊游!張開雙臂住拭,輕呼氣挪略,空氣中散...
    悠悠倩閱讀 193評論 0 0
  • 種植牙杠娱,被譽為“人類的第三幅牙齒”,它比傳統(tǒng)的假牙更加的自然美觀澈蟆,在咀嚼口感和力度上都接近真牙墨辛,但價格不菲,種一顆...
    洗發(fā)水的冬天閱讀 386評論 0 0
  • 先來看下一段文字: 接力:我在廣東趴俘,我支持販賣兒童死刑睹簇,要求上戶口錄入指紋奏赘。一個孩子的丟失、骨肉分離太惠、家庭破碎磨淌、一...
    安梓閱讀 155評論 0 1