Skip to content

Commit

Permalink
Application server
Browse files Browse the repository at this point in the history
  • Loading branch information
AyomideA-S committed Aug 24, 2024
1 parent fc1a24d commit 3d1e9f6
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 138 deletions.
51 changes: 20 additions & 31 deletions 0x1A-application_server/2-app_server-nginx_config
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
# server {
# listen 80;
# listen [::]:80;
#
# server_name 3.238.176.253;
#
# location / {
# proxy_set_header X-Served-By 9401-web-01;
# proxy_pass http://127.0.0.1:5000/airbnb-onepage/;
# }
# }

# Configures Nginx to serve the route /airbnb-onepage/ from AirBnB_clone_v2.

server {
# Listen on port 80
listen 80;
listen [::]:80;

# Use IP of server as domain name
server_name 3.238.176.253;
listen 80;
listen [::]:80 default_server;
add_header X-Served-By $hostname;
root /etc/nginx/html;
index index.html index.htm;
location /redirect_me {
return 301 https://www.youtube.com/watch?v=QH2-TGUlwu4;
}
root /var/www/html;

# Customize HTTP response header
add_header X-Served-By 9401-web-01;
server_name _;

# Serve /airbnb-onepage/ route from AirBnB_clone_v2
location = /airbnb-onepage/ {
proxy_pass http://127.0.0.1:5000/airbnb-onepage/;
location /airbnb-onepage {
proxy_pass http://0.0.0.0:5000/airbnb-onepage;
}

# 404 error page
error_page 404 /404.html;
location /404 {
root /var/www/html;
internal;
location /hbnb_static {
alias /data/web_static/current/;
}
}

location /{
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
53 changes: 24 additions & 29 deletions 0x1A-application_server/3-app_server-nginx_config
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
# Configures Nginx to serve the /number_odd_or_even/ route on AirBnB_clone_v2.

server {
# Listen on port 80
listen 80;
listen [::]:80;

# Use server IP as domain name
server_name 3.238.176.253;

# Customize HTTP response header
add_header X-Served-By 9401-web-01;

# Serve /airbnb-onepage/ route on AirBnB_clone_v2
location = /airbnb-onepage/ {
proxy_pass http://127.0.0.1:5000/airbnb-onepage/;
}

# Serve /number_odd_or_even/ route on AirBnB_clone_v2
location ~ /airbnb-dynamic/number_odd_or_even/(\d+)$ {
proxy_pass http://127.0.0.1:5001/number_odd_or_even/$1;
}

# 404 error page
error_page 404 /404.html;
location /404 {
root /var/www/html;
internal;
}
}
listen 80 default_server;
location /hbnb_static { alias /data/web_static/current/;}
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html
server_name_;
add_header X-Served-By $hostname;
location / {
try_files $uri $uri/ =404;
}
location /airbnb-onepage {
proxy_pass http://0.0.0.0:5000/airbnb-onepage;
}
location ~ ^/airbnb-dynamic/number_odd_or_even/([0-9]+)$ {
proxy_pass http://0.0.0.0:5001/number_odd_or_even/$1;
}
if ($request_filename ~ redirect_me){
rewrite ^ https://th3-gr00t.tk/ permanent;
}
error_page 404 /error_404.html;
location = /error_404.html {
internal;
}
}
61 changes: 27 additions & 34 deletions 0x1A-application_server/4-app_server-nginx_config
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
# Configures Nginx to serve the AirBnB_clone_v3 API.

server {
# Listen on port 80
listen 80;
listen [::]:80;

# Use server IP as domain name
server_name 3.238.176.253;

# Customize HTTP response header
add_header X-Served-By 9401-web-01;

# Serve /airbnb-onepage/ route on AirBnB_clone_v3
location = /airbnb-onepage/ {
proxy_pass http://127.0.0.1:5000/airbnb-onepage/;
}

# Serve /number_odd_or_even/ route on AirBnB_clone_v3
location ~ /airbnb-dynamic/number_odd_or_even/(\d+)$ {
proxy_pass http://127.0.0.1:5001/number_odd_or_even/$1;
}

# Serve API on AirBnB_clone_v3
location /api {
proxy_pass http://127.0.0.1:5002/api;
}

# 404 error page
error_page 404 /404.html;
location /404 {
root /var/www/html;
internal;
}
}
listen 80 default_server;
location /hbnb_static { alias /data/web_static/current/;}
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html
server_name_;
add_header X-Served-By $hostname;
location / {
try_files $uri $uri/ =404;
}
location /airbnb-onepage {
proxy_pass http://0.0.0.0:5000/airbnb-onepage;
}
location ~ ^/airbnb-dynamic/number_odd_or_even/([0-9]+)$ {
proxy_pass http://0.0.0.0:5001/number_odd_or_even/$1;
}
location /api/ {
proxy_pass http://0.0.0.0:5002;
}
if ($request_filename ~ redirect_me){
rewrite ^ https://th3-gr00t.tk/ permanent;
}
error_page 404 /error_404.html;
location = /error_404.html {
internal;
}
}
76 changes: 32 additions & 44 deletions 0x1A-application_server/5-app_server-nginx_config
Original file line number Diff line number Diff line change
@@ -1,45 +1,33 @@
# Configures Nginx to serve the complete AirBnB_clone_v4 application.

server {
# Listen on port 80.
listen 80;
listen [::]:80;

# Use server IP as domain name
server_name 3.238.176.253;

# Customize HTTP response header
add_header X-Served-By 9401-web-01;

# Serve /airbnb-onepage/ route on AirBnB_clone_v2
location = /airbnb-onepage/ {
proxy_pass http://127.0.0.1:5000/airbnb-onepage/;
}

# Serve /number_odd_or_even/ route on AirBnB_clone_v2
location ~ /airbnb-dynamic/number_odd_or_even/(\d+)$ {
proxy_pass http://127.0.0.1:5001/number_odd_or_even/$1;
}

# Serve AirBnB_clone_v3 API
location /api {
proxy_pass http://127.0.0.1:5002/api;
}

# Configure /2-hbnb route of AirBnB_clone_v4 as root location
location / {
proxy_pass http://127.0.0.1:5003/2-hbnb;
}

# Serve static content for AirBnB_clone_v4
location /static {
proxy_pass http://127.0.0.1:5003;
}

# 404 error page
error_page 404 /404.html;
location /404 {
root /var/www/html;
internal;
}
}
listen 80 default_server;
location /hbnb_static { alias /data/web_static/current/;}
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html
server_name_;
add_header X-Served-By $hostname;
location /static/ {
# rewrite ^(/static/[^?]*)\?.*$ $1 last;
alias /home/ubuntu/AirBnB_clone_v4/web_dynamic/static/;
try_files $uri $uri/ =404;
}
location / {
try_files $uri $uri/ =404;
}
location /airbnb-onepage {
proxy_pass http://0.0.0.0:5000/airbnb-onepage;
}
location ~ ^/airbnb-dynamic/number_odd_or_even/([0-9]+)$ {
proxy_pass http://0.0.0.0:5001/number_odd_or_even/$1;
}
location /api/ {
proxy_pass http://0.0.0.0:5002;
}
if ($request_filename ~ redirect_me){
rewrite ^ https://th3-gr00t.tk/ permanent;
}
error_page 404 /error_404.html;
location = /error_404.html {
internal;
}
}
83 changes: 83 additions & 0 deletions 0x1A-application_server/app_server_script
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env bash
echo "Updating Packages and Installing Requirements"

