部署LNMT

背景信息

. 操作系統(tǒng): CentOS 7.6
.Tomcat版本:Tomcat 9.0.27
.JDK版本: JDK 1.8.0_231

下載安裝包

1.下載Aapche Tomcat.
登錄https://tomcat.apache.org官方網(wǎng)站進(jìn)行下載攀圈,這里下載使用的是Tomcat 9.0.27峦甩,你可以根據(jù)需求自行下載所需的版本穴店。

[root@web01 ~]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.27/bin/apache-tomcat-9.0.27.tar.gz

2.下載JDK
jDK有開源的openjdk也有orcal提供的jdk,centos源默認(rèn)自帶openjdk,你可以使用yum進(jìn)行安裝拿穴,這里登錄https://www.oracle.com/technetwork/cn/java/javase/downloads進(jìn)行下載默色,這里下載使用的是JDK 1.8.0_231(下載到本地之后再上傳),你可以根據(jù)需求自行下載所需的版本呕诉。

安裝前準(zhǔn)備

1.關(guān)閉防火墻
1.1查看防火墻的狀態(tài)

[root@web01 ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since Fri 2019-10-25 15:53:56 CST; 32s ago
     Docs: man:firewalld(1)
 Main PID: 7564 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─7564 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Oct 25 15:53:56 web01 systemd[1]: Starting firewalld - dynamic firewall daemon...
Oct 25 15:53:56 web01 systemd[1]: Started firewalld - dynamic firewall daemon.

.如果防火墻的狀態(tài)是inactive,則防火墻為關(guān)閉狀態(tài)甩挫。
.如果防火墻的狀態(tài)參數(shù)為active,則防火墻為開啟狀態(tài)本實例為開啟狀態(tài),這里需要關(guān)閉防護(hù)墻英遭。
1.2關(guān)閉防火墻亦渗。如果防火墻為關(guān)閉狀態(tài)可以忽略此步驟法精。
.如果您想臨時關(guān)閉防火墻,運行命令systemctl stop firewalld狼荞。
.如果您想永久關(guān)閉防火墻粘秆,運行命令systemctl disable firewalld收毫。
2.關(guān)閉selinux。
2.1運行命令getenforce查看SELinux的當(dāng)前狀態(tài)

[root@web01 ~]# getenforce
Enforcing

.如果SELinux狀態(tài)參數(shù)是Disabled昔搂, 則SELinux為關(guān)閉狀態(tài)摘符。
.如果SELinux狀態(tài)參數(shù)是Enforcing策吠,則SELinux為開啟狀態(tài)猴抹。本示例中SELinux為開啟狀態(tài),因此需要關(guān)閉SELinux蝙砌。
2.1.1關(guān)閉SELinux。如果SELinux為關(guān)閉狀態(tài)可以忽略此步驟恬总。
.如果您想臨時關(guān)閉SELinux肚邢,運行命令setenforce 0道偷。
.如果您想永久關(guān)閉SELinux,運行命令vi /etc/selinux/config編輯SELinux配置文件并巍“枚桑回車后军拟,把光標(biāo)移動到SELINUX=enforcing這一行懈息,按i鍵進(jìn)入編輯模式,修改為SELINUX=disabled怒见, 按Esc鍵姑宽,然后輸入:wq并回車來保存并關(guān)閉SELinux配置文件炮车。
3.重啟系統(tǒng)使設(shè)置生效。
4.創(chuàng)建一般用戶www來運行Tomcat纪隙。

[root@web01 ~]# useradd www
[root@web01 ~]# id www
uid=1000(www) gid=1000(www) groups=1000(www)

5.創(chuàng)建網(wǎng)站根目錄

[root@web01 ~]# mkdir -p /data/jpress

6.修改網(wǎng)站根目錄下的文件權(quán)限為www绵咱。

[root@web01 ~]# chown -R www.www /data/jpress

安裝JDK

1.創(chuàng)建JDK的安裝目錄

[root@web01 ~]# mkdir /usr/java

2.上傳下載的JDK包依次運行以下命令解壓jdk-8u231-linux-x64.tar.gz到/usr/java

[root@web01 ~]# chmod +x jdk-8u231-linux-x64.tar.gz 
[root@web01 ~]# tar xzf jdk-8u231-linux-x64.tar.gz -C /usr/java

3.設(shè)置環(huán)境變量

[root@web01 /usr/java]# echo -e "# set java environment\nexport JAVA_HOME=/usr/java/jdk1.8.0_231\nexport CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib\nexport PATH=$JAVA_HOME/bin:$PATH">>/etc/profile

4.運行如下命令加載環(huán)境變量告喊。

[root@web01 /usr/java]# source /etc/profile

5.運行java --version命令顯示JDK版本信息黔姜。
返回結(jié)果如圖所示秆吵,表示JDK已經(jīng)安裝成功。

[root@web01 ~]# java -version
java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)

