https://github.com/novasush/fastapi-nginx-unit
Deploying an Asynchronous FastAPI on NGINX Unit
https://github.com/novasush/fastapi-nginx-unit
docker docker-compose fastapi nginx-unit openapi python python3
Last synced: 11 months ago
JSON representation
Deploying an Asynchronous FastAPI on NGINX Unit
- Host: GitHub
- URL: https://github.com/novasush/fastapi-nginx-unit
- Owner: novasush
- License: mit
- Created: 2021-03-06T10:25:52.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-03-14T10:14:23.000Z (about 5 years ago)
- Last Synced: 2025-04-14T17:07:39.362Z (11 months ago)
- Topics: docker, docker-compose, fastapi, nginx-unit, openapi, python, python3
- Language: Python
- Homepage:
- Size: 3.91 KB
- Stars: 11
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fastapi-nginx-unit
A simple 'hello world' app of FastAPI running on NGINX Unit App server using ASGI.
## Running on Docker
```bash
docker-compose up --build
```
After that you can test using `curl` command.
```bash
curl http://localhost
```
## Running test cases (docker)
```bash
# Run the docker server if you haven't started it yet then run below command for executing test cases
docker-compose exec api pytest /fastapi
```
## Running from terminal
1. Create venv in your project directory and install fastapi
```bash
$ python3 -m venv apienv
$ source apienv/bin/activate
(venv)$ pip install --upgrade pip
(venv)$ pip install -r requirements.txt
$ deactivate
```
2. Run the following command for each directory containing app code or data so Unit can access it:
```bash
(venv)$ sudo chown -R unit:unit /path/to/app/
```
3. Create a JSON configuration file containing information about application.
```json
// config.json
{
"listeners": {
"*:80": {
"pass": "applications/fastapi"
}
},
"applications": {
"fastapi": {
"type": "python 3.x",
"path": "/path/to/app/",
"home": "/path/to/app/venv/",
"module": "asgi",
"callable": "app"
}
}
}
```
4. Run the server
```bash
(venv)$ unitd
```
5. Push the configurations json file on server
```bash
(venv)$ sudo curl -X PUT --data-binary @config.json --unix-socket \
/path/to/control.unit.sock http://localhost/config/
```
6. Testing (terminal)
```bash
(venv)$ pytest .
```