CentOS 7.x 下整合安装 Nginx和Tomcat 8.5
2020-05-12 16:32:28


1.安装nginx


首先访问nginx官网 www.nginx.org 。点击下图所示的任意版本的nginx-版本号的链接。

在下图的 Stable version (稳定版本)中,右键所图所示的nginx-1.14.2,获取文件url。

将下图中的url复制。

在 centOS 7中通用wge命令获取该文件。

cd
wget http://nginx.org/download/nginx-1.14.2.tar.gz

如果没有wget命令,请用命令yum -y install wget进行安装

 解压

tar zvxf nginx-1.14.2.tar.gz

进入目录

cd nginx-1.14.2/

先安装三个依赖包

yum -y install gcc

yum -y install pcre-devel

yum -y install openssl-devel

开始编译安装

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_flv_module --with-http_addition_module --with-http_ssl_module
make
make install

2.安装jre&jdk

先下载jre安装文件。

wget http://www.lerx.com/download/linux/jre-8u181-linux-x64.gz

解压

tar zvxf jre-8u181-linux-x64.gz

建立java目录并将jre移到该目录下

mkdir /usr/java
mv jre1.8.0_181/ /usr/java/

3.安装Tomcat

打开Tomcat官网,点击下图Download位置的Tomcat 8

右链下图的tag.gz,获得安装文件的url

在centOS 7中下载

wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.39/bin/apache-tomcat-8.5.39.tar.gz

解压并将该目录移到/usr/local目录下

tar zvxf apache-tomcat-8.5.39.tar.gz
mv apache-tomcat-8.5.39 /usr/local/

4.安装nginx为系统自启动服务

进入目录/usr/lib/systemd/system

 cd /usr/lib/systemd/system

编写服务文件

vi nginx.service

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
User=root
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

执行并启动服务

systemctl enable nginx.service

systemctl start nginx.service

开启80端口

firewall-cmd --zone=public --add-port=80/tcp --permanent 

firewall-cmd --reload

此时可通过浏览器看见nginx已经成功安装运行。

5.安装Tomcat为系统服务

在本例中,将服务名称改为portal

并建立用户portal,将网站根目录设置在 /home/portal/public_html

useradd portal
mkdir /home/portal/public_html

复制 /usr/local/apache-tomcat-8.5.39/conf/ 到 /home/portal/

cp -Rf /usr/local/apache-tomcat-8.5.39/conf  /home/portal/

修改 /home/portal/conf/server.xml 将 address加入Connector

<Connector port="8080" address="127.0.0.1" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

修改 /home/portal/conf/server.xml 中的host配置,加入Context信息。

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
                <Context path="" docBase="/home/portal/public_html" debug="0" reloadable="true" crossContext="true" >
			<Manager className="org.apache.catalina.session.PersistentManager" saveOnRestart="false">
				<Store className="org.apache.catalina.session.FileStore"/>
			</Manager>
		</Context>

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <!-- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" /> -->

      </Host>

建应其它相应的目录 

cd /home/portal

mkdir work

mkdir logs

mkdir temp

修改文件夹属性和权限

chown -Rf portal:portal /home/portal

chmod 755 -Rf  /home/portal

chmod 755 -Rf  /usr/local/apache-tomcat-8.5.37/bin

chmod 755 -Rf  /usr/local/apache-tomcat-8.5.37/lib

 编辑tomcat服务启动文件/usr/lib/systemd/system/portal.service

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

[Service]

User=portal

Type=forking

Environment='JAVA_HOME=/usr/java/jre1.8.0_181'
Environment='CATALINA_PID=/home/portal/temp/portal.pid'
Environment='CATALINA_HOME=/usr/local/apache-tomcat-8.5.54'
Environment='CATALINA_BASE=/home/portal'
Environment='CATALINA_OPTS=-server -Xms1024M -Xmx1024M -XX:MaxNewSize=128m -Xss512k -XX:+AggressiveOpts -XX:+UseBiasedLocking -XX:+DisableExplicitGC -XX:MaxTenuringThreshold=15 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:LargePageSizeInBytes=64m  -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -Djava.awt.headless=true'

WorkingDirectory=/home/portal/work

PIDFile=/home/portal/temp/portal.pid
ExecStart=/usr/local/apache-tomcat-8.5.54/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

注意本例中nginx配置文件的第二行需改成user  portal; 否则上传的文件将无法读取。


发布:lzh