1. 程式人生 > >快速安裝部署CentOS7 64位環境(jdk1.8+Tomcat9+Openresty+mariaDB)

快速安裝部署CentOS7 64位環境(jdk1.8+Tomcat9+Openresty+mariaDB)

安裝內容:

1) 關閉SELinex

2)禁用防火牆

3)安裝Jdk1.8.0_181

4)安裝Tomcat9.0.10

5)安裝Openresty

6)安裝MariaDB

7)配置開機啟動Tomcat

8)配置開機啟動Openresy的Nginx

 

 

具體如下:

1) 關閉SELinex

參看:https://blog.csdn.net/xinluke/article/details/51925293

檢視

[[email protected] ~]# getenforce
Disabled
[[email protected]
~]# /usr/sbin/sestatus -v SELinux status: disabled

臨時關閉

##設定SELinux 成為permissive模式
##setenforce 1 設定SELinux 成為enforcing模式
setenforce 0

永久關閉

vi /etc/selinux/config

將SELINUX=enforcing改為SELINUX=disabled 
設定後需要重啟才能生效

 

2)禁用防火牆

參看:https://www.cnblogs.com/moxiaoan/p/5683743.html

1、firewalld的基本使用

啟動: systemctl start firewalld

關閉: systemctl stop firewalld

檢視狀態: systemctl status firewalld 

開機禁用  : systemctl disable firewalld

開機啟用  : systemctl enable firewalld

 

3)安裝Jdk1.8.0_181

參看:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

mkdir -p /data/local/java

cd /data/local/java

《將下載的jdk-8u181-linux-x64.tar.gz放到/data/local/java目錄中》

tar -zxvf jdk-8u181-linux-x64.tar.gz
vi /etc/profile

export JAVA_HOME=/data/local/java/jdk1.8.0_181
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export  PATH=${JAVA_HOME}/bin:$PATH
source /etc/profile

4)安裝Tomcat9.0.10

參看:https://tomcat.apache.org/download-90.cgi

cd /data/local/

wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.10/bin/apache-tomcat-9.0.10.tar.gz

tar -zxvf apache-tomcat-9.0.10.tar.gz

《配置tomcat的Jdk》

vi /data/local/apache-tomcat-9.0.10/bin/setenv.sh 

新增幾行內容:

#!/bin/sh

export JAVA_HOME=/data/local/java/jdk1.8.0_181/
export JRE_HOME=/data/local/java/jdk1.8.0_181/jre/
export CATALINA_HOME=/data/local/apache-tomcat-9.0.10/
export CATALINA_BASE=/data/local/apache-tomcat-9.0.10/
#設定Tomcat的PID檔案
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
#新增JVM選項
JAVA_OPTS="-Xms1024m -Xmx2048m"

《配置tomcat的埠號》

也可以不修改,保持預設的8080

如果要修改做如下命令

vi /data/local/apache-tomcat-9.0.10/conf/server.xml

 

啟動命令

nohup /data/local/apache-tomcat-9.0.10/bin/start.sh

 

 

 

5)安裝Openresty

參看:http://wiki.jikexueyuan.com/project/openresty/openresty/install_on_centos.html

sudo yum-config-manager --add-repo https://openresty.org/yum/cn/centos/OpenResty.repo
sudo yum install openresty
 

 

6)安裝MariaDB

參看:https://blog.csdn.net/u013488847/article/details/81265998

1.使用rpm -qa | grep mariadb搜尋 MariaDB 現有的包:

如果存在,使用rpm -e --nodeps mariadb-*全部刪除:

rpm -qa | grep mariadb

rpm -e --nodeps mariadb-*

 

2.使用rpm -qa | grep mariadb搜尋 MariaDB 現有的包:

如果存在,使用yum remove mysql mysql-server mysql-libs compat-mysql51全部刪除;

 

3.開始新的安裝, 建立MariaDB.repo檔案

vi /etc/yum.repos.d/MariaDB.repo

[mariadb]
name = MariaDB
baseurl = http://mirrors.ustc.edu.cn/mariadb/yum/10.3/centos7-amd64/
gpgkey=http://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1

4.執行安裝命令安裝MariaDB

yum -y install MariaDB-server MariaDB-client

 

首先下載安裝包,然後進行自動安裝,安裝成功之後啟動MariaDB服務。

systemctl start mariadb #啟動服務
systemctl enable mariadb #設定開機啟動
systemctl restart mariadb #重新啟動
systemctl stop mariadb.service #停止MariaDB
systemctl start mariadb #啟動服務
systemctl enable mariadb #設定開機啟動
systemctl restart mariadb #重新啟動
systemctl stop mariadb.service #停止MariaDB

 

5.登入到資料庫

  用mysql -uroot命令登入到MariaDB,此時root賬戶的密碼為空。

mysql -uroot -p
use mysql;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'xxxxxx' WITH GRANT OPTION;
FLUSH PRIVILEGES;

 

6.進行MariaDB的相關簡單配置,使用mysql_secure_installation命令進行配置。

配置檔案如下:

/etc/my.cnf

/etc/my.cnf.d/mysql-clients.cnf

/etc/my.cnf.d/server.cnf

/etc/my.cnf.d/client.cnf

 

其中,要修改的就後面三個檔案

 

/etc/my.cnf.d/mysql-clients.cnf

#
# These groups are read by MariaDB command-line tools
# Use it for options that affect only one utility
#

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates


[mysql_upgrade]

[mysqladmin]

[mysqlbinlog]

[mysqlcheck]

[mysqldump]
quick
max_allowed_packet = 16M

[mysqlimport]

[mysqlshow]

[mysqlslap]
 

