fastapi project
(env) atul@atul-Lenovo-G570:~/fprofile$ pip3 install "fastapi[all]"
(env) atul@atul-Lenovo-G570:~/fprofile$ pip3 freeze > requirements.txt
The command uvicorn main:app refers to:
uvicorn main:app --reload
(env) atul@atul-Lenovo-G570:~/fprofile$ pip3 install sqlalchemy
(env) atul@atul-Lenovo-G570:~/fprofile$ pip3 install psycopg2-binary
(env) atul@atul-Lenovo-G570:~/fprofile$ pip3 install alembic
Below command will create an alembic directory with necessary configuration files.
(env) atul@atul-Lenovo-G570:~/fprofile$ alembic init alembic
Open the alembic.ini file in the alembic directory and make the following changes:
sqlalchemy.url = postgresql://postgres:123456789@localhost:5432/fprofile_db
Open the alembic/env.py and add below line of code
*******************
from database.dbconnection import Base # by atul
from database.model import * # by atul
target_metadata = Base.metadata
***********************
If you want alembic handles migrations follow this method: In the alembic folder edit env.py and find target_metadata line and edit like the following
import the "from main import Base" in alembic/env.py file and set the "target_metadata = Base.metadata" in alembic/env.py file
Reference url https://fastapi.blog/blog/posts/2023-07-20-fastapi-sqlalchemy-migrations-guide
(env) atul@atul-Lenovo-G570:~/fprofile$ alembic revision --autogenerate -m "Initial migration"
(env) atul@atul-Lenovo-G570:~/fprofile$ alembic upgrade head
create password hashing https://fastapi.tiangolo.com/tutorial/security/oauth2-jwt/
(env) atul@atul-Lenovo-G570:~/fprofile$ pip3 install "passlib[bcrypt]"
(env) atul@atul-Lenovo-G570:~/fprofile$ pip3 install python-jose
(env) atul@atul-Lenovo-G570:~/fprofile$ openssl rand -hex 32
sudo apt update
sudo apt install nginx
$ sudo ufw app list
# output is like blow
Available applications:
Apache
Apache Full
Apache Secure
CUPS
Nginx Full
Nginx HTTP
Nginx HTTPS
. enable the ufw status
:~$ sudo ufw enable
:~$ sudo ufw status
:~$ sudo ufw allow 'Nginx Full'
#output like below
Rules updated
Rules updated (v6)
sudo systemctl stop apache2
atul@atul-Lenovo-G570:~$ sudo systemctl start nginx
atul@atul-Lenovo-G570:~$ systemctl status nginx
create ec2 instance if not created in aws
Install nginx server if not installed
Do not enable the ufw command. Otherwise you cannot connect ec2 instance by ssh. You cannot connect from putty. You cannot connect after clicking connect command.
create in fprofile directory in /var/www in ec2 instance
sudo mkdir /var/www/fprofile
sudo chmod ugo+rwx fprofile
:~$ python3 -V
Install python version according to you. In this application we are using python3.10 version so install python version according to your requirement
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.10
python3.10 -V
# or
python3.10 --version
install the python virtual environment
:~$ sudo apt install python3.10-venv
:/var/www/fprofile$ python3.10 -m venv env
note env is the virtual directory name
:/var/www/fprofile$ source env/bin/activate
:/var/www/fprofile$ source env/bin/deactivate
:/var/www/fprofile$ source env/bin/activate
(venv) ubuntu@********122:/var/www/fprofile$ python -V
# Or
(venv) ubuntu@********122:/var/www/fprofile$ python3 -V
# in output you can see Python3.10.14
(venv) ubuntu@********122:/var/www/fprofile$ pip -V
# Or
(venv) ubuntu@********122:/var/www/fprofile$ pip3 -V
# you can see pip 23.0.1 using both above command
(venv) ubuntu@********122:/var/www/fprofile$ python3.10 -m pip install pip=22.0.1
(venv) ubuntu@********122:/var/www/fprofile$ touch index.html
(venv) ubuntu@********122:/etc/nginx/sites-available$ sudo chmod ugo+rwx default
server {
************
root /var/www/fprofile;
************
}
(venv) ubuntu@********122:/etc/nginx/sites-available$ sudo systemctl restart nginx
(env) atul@atul-Lenovo-G570:~/fprofile1$ pip3 install -r requirements.txt
(env):/var/www/fprofile$ pip3 install -r requirments.txt
:etc/nginx/sites-available$ sudo nginx -t
You will see Syntax is ok if all cofiguration correct.
apt install net-tools
server {
************
server_name 107.22.13.121;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
proxy_pass http://0.0.0.0:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
********************
}
url for help: https://www.geeksforgeeks.org/nohup-command-in-linux-with-examples/
# run with --reload option
(env):/var/www/fprofile$ nohup uvicorn main:app --host 0.0.0.0 --port 8080 --reload > uvicorn.log 2>&1 &
# Or run without --reload option
(env):/var/www/fprofile$ nohup uvicorn main:app --host 0.0.0.0 --port 8080 > uvicorn.log 2>&1 &
run the nohup uvicorn main:app --host 0.0.0.0 and --port 8080 --reload > uvicorn.log 2>&1 & because we have set the proxy_pass in /etc/nginx/sites-available/default file
note: If you run uvicorn command without --reload then you can upload changes but your chage will not be reflect. Your project stable and you have not need to change then run uvicorn without --reload option
Now you can close your ssh or putty and check your public IP address your fastapi project is running
check running process
sudo netstat -plten |grep ':8080'
command: kill -9 [process ID (PID)]
kill -9 16085
$ tmux new -s [session_name]
$ tmux new -s tmux_fprofile
ubuntu@***:/var/www/fprofile$ source env/bin/activate
#run uvicorn with --reload option
(env):/var/www/fprofile$ uvicorn main:app --host 0.0.0.0 --port 8080 --reload > uvicorn.log 2>&1 &
#run uvicorn without --relaod option
(env):/var/www/fprofile$ uvicorn main:app --host 0.0.0.0 --port 8080 > uvicorn.log 2>&1 &
Now check browser using public IP address
Run the tmux ls command to show session_list
note: If you run uvicorn command with --reload then you can upload changes and your change will be reflect. Some time you have need to fix the bug
note: If you run uvicorn command without --reload then you can upload changes but your chage will not be reflect. Your project stable and you have not need to change then run uvicorn without --reload option
$ tmux ls
Detaching from a tmux session
Attach again from session
run the below command command: $ tmux a -t [session_name]
$ tmux a -t tmux_fprofile
You can type exit command to exit from tmux window and you come in ubunut terminal
$ exit
$ tmux ls
$ sudo netstat -plten
Now you can close your ssh or putty and check your public IP address your fastapi project is running
After some time letter like 3 month or 6 month and you not remember that you run uvicorn with tmux or nohup then and you want to stop the running of project then kill the process usig kill command. And you run again uvicorn command to run your applicatio.
set the alembic.ini file
sqlalchemy.url = postgresql://postgres:[password]@[endpoint]:5432/fprofile_db
sqlalchemy.url = postgresql://postgres:123456789@testpostresql.ctk6csmmwzzi.us-east-1.rds.amazonaws.com:5432/fprofile_db
(env) atul@atul-Lenovo-G570:~/fprofile$ pip3 install pillow
ref. https://www.geeksforgeeks.org/send-mail-gmail-account-using-python/
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
# start TLS for security
s.starttls()
# Authentication
s.login("smtp_authentication_email_id", "smtp_authentication_email_id_password")
# message to be sent
message = "Message_you_need_to_send"
# sending the mail
s.sendmail("sender_email_id", "receiver_email_id", message)
# terminating the session
s.quit()
https://sabuhish.github.io/fastapi-mail/getting-started/ https://pypi.org/project/fastapi-mail/
Comsysapp.com is an educational website. Students and software developers can learn programming language tutorials. Comsysapp is very useful for beginners and professional developers. Comsysapp provides tutorial in easy language. Comsysapp.com has focus on simplicity.
Comsysapp.com provides free tutorials like c, html, css, etc. All tutorials are free for beginner and professionals.
comsysapp.com is not responsible for any mistake. We are not responsible if information made available on our website is incomplete or invalid. But comsysapp.com always try for zero-zero mistake.
comsysapp.com does not collect any data from users. We use Google AdSense advertising on our website. We never ask personal or private information.
copyright © 2023