安裝Tomcat

1.依次運行以下命令解壓apache-tomcat-9.0.27.tar.gz主穗,重命名為tomcat目錄毙芜,并設(shè)置用戶權(quán)限忽媒。

[root@web01 ~]# tar xf apache-tomcat-9.0.27.tar.gz 
[root@web01 ~]# mv apache-tomcat-9.0.27 /usr/local/tomcat
[root@web01 ~]# chown -R www.www /usr/local/tomcat

在/usr/local/tomcat/目錄下:
bin:存放Tomcat的一些腳本文件,包含啟動和關(guān)閉Tomcat服務(wù)腳本腋粥。
conf:存放Tomcat服務(wù)器的各種全局配置文件晦雨,其中最重要的是server.xml和web.xml。
webapps:Tomcat的主要Web發(fā)布目錄隘冲,默認(rèn)情況下把Web應(yīng)用文件放于此目錄闹瞧。
logs:存放Tomcat執(zhí)行時的日志文件。
2.配置server.xml文件展辞。
2.1運行以下命令切換到/usr/local/tomcat/conf/目錄奥邮。

[root@web01 ~]# cd /usr/local/tomcat/conf/

2.2運行以下命令重命名server.xml文件。

[root@web01 /usr/local/tomcat/conf]# mv server.xml server.xml.bak

2.3新建一個server.xml文件洽腺。
a.運行命令vi server.xml創(chuàng)建server.xml文件。
b. 按下i 鍵靡砌,添加以下內(nèi)容已脓。

<?xml version="1.0" encoding="UTF-8"?>
<Server port="8006" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
<Listener className="org.apache.catalina.core.AprLifecycleListener"/>
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
 type="org.apache.catalina.UserDatabase"
 description="User database that can be updated and saved"
 factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
 pathname="conf/tomcat-users.xml"/>
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080"  #port:定義下面<host></host>的監(jiān)聽端口是8080
 protocol="HTTP/1.1"
 connectionTimeout="20000"
 redirectPort="8443"
 maxThreads="1000"
 minSpareThreads="20"
 acceptCount="1000"
 maxHttpHeaderSize="65536"
 debug="0"
 disableUploadTimeout="true"
 useBodyEncodingForURI="true"
 enableLookups="false"
 URIEncoding="UTF-8"/>
<Engine name="Catalina" defaultHost="localhost">#defaultHost:定義該端口下,默認(rèn)虛擬主機(jī)是那個一個
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
  resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="/data/jpress" unpackWARs="true" autoDeploy="true">#appBase:定義該應(yīng)用存放路徑通殃,可以把war包直接上傳到該目錄度液,實現(xiàn)自動化部署
#autoDeploy:定義是否可以自動部署
#name:綁定站點的域名
#unpackWARS:定義是否自動解壓war包
<Context path="" docBase="/data/jpress" debug="0" reloadable="true" crossContext="true"/>
#path:定義本“Context”訪問時的地址,比如定義path為123画舌,則訪問該“Context”時堕担,應(yīng)該是http://host/123/,所以這個地址是訪問鏈接里面的地址,并不是系統(tǒng)目錄
#docBase:定義本“context”(類似nginx的location)所在路徑曲聂,它是一個目錄霹购,如果是絕對路徑就與appBase沒有關(guān)系,如果是相對路徑朋腋,則指的是相對appBase的一個目錄
#reloadable:定義本“Context”是否允許自動加載齐疙,即當(dāng)前應(yīng)用有所改變時膜楷,不重啟tomcat就可以自動加載
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
</Host>
</Engine>
</Service>
</Server>
說明:tomcat的網(wǎng)站目錄設(shè)置比較麻煩,appBase贞奋、docBase和Context的path的設(shè)置都對網(wǎng)站目錄有影響赌厅,建議appBase最好不要動,即webapps目錄轿塔,而docBases設(shè)置一個相對路徑(相對webapps的一個目錄)特愿,path就設(shè)定位“/”,最終結(jié)合在一起,就是webapps/docBasesh設(shè)置的目錄勾缭。

c.按ESC鍵揍障,輸入:wq并回車以保存并關(guān)閉文件。
3.設(shè)置JVM內(nèi)存參數(shù)俩由。
a.運行vi /usr/local/tomcat/bin/setenv.sh命令創(chuàng)建/usr/local/tomcat/bin/setenv.sh文件毒嫡。
b.按下i鍵,添加以下內(nèi)容采驻。

JAVA_OPTS='-Djava.security.egd=file:/dev/./urandom -server -Xms256m -Xmx496m -Dfile.encoding=UTF-8'