/etc/my.cnf.d/server.cnf

#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
[mysqld]
port        = 3306
socket        = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M

character_set_server = utf8mb4
event_scheduler=ON
init_connect='SET NAMES utf8mb4'

log-error = /usr/local/mysql/log/error.log
general-log-file = /usr/local/mysql/log/mysql.log
lower_case_table_name=1
max_connections = 1000

#slow query 
slow-query-log=1
slow-query-log-file=/usr/local/mysql/log/slowquery.log
long_query_time=2

# Point the following paths to different dedicated disks
#tmpdir        = /tmp/

# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!

#skip-networking

# Replication Master Server (default)
# binary logging is required for replication
log-bin=mysql-bin

# binary logging format - mixed recommended
binlog_format=mixed

# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id    = 1

# Replication Slave (comment out master section to use this)
#
# To configure this host as a replication slave, you can choose between
# two methods :
#
# 1) Use the CHANGE MASTER TO command (fully described in our manual) -
#    the syntax is:
#
#    CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,
#    MASTER_USER=<user>, MASTER_PASSWORD=<password> ;
#
#    where you replace <host>, <user>, <password> by quoted strings and
#    <port> by the master's port number (3306 by default).
#
#    Example:
#
#    CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
#    MASTER_USER='joe', MASTER_PASSWORD='secret';
#
# OR
#
# 2) Set the variables below. However, in case you choose this method, then
#    start replication for the first time (even unsuccessfully, for example
#    if you mistyped the password in master-password and the slave fails to
#    connect), the slave will create a master.info file, and any later
#    change in this file to the variables' values below will be ignored and
#    overridden by the content of the master.info file, unless you shutdown
#    the slave server, delete master.info and restart the slaver server.
#    For that reason, you may want to leave the lines below untouched
#    (commented) and instead use CHANGE MASTER TO (see above)
#
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id       = 2
#
# The replication master for this slave - required
#master-host     =   <hostname>
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user     =   <username>
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password =   <password>
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port     =  <port>
#
# binary logging - not required for slaves, but recommended
#log-bin=mysql-bin

# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = /usr/local/mysql/data
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /usr/local/mysql/data
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 16M
#innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50

#
# * Galera-related settings
#
[galera]
# Mandatory settings
#wsrep_on=ON
#wsrep_provider=
#wsrep_cluster_address=
#binlog_format=row
#default_storage_engine=InnoDB
#innodb_autoinc_lock_mode=2
#
# Allow server to accept connections on all interfaces.
#
#bind-address=0.0.0.0
#
# Optional setting
#wsrep_slave_threads=1
#innodb_flush_log_at_trx_commit=0

# this is only for embedded server
[embedded]

# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]

# This group is only read by MariaDB-10.3 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.3]

/etc/my.cnf.d/client.cnf
[client]
#password    = your_password
port        = 3306
socket        = /tmp/mysql.sock
default-character-set = utf8mb4
 

 

dump備份/mysql還原資料庫

 

參看:https://blog.csdn.net/u010098331/article/details/50896175

備份資料庫

"D:\tools\MariaDB 10.2\bin\mysqldump" -uroot -p xxx_site > d:/websites/dumpsvs0729.sql

 

還原資料庫

mysql -uroot -hlocalhost -p'xxxx' -P3306 -databases test xxx_site < /data/local/db/dumpsvs0729.sql

 

7)配置開機啟動Tomcat

參看:https://blog.csdn.net/u012351661/article/details/76254268

假設Tomcat的安裝路徑為/data/local/apache-tomcat-9.0.10

1 為Tomcat新增啟動引數

catalina.sh在執行的時候會呼叫同級路徑下的setenv.sh來設定額外的環境變數,因此在/opt/tomcat/bin路徑下建立setenv.sh檔案,內容如下:

vi /data/local/apache-tomcat-9.0.10/bin/setenv.sh

#!/bin/sh

export JAVA_HOME=/data/local/java/jdk1.8.0_181/
export JRE_HOME=/data/local/java/jdk1.8.0_181/jre/
export CATALINA_HOME=/data/local/apache-tomcat-9.0.10/
export CATALINA_BASE=/data/local/apache-tomcat-9.0.10/
#設定Tomcat的PID檔案
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
#新增JVM選項
JAVA_OPTS="-Xms1024m -Xmx2048m"

chmod 755 /data/local/apache-tomcat-9.0.10/bin/setenv.sh


2 編寫tomcat.service檔案

先建立並儲存一個tomcat.pid

vi /data/local/apache-tomcat-9.0.10/tomcat.pid

chmod 775 tomcat.pid


在/usr/lib/systemd/system路徑下新增tomcat.service檔案,內容如下:

vi /usr/lib/systemd/system/tomcat.service

[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/data/local/apache-tomcat-9.0.10/tomcat.pid
ExecStart=/data/local/apache-tomcat-9.0.10/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

##[unit]配置了服務的描述,規定了在network啟動之後執行,
##[service]配置服務的pid,服務的啟動,停止,重啟
##[install]配置了使用使用者
 


3 將Tomcat加入服務管理

systemctl enable tomcat.service
systemctl disable tomcat.service
systemctl start tomcat.service
systemctl stop tomcat.service

systemctl restart tomcat.service

 

 

8)配置開機啟動Openresy的Nginx

參看:http://stevendu.iteye.com/blog/2269166

vi /usr/lib/systemd/system/nginx.service

[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t
ExecStart=/usr/local/openresty/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

systemctl daemon-reload

systemctl enable nginx.service

systemctl start nginx.service

systemctl stop nginx.service

systemctl status nginx.service

systemctl restart nginx.service