DEV Community

Shahriyar Al Mustakim Mitul
Shahriyar Al Mustakim Mitul

Posted on • Edited on

DevOps Prerequisite (Part 5): Web server

What is a web server?

Most apps and services has moved to cloud or servers. SO, we need to learn about them.

Let's see what is standalone desktop application

Image description

Here the calculate can calculate within the desktop

Whereas, slack is an example of client server application where slack application needs messages from others stored in servers. It loads them from server to local device/desktop.

Image description

So, what are these servers?

Servers that keeps messages are chat servers, all the servers which keeps webs content (html, css, images etc) etc is web server, behind web server we have application server to keep backend content (processing , updating database etc)

Image description
etc etc.

Basically all of these servers (generally a virtual machine or physical machine) have some ports from where it listens to certain requests.

Web frameworks vs web servers

Assume that you have asked to check the website (www.shop-app.com) and then the website requests the server to check the content

Image description

Here is your response shown in the webpage

Image description

Here webpage was our client. So, it's a client server system.

Here client uses frontend languages (html, css etc) to communicate to the web server whereas, web servers use backend languages (java, python, js) etc.

Image description

Both codes were developed in a same application code
For example, using springboot client can call for products and backend can map for it

Image description

Web framework (helps developing application code) basically makes these tasks easier.

Web server whereas hosts the application code.

Image description

Apache web servers

In centos , you can use just install apache web server, start it and check status

Image description
We can also check once a user access the server using access_logs and check errors from error_logs

Image description

Now, the question is which port does this server listen to? You can check that from /etc/httpd/conf/httpd/conf file

Image description

This is how you can run the server content

Image description

Now assume you have made your own html, css and javascript files and you want to keep them on the server.
You can just keep them all within /var/www/html (and mention DocumentRoot "/var/www/html" on the httpd.conf file ) and once reload, you can see this

Image description
Your own website is loaded.

You can also access the website according to your desired website name (www.house.com:80)

Image description
To access that from anywhere on internet, you need to keep that in the DNS server and to access that locally , you can set the local IP address as the website address and save it at /etc/hosts

Image description

Assume you want to keep 2 website contents (house.com and oranges.com) in the web server and you want to check them,

Image description

you can now add these codes in the config file. You have to make sure that all the codes(html, css, js) necessary for house.com is available in /var/www/houses and all the codes needed for organges.com is in /var/www/oranges

The webserver will listen to port 80 and thus VirtualHOst is set to 80
Also, rather than adding all of these codes to the main config file, you can keep them in their own config file and then mention them in the main config file using Include

Image description

Apache Tomcat
We can also have java based web server

Image description
Make sure to install java first and then install apache tomcat

This server runs on port 8080

Image description

This is the default website.

From the tomcat file, you can check bin (keeps sh and bat files for shutdown , start etc), conf ( for various configurations, ports etc) , logs( keeps various logs), webapps (where codes for the website remains and if you need to run your own code, place it there) and other folders which is basically responsible to make the website run

Image description

Let's do it. Let's assume our application is called "app" and we have various java files, web page or others etc there. We can now make an web archive (war) instead of tar (which packages/compresses normal java files and dependencies). As we have more than normal java files like web based codes, we use war

Image description
We can also use mvn or gradle to package the app.

Once done, we need to move this to webapps folder. One removed, server will automatically generate some INF files and redirect to this new code

Image description

Now, you can check your website here

Image description
by appending /app (we have app.war file added to the folder) with the previous website name.

*Flask (Python based web framework)

Image description

When we start using flask, we will need these files.

First we install all of the required files ( pip install -r requirements.txt) and then use the main.py

Image description

This is good for dev test but for production, we need production grade servers.

Instead we can use tools like Gunicorn, uWSGI etc to deploy the app in production servers.

Image description

The file name is main and the flash app name is app. You can run two workers using -w 2

Image description

Using NodeJs

We can also use the javascript code (for the web server) to . So, here our codes is within app.js

Image description
You can install first and it will install all of the files mentioned in the package.json file and then you can start it.

But if you have scripts mentioned, you can start that script too.

Scripts are mentioned in the package.json file.
Image description

There are some run times which you should use instead of using node alone.
This run times will moderate nodes.For example, pm2 etc.
Install pm2

sudo npm install pm2@latest -g
Enter fullscreen mode Exit fullscreen mode

Run the app.js using pm2

pm2 start app.js
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

IP address and ports

Assume this is our laptop and it has a network adapter (enp0s3)

Image description
Once it connects with a switch, it gets one IP address assigned to it.
You can check the IP address using ip addr show command

Image description

If we assign an WIFI, same thing happens.

Image description

Each of this IP has a lot of ports.

Image description

Now lets understand how a python flask works.
generally it listens to port 5000 by default but we can change it.

Image description
We have told it to listed to port 8000

Image description

But we have 2 IP addresses and both have port 8000. But we did not specify that.

Image description

Now, we have specified the IP address and port to communicate.

Also, if we want to get access to both Ips, we can set the host Ip to 0.0.0.0

Image description

If we don't set any Ip, it will automatically use default IP:127.0.0.1

Image description

This IP is referred as loop back address.
Image description

You can also check the IP address and you will see it's available mentioning (LOOPBACK,UP,....)

Image description
You can now , access to port 8000 of this ip using http://127.0.0.1:8000
or, use http://localhost:8000 instead
Image description

Image description

Image description

Here you will see device 1 and device 2.
We did set our port to 8000 for device 1 and it is connected to internet. So, we can use localhost:8000 here but

if you want to use it on device 2, it won't work as device 2 is not connected to internet and we did not set anything for this device till now.

Image description
Lab

Image description

Image description

Image description

Image description

Image description

Top comments (0)