Web Server¶
- Web Servers serve the static client-side content (HTML, CSS, JS)
- Application Servers serve dynamic content like the backend (responsible for the business logic)
- Web Frameworks like Express, Spring and Flask make it easy to develop application to run on a web server. This application code can then be hosted on the web server using Apache Tomcat or Nginx web server. These tools run processes that listen for requests from clients on a particular port.
- Web Servers can also host multiple applications and redirect the request based on the URL or hostname or path to the right application.
Static vs Dynamic Websites
- Static websites host static content (HTML, CSS and images that don't change). After the site is served, there is no interaction with the web server.
- Apache HTTP and Nginx are examples of static web server
- Dynamic websites host content that changes (eg. products on Amazon). They have a backend to process payments, shipping information etc.
- Apache Tomcat and uWSGI are examples of dynamic web servers or application servers
Apache Web Server¶
- Open-source HTTP web server, developed and maintained by the Apache Foundation
- Install Apache in CentOS -
yum install httpd
)- If you have firewall on your system, add a rule to allow HTTP traffic
firewall-cmd --permanent --add-service=http
- If you have firewall on your system, add a rule to allow HTTP traffic
- View logs
cat /var/log/httpd/access_log
- access logscat /var/log/httpd/error_log
- error logs
- Config file -
/etc/httpd/conf/httpd.conf
Listen
- Port or IP:Port to listen onDocumentRoot
- Directory where to place the static filesServerName
- Domain name of the website (this will require a DNS to work)
Host multiple webpages on the same web server
We need to create two virtual hosts, each hosting a website with a ServerName and
DocumentRoot. Based on the domain name in the request, the Apache server will automatically
forward the request to the right application.
If the number of webpages grow, we can separate the config files for each website and include them in the main config file.
Apache Tomcat¶
- Provides a web server environment where we can host Java based web applications
- Installing Apache Tomcat on CentOS
- Inside the
apache-tomcat-<version>
directory, there are multiple directoriesbin
- contains scripts to control the web serverconf
- contains configuration files for the web serverlogs
- contains log fileswebapps
- store web application files hosted by tomcat server- Package the web application into a
.war
file and and move it towebapps
folder to host. If tomcat server is running, it will automatically detect the new app and host it.
- Package the web application into a
Deployments¶
Flask App¶
- Install dependencies -
pip install -r requirements.txt
- Run the app
python main.py
- good for running in development server (not in production)- For production, we need to run it in a production grade web server for Flask like Gunicorn, uWSGI, Gevent or Twisted Web.
gunicorn <filename>:<flask-app-component>
eg.gunicorn main:app
gunicorn main:app -w 3
- spawn 3 workers to host the flask app
NodeJS App¶
- Install dependencies -
npm install
- Run the app
node app.js
- if scriptspackage.json
are emtpynpm run start
- if start script is present- For production, we need to run the app in a production grade web server for NodeJS like SupervisorD, Forever and Process Manager 2 (PM2).
- PM2 is a production grade process manager for NodeJS with built in load balancer
pm2 start app.js
pm2 start app.js -i 3
- run 3 processes (workers)pm2 delete app.js
- terminate all the running processes of the app
IP Addresses and Ports¶
- Computer systems like laptops or servers have multiple wired and wireless network interfaces for connectivity.
- Once a system connects to network via an interface, the interface is assigned an IP address.
- Every network interface is divided into multiple logical components called ports (max 65535 ports per IP address). Each port is a communication endpoint.
- Programs running on the system can listen on one of the ports for requests.
- A system can connect to the same network through two different interfaces and hence take up two IP addresses (one for each interface).
- When running an app, we can specify the IP on which the port should be opened for the app to listen on.
- We can open the port on all the available IP addresses by specifying
host = 0.0.0.0
- If we don't want to open a port on an IP address on the network (eg, to test an app in our laptop's browser), we can omit the
host
field. In this case, the system opens the specified port on127.0.0.1
(loopback address)
Every host has a built in virtual interface called Loopback Address which always equals
127.0.0.1
. Opening a port at this IP means opening it locally on the system (not the network). The loopback address it also referred to aslocalhost
Last updated: 2022-10-08