安裝Tomcat
移除apt所安裝的tomcat9
Tomcat 10的版本比Tomcat 9更快更穩定,所以先前若有使用 apt-get 安裝過Tomcat 9的人,請依如下步驟移除。
sudo apt-get purge tomcat9 sudo apt autoremove sudo rm -rf /etc/tomcat9
下載及安裝
請到 https://tomcat.apache.org/download-10.cgi 下載Core版 .tar.gz檔,然後依如下步驟安裝。
sudo apt-get install openjdk-18-jdk sudo useradd -m -d /opt/tomcat -U -s /bin/false tomcat wget https://downloads.apache.org/tomcat/tomcat-10/v10.0.22/bin/apache-tomcat-10.0.22.tar.gz sudo mkdir /opt/tomcat sudo tar xzvf apache-tomcat-10.0.22.tar.gz -C /opt/tomcat --strip-components=1 sudo chown -R tomcat:tomcat /opt/tomcat sudo chmod -R u+x /opt/tomcat/bin
設定啟動服務
sudo vim /etc/systemd/system/tomcat.service,並寫入如下指令
[Unit] Description=Tomcat After=network.target [Service] Type=forking User=tomcat Group=tomcat Environment="JAVA_HOME=/usr/lib/jvm/java-1.18.0-openjdk-amd64" Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom" Environment="CATALINA_BASE=/opt/tomcat" Environment="CATALINA_HOME=/opt/tomcat" Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid" Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC" ExecStart=/opt/tomcat/bin/startup.sh ExecStop=/opt/tomcat/bin/shutdown.sh [Install] WantedBy=multi-user.target
啟用服數及啟動Tomcat
sudo systemctl daemon-reload sudo systemctl enable tomcat.service sudo systemctl start tomcat.service sudo systemctl status tomcat.service
更改網頁根目錄
sudo vim /opt/tomcat/conf/server.xml,新增如下藍色部份
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" />
<Context path="" docBase="/data/server/tomcat" debug="0" reloadable="true"/>
</Host>
Nginx設定
開啟 /etc/nginx/sites-available/default,新增如下藍色的部份即可,將所有 *.jsp的檔案全都轉向給 Tomcat http://localhost:8080
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
#location ~ .*.jsp$ {
location ~* \.jsp$ {
index index.jsp;
proxy_pass http://localhost:8080;
}