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
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.
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)
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
Here is your response shown in the webpage
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.
Both codes were developed in a same application code
For example, using springboot client can call for products and backend can map for it
Web framework (helps developing application code) basically makes these tasks easier.
Web server whereas hosts the application code.
Apache web servers
In centos , you can use just install apache web server, start it and check status
We can also check once a user access the server using access_logs and check errors from error_logs
Now, the question is which port does this server listen to? You can check that from /etc/httpd/conf/httpd/conf
file
This is how you can run the server content
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
You can also access the website according to your desired website name (www.house.com:80)
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
Assume you want to keep 2 website contents (house.com and oranges.com) in the web server and you want to check them,
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
Apache Tomcat
We can also have java based web server
Make sure to install java first and then install apache tomcat
This server runs on port 8080
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
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
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
Now, you can check your website here
by appending /app (we have app.war file added to the folder) with the previous website name.
*Flask (Python based web framework)
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
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.
The file name is main
and the flash app name is app
. You can run two workers using -w 2
Using NodeJs
We can also use the javascript code (for the web server) to . So, here our codes is within app.js
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.
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
Run the app.js using pm2
pm2 start app.js
IP address and ports
Assume this is our laptop and it has a network adapter (enp0s3)
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
If we assign an WIFI, same thing happens.
Each of this IP has a lot of ports.
Now lets understand how a python flask works.
generally it listens to port 5000 by default but we can change it.
We have told it to listed to port 8000
But we have 2 IP addresses and both have port 8000. But we did not specify that.
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
If we don't set any Ip, it will automatically use default IP:127.0.0.1
This IP is referred as loop back address.
You can also check the IP address and you will see it's available mentioning (LOOPBACK,UP,....)
You can now , access to port 8000 of this ip using http://127.0.0.1:8000
or, use http://localhost:8000
instead
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.
Top comments (0)