Installing Tomcat + Nginx in Ubuntu Cloud server
Tags: tomcat, nginx, cloud, devops
Published: 2020-11-23 10:02:07

######Info: This post is one of the articles in context of this post: [Huawei Cloud ECS server notes](https://dev.to/jemaloq/huawei-cloud-ecs-server-notes-1gnj)


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: 
```bash
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
```

Launch TomCat as docker container:
```bash
sudo sh ~/launchers/tomcat_8888.sh
```
Now create a webpage for testing tomcat:
```bash
cd ~/www_server
mkdir home
cd home/
touch index.html
```
Put the following html code in this index.html file:
```html
<!DOCTYPE html>
<html>
<head>
<title>Test TomCat Servie</title>
</head>
<body>

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

</body>
</html>

```

Open the browser, try http://myip:8888/test, voilà:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vuy4ru5bt041gon9e91q.png)


##Nginx##
I use the following cmd line for installing **Nginx** in a native way:
```bash
sudo apt install nginx-full
```
Check if **Nginx**service is running:
```bash
service nginx status
```
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):

```bash
touch /etc/nginx/conf.d/myconf.conf
vi /etc/nginx/conf.d/myconf.conf
```
In *myconf.conf* file, I add the following lines:

```bash
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;
    }
}
```

Make nginx reload all its configurations:
```bash
nginx -s reload

```

Check if http://myip:8888/test/ has been redirected to http://myip/test:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5s8hbdhl6102p49o8wel.png)


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](https://dev-to-uploads.s3.amazonaws.com/i/h56zsmilp95nz1j2j465.png)