c. 按下Esc鍵审胚,輸入:wq并回車以保存并關(guān)閉文件。
4.啟動Tomcat并設(shè)置開機(jī)自啟動
這里根據(jù)系統(tǒng)的不同啟動方式有兩種:
a. 適用于CentOS6及其以前的版本礼旅,設(shè)置Tomcat自啟動腳本膳叨。
i.運行以下命令下載腳本。

[root@web01 ~]# wget https://github.com/lj2007331/oneinstack/raw/master/init.d/Tomcat-init

ii.運行以下命令重命名Tomcat-init痘系。

[root@web01 ~]# mv Tomcat-init /etc/init.d/tomcat

iii.運行以下命令為/etc/init.d/tomcat添加可執(zhí)行權(quán)限菲嘴。

[root@web01 ~]# chmod +x /etc/init.d/tomcat

iv.運行以下命令設(shè)置啟動腳本JAVA_HOME。

[root@web01 ~]# sed -i 's@^export JAVA_HOME=.*@export JAVA_HOME=/usr/java/jdk1.8.0_231@' /etc/init.d/tomcat

v.依次運行以下命令設(shè)置Tomcat開機(jī)自啟動汰翠。

[root@web01 ~]#chkconfig --add tomcat
[root@web01 ~]#chkconfig tomcat on

vi.運行以下命令啟動Tomcat龄坪。

[root@web01 ~]#service tomcat start  

b. 利用systemctl管理Tomcat啟動、停止复唤、重啟及開機(jī)啟動健田。
i.用service來管理服務(wù)的時候,是在/etc/init.d/目錄中創(chuàng)建一個腳本文件佛纫,來管理服務(wù)的啟動和停止妓局,在systemctl中,也類似呈宇,文件目錄有所不同好爬,在/usr/lib/systemd/system目錄下創(chuàng)建一個腳本文件tomcat,
i.使用 vi /usr/lib/systemd/system/tomcat.service創(chuàng)建tomcat.service腳本甥啄。按i輸入如下內(nèi)容:

[Unit]
Description=Tomcat
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/tomcat/pid
ExecStart=/usr/local/tomcat/bin/catalina.sh start
ExecReload=/usr/local/tomcat/bin/catalina.sh restart
ExecStop=/usr/local/tomcat/bin/catalina.sh stop

[Install]
WantedBy=multi-user.target
[Unit] 表示這是基礎(chǔ)信息
Description 是描述
After 是在那個服務(wù)后面啟動存炮,一般是網(wǎng)絡(luò)服務(wù)啟動后啟動
[Service] 表示這里是服務(wù)信息
Type 是服務(wù)類型
PIDFile 是服務(wù)的pid文件路徑, 開啟后,必須在tomcat的bin/catalina.sh中加入CATALINA_PID參數(shù)
ExecStart 是啟動服務(wù)的命令
ExecReload 是重啟服務(wù)的命令
ExecStop 是停止服務(wù)的指令
[Install] 表示這是是安裝相關(guān)信息
WantedBy 是以哪種方式啟動:multi-user.target表明當(dāng)系統(tǒng)以多用戶方式(默認(rèn)的運行級別)啟動時穆桂,這個服務(wù)需要被自動運行宫盔。

ii.在/usr/local/tomcat/bin/catalina.sh中加入CATALINA_PID參數(shù)以及JAVA_HOME的路徑參數(shù),需要在# OS specific support.$var must be set to either true or false.上加入如下參數(shù)

CATALINA_PID=/usr/local/tomcat/pid
export JAVA_HOME=/usr/java/jdk1.8.0_231
export JAVA_HOME=/usr/java/jdk1.8.0_231/jre
注意:rpm安裝的JDK無需寫入java_home參數(shù)

使用vi /usr/local/tomcat/bin/catalina.sh在# OS specific support. $var must be set to either true or false這行下添加如上的參數(shù)享完。
iii.創(chuàng)建軟鏈接
創(chuàng)建軟鏈接是為了下一步系統(tǒng)初始化時自動啟動服務(wù)

ln -s /lib/systemd/system/tomcat.service /etc/systemd/system/multi-user.target.wants/tomcat.service

ln -s 是創(chuàng)建軟鏈接
ln -s 原文件 目標(biāo)文件(快捷方式的決定地址)
如果創(chuàng)建軟連接的時候出現(xiàn)異常飘言,不要擔(dān)心,看看/etc/systemd/system/multi-user.target.wants/ 目錄是否正常創(chuàng)建軟鏈接為準(zhǔn)驼侠,有時候報錯只是提示一下,其實成功了谆吴。

