DEV Community

Cover image for Installing Tomcat + Nginx in Ubuntu Cloud server
Z. QIU
Z. QIU

Posted on • Edited on

1

Installing Tomcat + Nginx in Ubuntu Cloud server

Info: This post is one of the articles in context of this post: Huawei Cloud ECS server notes

I have just now installed TomCat and Nginx on our Huawei Cloud server. In this post, I would like to note the operations I've done in this task.

TomCat

I have created file ~/launchers/tomcat_8888.sh and filled it with the following content:

docker rm -f tomcat_8888
docker run \
 -it -d --privileged=true \
 --name tomcat_8888 \
 --net bridge-network \
 -p 8888:8080  \
 -v /home/jemaloQ/www_server:/usr/local/tomcat/webapps \
 -v /home/jemaloQ/docker/www_server/logs:/usr/local/tomcat/logs \
 tomcat:latest
Enter fullscreen mode Exit fullscreen mode

Launch TomCat as docker container:

sudo sh ~/launchers/tomcat_8888.sh
Enter fullscreen mode Exit fullscreen mode

Now create a webpage for testing tomcat:

cd ~/www_server
mkdir home
cd home/
touch index.html
Enter fullscreen mode Exit fullscreen mode

Put the following html code in this index.html file:

<!DOCTYPE html>
<html>
<head>
<title>Test TomCat Servie</title>
</head>
<body>

<h1>Tomcat over docker is working</h1>
<p> Port = 8888 </p>

</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Open the browser, try http://myip:8888/test, voilà:
Alt Text

Nginx

I use the following cmd line for installing Nginx in a native way:

sudo apt install nginx-full
Enter fullscreen mode Exit fullscreen mode

Check if Nginxservice is running:

service nginx status
Enter fullscreen mode Exit fullscreen mode

The default conf file of Nginx "nginx.conf" can be found in directory /etc/nginx/. Usually, we do not need to modify this file. In this file, there is one line as: include /etc/nginx/conf.d/*.conf;, meaning that Nginx shall take into account all .conf user files created in /etc/nginx/conf.d repository.

Below are operations that I have performed (Some may require sudo permission):

touch /etc/nginx/conf.d/myconf.conf
vi /etc/nginx/conf.d/myconf.conf
Enter fullscreen mode Exit fullscreen mode

In myconf.conf file, I add the following lines:

server {
    listen       80;
    server_name 111.22.133.117 sssbot.cn localhost;
    # server_name localhost;


    gzip            on;
    gzip_types      text/plain application/xml text/css application/javascript;
    gzip_min_length 1000;



        location /demo/industry_safety {
                        proxy_pass http://111.22.133.117:8888/industry_safety;
        }

        location ~* /demo/industry_safety/([a-z]+)$   {
                   rewrite /demo/industry_safety/([a-z]+)$ /demo/industry_safety/;                                                                              
                        }


        location /test {
          proxy_pass http://111.22.133.117:8888/test;
        }

        location / {
                        default_type application/octet-stream;
                        include /etc/nginx/mime.types;
                        root /usr/share/nginx/html;
                        index  index.html index.htm;
                        try_files $uri $uri/  /index.html;

        }


    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
Enter fullscreen mode Exit fullscreen mode

Make nginx reload all its configurations:

nginx -s reload

Enter fullscreen mode Exit fullscreen mode

Check if http://myip:8888/test/ has been redirected to http://myip/test:
Alt Text

I have a real SaaS project named "industry_safety" whose front-end is a Vue-based website. There are many static resources in this project which may bring problems after URL redirection. As shown in /etc/nginx/conf.d/myconf.conf file, I have redirected original url http://myip:8888/industry_safety/ to http://myip/demo/industry_safety/.

Check it now and it works :

Alt Text

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

Try REST API Generation for MS SQL Server.

DevOps for Private APIs. With DreamFactory API Generation, you get:

  • Auto-generated live APIs mapped from database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more