2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
"Girl, it depends on luck if we meet again. Don't pretend you don't know each other, and don't be afraid to say "long time no see"..."
To deploy a backend API written in Python to a Linux environment, you can follow the detailed steps below. This article will cover environment preparation, API writing, using Gunicorn as a WSGI server, configuring Nginx as a reverse proxy, and using Systemd to manage services.
If Python is not installed on your Linux system, you can install it using the following command:
sudo yum update
sudo yum install python3 python3-pip
Assuming you are using Flask or Django framework, install the required libraries first:
pip3 install flask # 如果你使用的是 Flask
pip3 install django # 如果你使用的是 Django
Create a simple Flask API example:
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/api', methods=['GET'])
def api():
return jsonify({'message': 'Hello, World!'})
if __name__ == '__main__':
app.run(host='0.0.0.0')
Create a new Django project and add the API:
django-admin startproject myproject
cd myproject
django-admin startapp myapp
exist myapp/views.py
Add the API view:
from django.http import JsonResponse
def api(request):
return JsonResponse({'message': 'Hello, World!'})
exist myproject/urls.py
Configure URL routing in:
from django.contrib import admin
from django.urls import path
from myapp.views import api
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', api),
]
To quickly start an application in a development or test environment, you can run:
python main.py 或者 nohup python main.py
Django has its own development server, which you can start with the following command:
python manage.py runserver 0.0.0.0:8000
Gunicorn is recommended for production environments. To ensure the performance and stability of your application in a production environment, it is recommended that you use Gunicorn.Gunicorn As an application server, it handles the execution of Python code and multi-process management.
Install Gunicorn:
pip3 install gunicorn
Run the Flask application using Gunicorn:
gunicorn --bind 0.0.0.0:8000 app:app
Run the Django application using Gunicorn:
gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
Install Nginx:
sudo yum install nginx
Editing the Configuration File /etc/nginx/sites-available/default
:
server {
listen 80;
server_name your_domain_or_IP;
location / {
proxy_pass http://127.0.0.1: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;
}
}
Reload the Nginx configuration:
sudo systemctl restart nginx
Create a new Systemd service file:
sudo vim /etc/systemd/system/myproject.service
[Unit]
Description=Gunicorn instance to serve myproject
After=network.target
[Service]
User=your_user
Group=www-data
WorkingDirectory=/path/to/your/project
ExecStart=/usr/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 app:app
[Install]
WantedBy=multi-user.target
Will ExecStart
Change the line to:
ExecStart=/usr/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 myproject.wsgi:application
Start and enable the service:
sudo systemctl start myproject
sudo systemctl enable myproject
Make sure the service is running:
sudo systemctl status myproject
In this way, your Python API is successfully deployed to the Linux environment. You can adjust the configuration as needed to suit your specific project requirements. I hope this detailed guide is helpful to you!