$ ll /etc/systemd/system/multi-user.target.wants/
total 8
drwxr-xr-x 2 root root 4096 Mar 30 15:46 ./
drwxr-xr-x 13 root root 4096 Mar 13 14:18 ../
lrwxrwxrwx 1 root root 31 Nov 23 14:43 tomcat.service -> /lib/systemd/system/tomcat.service
...略...
iv. 刷新配置
剛剛配置的服務(wù)需要讓systemctl能識別倒源,就必須刷新配置

[root@web01 ~]#systemctl daemon-reload

如果沒有權(quán)限可以使用sudo

sudo systemctl daemon-reload

v.啟動,重啟句狼,停止笋熬。
啟動tomcat

[root@web01 ~]#systemctl start tomcat

重啟tomcat

[root@web01 ~]#systemctl restart tomcat

停止tomcat

[root@web01 ~]#systemctl stop tomcat

5.tomcat 開機(jī)自啟動
服務(wù)加入開機(jī)啟動

[root@web01 ~]#systemctl enable tomcat

禁止開機(jī)啟動

[root@web01 ~]#systemctl disable tomcat
  1. 查看狀態(tài)
[root@web01 ~]#systemctl status tomcat
● tomcat.service - Tomcat
   Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-10-25 20:37:22 CST; 1min 42s ago
  Process: 8673 ExecStart=/usr/local/tomcat/bin/catalina.sh start (code=exited, status=0/SUCCESS)
 Main PID: 8679 (java)
   CGroup: /system.slice/tomcat.service
           └─8679 /usr/java/jdk1.8.0_231/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/...

Oct 25 20:37:22 web01 systemd[1]: Starting Tomcat...
Oct 25 20:37:22 web01 catalina.sh[8673]: Tomcat started.
Oct 25 20:37:22 web01 systemd[1]: Started Tomcat.

安裝nginx

1.使用nginx官方源進(jìn)行安裝。
i.使用vi /etc/yum.repos.d/nginx.repo創(chuàng)建nginx.repo源文件腻菇。
按下i鍵胳螟,添加以下內(nèi)容。

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

ii.按下Esc鍵筹吐,輸入:wq并回車以保存并關(guān)閉文件糖耸。
iii.下載安裝nginx。

[root@web01 ~]# yum -y install nginx

iv.啟動nginx并設(shè)置開機(jī)自啟動

[root@web01 ~]#systemctl start nginx
[root@web01 ~]#systemctl enable nginx

安裝mariadb數(shù)據(jù)庫

i.使用yum方式安裝mariadb數(shù)據(jù)庫丘薛。

[root@web01 ~]# yum install mariadb-server mariadb -y

ii.啟動mariadb數(shù)據(jù)庫并添加入開機(jī)自啟動嘉竟。

[root@web01 ~]# systemctl start mariadb
[root@web01 ~]# systemctl enable mariadb

iii.配置mariadb的賬號和密碼并做初步的優(yōu)化
注意:此處除了設(shè)置新密碼,其他操作默認(rèn)回車即可洋侨。

[root@web01 ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] 
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] 
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] 
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] 
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] 
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

部署博客jpress

1.配置nginx代理舍扰。
i.使用vi /etc/nginx/conf.d/proxy.conf 創(chuàng)建代理主機(jī)。
按i輸入如下內(nèi)容:

server {
 listen 80;
 server_name jpress.weiaixiong.com;
 location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_set_header Host      $host;
    proxy_set_header X-Real-IP $remote_addr;
    }
 }

ii.按下Esc鍵希坚,輸入:wq并回車以保存并關(guān)閉文件边苹。
iii.使用nginx -t 檢查是否有錯。

[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

iv.重新啟動nginx

[root@web01 ~]# systemctl restart ngnx

2.創(chuàng)建數(shù)據(jù)庫

[root@web01 /data/jpress]# mysql -uroot -p123456
MariaDB [(none)]> create database jpress;
Query OK, 1 row affected (0.00 sec)

3.上傳源碼至站點目錄/data/jpress裁僧。

[root@web01 ~]# cd /data/jpress/
[root@web01 /data/jpress]# rz -E
rz waiting to receive.
[root@web01 /data/jpress]# ls
jpress-v2.0.8.war
[root@web01 /data/jpress]# systemctl restart tomcat.service
[root@web01 /data/jpress]# ls
jpress-v2.0.8  jpress-v2.0.8.war
[root@web01 /data/jpress]# cd jpress-v2.0.8/
[root@web01 /data/jpress/jpress-v2.0.8]# ls
META-INF  robots.txt  static  templates  WEB-INF
[root@web01 /data/jpress/jpress-v2.0.8]# cp -a ./* /data/jpress/

4.測試訪問并進(jìn)一步連接數(shù)據(jù)路進(jìn)行安裝个束。


5.安裝完成后測試結(jié)果如圖。


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
禁止轉(zhuǎn)載锅知,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者播急。

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