# Update Package Manager
sudo apt-get update
sudo apt-get install -y nginx
# Install pip
sudo apt-get install -y python3-pip
# Install Flask, flask_cors, sqlalchemy Using PIP
pip install flask
pip install flask_cors
pip install sqlalchemy
# Install net-tools
sudo apt install -y net-tools
# Install GUNICORN
sudo apt-get install -y gunicorn
# Remove any current AIRBNB Repositoty
rm -rf AirBnB_clone_v*
# Clone My Repository
git clone https://github.com/AyomideA-S/AirBnB_clone_v2
git clone https://github.com/AyomideA-S/AirBnB_clone_v3
git clone https://github.com/AyomideA-S/AirBnB_clone_v4
# Change Directory
cd AirBnB_clone_v2
# Switch Branches
git checkout app-server

# Define the path to the Nginx configuration file to be overwritten
NGINX_CONFIG="/etc/nginx/sites-enabled/default"

# Overwrite the Nginx configuration file with the provided configuration
sudo tee "$NGINX_CONFIG" > /dev/null <<EOF
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
add_header X-Served-By \$hostname;
location /hbnb_static {
alias /data/web_static/current/;
}
location /static/ {
alias /home/ubuntu/AirBnB_clone_v4/web_dynamic/static/;
try_files \$uri \$uri/ =404;
}
location / {
proxy_pass http://0.0.0.0:5003;
}
location /airbnb-onepage {
proxy_pass http://0.0.0.0:5000/airbnb-onepage;
}
location ~ ^/airbnb-dynamic/number_odd_or_even/([0-9]+)$ {
proxy_pass http://0.0.0.0:5001/number_odd_or_even/\$1;
}
location /api/ {
proxy_pass http://0.0.0.0:5002;
}
if (\$request_filename ~ redirect_me){
rewrite ^ https://th3-gr00t.tk/ permanent;
}
error_page 404 /error_404.html;
location = /error_404.html {
internal;
}
}
EOF

# Restart Nginx to apply the changes
sudo systemctl restart nginx

echo "Nginx configuration has been successfully updated."

# Setup tmux session for task 4
cd /home/ubuntu/AirBnB_clone_v3
tmux new-session -d 'gunicorn --bind 0.0.0.0:5002 api.v1.app:app'

# Setup tmux session for task 5
cd /home/ubuntu/AirBnB_clone_v4
git checkout app-server

echo "Done !!!!"
20 changes: 20 additions & 0 deletions 0x1A-application_server/gunicorn.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[Unit]
Description = HBNB
After = network.target

[Service]
PermissionsStartOnly = true
PIDFile = /run/hbnb/hbnb.pid
User = ubuntu
Group = ubuntu
WorkingDirectory = /home/ubuntu/AirBnB_clone_v4
ExecStartPre = /bin/mkdir /run/hbnb
ExecStartPre = /bin/chown -R ubuntu:ubuntu /run/hbnb
ExecStart = /usr/bin/env HBNB_MYSQL_USER=hbnb_dev HBNB_MYSQL_PWD=hbnb_dev_pwd HBNB_MYSQL_HOST=localhost HBNB_MYSQL_DB=hbnb_dev_db HBNB_TYPE_STORAGE=db gunicorn -w 3 --access-logfile /tmp/airbnb-access.log --error-logfile /tmp/airbnb-error.log --bind 0.0.0.0:5003 web_dynamic.2-hbnb:app --pid /run/hbnb/hbnb.pid
ExecReload = /bin/kill -s HUP $MAINPID
ExecStop = /bin/kill -s TERM $MAINPID
ExecStopPost = /bin/rm -rf /run/hbnb
PrivateTmp = false

[Install]
WantedBy = multi-user.target

0 comments on commit 3d1e9f6

Please sign in to comment.