Reflex is a Python framework for building full-stack web applications. It lets developers create interactive web applications using Python while Reflex handles the frontend and backend infrastructure.
This tutorial demonstrates three methods for deploying a Reflex app on an Ubuntu server.

Prerequisites
- A server running Ubuntu.
- SSH access to the server.
- A user account with sudo privileges.
Choosing a Deployment Method
Deploy a Reflex application on an Ubuntu server using different process management and deployment tools, depending on your needs and preferences:
- PM2 and Nginx. Use PM2 to manage the Reflex application process and Nginx as a reverse proxy.
- Docker and Docker Compose. Package the application and its dependencies into a Docker image and manage the container with Docker Compose.
- systemd. Run Reflex as a native Linux service and use systemd to manage its process.
Each method uses an independent deployment setup. You can follow any method without completing the previous ones.
Note: All three methods use HTTP for demonstration purposes. For a public production deployment, configure a domain name and HTTPS.
Option 1: Deploy Reflex Using PM2 and Nginx
This method deploys a Reflex application directly on the Ubuntu server. PM2 manages the application process, while Nginx acts as a reverse proxy and forwards HTTP and WebSocket traffic to Reflex.
The Reflex application listens internally on port 3000. Nginx listens on port 80 and forwards incoming requests to the application. This setup lets users access the application through the server's public IP address without specifying the Reflex port.
Step 1: Install Node.js
PM2 is a process manager for Node.js applications, but it can also manage other processes, including Python applications. It requires Node.js and npm to run.
Install Node.js from the NodeSource repository. Use the curl command to add the repository for the latest LTS release:
# curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
Install Node.js and npm:
sudo apt install -y nodejs
Verify that both tools are available:
node --version
npm --version

The version commands should display the installed Node.js and npm versions.
Step 2: Install uv
Reflex recommends uv as its default Python package and project manager. It provides Python version management, dependency installation, and virtual environment management in one tool.
Unlike managing a virtual environment and installing packages separately with venv and pip, uv handles these tasks through a unified workflow. It also creates a project-local .venv directory automatically when you add dependencies.
Install uv using the official installer:
# curl -LsSf https://astral.sh/uv/install.sh | sh

Reload the shell configuration so the current session can find the newly installed command:
source ~/.bashrc
Verify the installation:
uv --version

Step 3: Create the Reflex application
Before configuring the deployment tools, create and initialize the Reflex project. This tutorial uses Python 3.12 and Reflex 0.11.0 to keep the environment consistent across all three methods.
Create a directory for the project and navigate into it:
mkdir ~/reflex_demo
cd ~/reflex_demo
Check the current directory with pwd:
pwd
Initialize a Python project using Python 3.12:
uv init --python 3.12

Install Reflex as a project dependency:
uv add "reflex==0.11.0"
Verify the installed Reflex version:
uv run reflex --version

Initialize a blank Reflex application:
uv run reflex init --template blank

The command creates the application structure and its required configuration files. The blank template provides a minimal starting point without additional example components.
Verify that the project files were created:
ls -la
Step 4: Test the application
Before configuring PM2, verify that Reflex starts successfully in production mode. This helps identify application or dependency issues before introducing the process manager.
Run the application:
.venv/bin/reflex run --env prod

The command starts the Reflex application using the executable in the project's virtual environment.
Open another SSH session and send a request to the local health-check endpoint:
# curl http://127.0.0.1:3000/ping
The expected response is pong:

This confirms that the Reflex backend responds on port 3000.
After confirming that the application starts, stop it with Ctrl+C before configuring PM2.
Step 5: Install PM2
PM2 runs the application as a managed background process. It can monitor the process, restart it after failures, and restore saved processes when the server starts.
Install PM2 globally using npm:
sudo npm install -g pm2

Verify the installation:
pm2 --version
Retrieve the server's public IP address with:
# curl -4 ifconfig.me
Copy the address as it will be needed to configure the public API URL in the next step.
Step 6: Configure PM2
PM2 uses an ecosystem configuration file to define application settings, startup commands, environment variables, and process management options.
Create the configuration file in the Reflex project directory:
nano ecosystem.config.js
Add the following configuration:
module.exports = {
apps: [
{
name: "reflex-demo",
script: "bash",
args: [
"-c",
"unset NODE_CHANNEL_FD NODE_CHANNEL_SERIALIZATION_MODE; exec .venv/bin/reflex run --env prod < /dev/null >> reflex.log 2>&1"
],
cwd: __dirname,
interpreter: "none",
autorestart: true,
watch: false,
max_restarts: 10,
restart_delay: 5000,
env: {
REFLEX_API_URL: http://SERVER_IP
}
}
]
};
Replace SERVER_IP with the server's public IP address. Do not include port 3000.
The configuration defines the following settings:
name: Assigns a name to the PM2 process.scriptandargs: Use a Bash wrapper to start Reflex.cwd: Sets the project directory as the working directory.interpreter: Tells PM2 to execute the Bash command directly.autorestart: Enables automatic process restarts.max_restarts: Limits the number of consecutive restart attempts.restart_delay: Adds a delay between restart attempts.REFLEX_API_URL: Defines the public address that the Reflex frontend uses to communicate with its backend.
The Bash wrapper unsets NODE_CHANNEL_FD and NODE_CHANNEL_SERIALIZATION_MODE before starting Reflex. It also redirects application output to reflex.log.
The REFLEX_API_URL setting is particularly important when using Nginx as a reverse proxy. It tells the browser to send backend and WebSocket requests through the public server address instead of connecting directly to port 3000. Nginx then forwards those requests to the local Reflex process.
Start the application through PM2:
pm2 start ecosystem.config.js
Check the process status:
pm2 status

Inspect the application logs for any startup errors:
tail -n 30 reflex.log

PM2 can also restore managed processes after a server restart. Generate the startup command:
pm2 startup

PM2 displays an additional command. Run the exact command generated for your system to configure the startup integration.
Save the current process list:
pm2 save
PM2 uses this saved list to restore the managed applications during system startup.
Step 7: Install and configure Nginx
Nginx acts as a reverse proxy between external clients and the Reflex application. Instead of exposing the application's internal port directly, Nginx accepts incoming HTTP requests on port 80 and forwards them to port 3000.
The configuration must also support WebSocket connections because Reflex uses them for communication between the browser and backend.
Install Nginx:
sudo apt install -y nginx
Enable the service and start it immediately:
sudo systemctl enable --now nginx
Check its status:
sudo systemctl status nginx

Create a dedicated Nginx configuration file for the Reflex application:
sudo nano /etc/nginx/sites-available/reflex
Add the following configuration:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
server_name _;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $http_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;
proxy_read_timeout 86400;
}
}
The configuration includes several important settings:
listen 80: Makes Nginx listen for incoming HTTP requests on port 80.proxy_pass: Forwards requests to the Reflex application on the local port 3000.proxy_http_version 1.1: Uses HTTP/1.1 for the proxied connection.UpgradeandConnection: Support WebSocket upgrades.proxy_set_header: Preserves information about the original client request.proxy_read_timeout: Allows long-lived connections without prematurely closing them.
Enable the configuration by creating a symbolic link:
sudo ln -sfn /etc/nginx/sites-available/reflex /etc/nginx/sites-enabled/reflex
Remove the default Nginx site configuration to prevent it from conflicting with the Reflex server configuration:
sudo rm -f /etc/nginx/sites-enabled/default
Test the configuration for syntax errors:
sudo nginx -t

If the test succeeds, reload Nginx to apply the changes:
sudo systemctl reload nginx
Step 8: Configure the firewall
The application is accessible through Nginx on port 80, so allow inbound HTTP traffic. Keep SSH access enabled to avoid losing remote access when activating the firewall.
Allow SSH connections:
sudo ufw allow OpenSSH
Allow HTTP traffic:
sudo ufw allow 80/tcp
Enable the firewall:
sudo ufw enable
Check the firewall status and verify that the rules are active:
sudo ufw status
Also, ensure that the BMC firewall configuration allows inbound TCP traffic on port 80.
Step 9: Verify the deployment
After configuring PM2 and Nginx, verify that the reverse proxy forwards requests to Reflex.
Send a request to the local Nginx endpoint:
# curl http://127.0.0.1/ping
The expected response is pong.
Open the following address in a browser:
http://SERVER_IP
Replace SERVER_IP with the server's public IP address and press Enter. The Reflex starter application should load. Verify that its interactive functionality works.

Option 2: Deploy Reflex Using Docker and Docker Compose
This method packages the Reflex application and its dependencies into a Docker image. Docker Compose builds and runs the container, which includes the services required to serve the application.
The deployment uses a multi-stage Dockerfile. Bun builds the frontend, Caddy serves the frontend and proxies backend requests, and Redis provides state management.
Docker Compose maps host port 80 to container port 8080. This lets users access the application through the server's public IP address without specifying the container port.
Step 1: Verify Docker is installed
Docker Engine provides the container runtime, while Docker Compose defines and manages multi-container applications. This deployment uses both tools to build and run the Reflex application.
Check whether Docker Engine is installed:
docker --version
Verify that the Docker Compose plugin is available:
docker compose version

If Docker is not installed, follow the Docker installation guide before continuing.
Step 2: Install uv and Python
The Docker deployment still uses uv on the host to initialize the Reflex project and manage its Python dependencies. The Docker image has its own build environment, so installing uv on the host does not replace the tools included in the image.
Reflex recommends uv as its default Python package and project manager. It simplifies project initialization, dependency installation, and virtual environment management.
Install uv:
# curl -LsSf https://astral.sh/uv/install.sh | sh
Reload the shell configuration:
source ~/.bashrc
uv python install 3.12
Step 3: Create the Reflex application
Create a directory for the application and initialize a Python project. The tutorial uses Reflex 0.11.0 and Python 3.12 to keep the application version consistent with the other deployment methods.
Create and enter the project directory:
mkdir ~/reflex_demo
cd ~/reflex_demo
Initialize the project:
uv init --python 3.12
Install Reflex:
uv add "reflex==0.11.0"
Verify the installed version:
uv run reflex --version
If prompted, install unzip, which the Reflex initialization process requires when extracting template files:
sudo apt install unzip
Initialize the application:
uv run reflex init

Select the blank template (0) when prompted.
Verify the generated project files:
ls -la
Step 4: Prepare the Docker configuration
Docker uses a Dockerfile to define how to build an image. The image contains the application, its dependencies, and the runtime components required to start it.
This deployment uses a multi-stage build to separate frontend compilation from the final runtime image. The builder stage installs the Python dependencies and builds the static frontend. The runtime stage contains only the application files and the services needed to run it.
Before creating the Dockerfile, add a requirements file that specifies the Reflex version:
printf 'reflex==0.11.0\n' > requirements.txt
Create the Dockerfile
Create the Dockerfile with:
nano Dockerfile
Add the following configuration:
# Single-container production deployment for Reflex.
ARG PORT=8080
ARG API_URL
# Build stage
FROM python:3.13-slim AS builder
# Install uv and Bun
COPY --from=ghcr.io/astral-sh/uv:0.12 /uv /bin/uv
COPY --from=oven/bun:1 /usr/local/bin/bun /usr/local/bin/bun
ENV UV_COMPILE_BYTECODE=1
ENV UV_NO_CACHE=1
ENV PATH="/app/.venv/bin:$PATH"
WORKDIR /app
# Install Python dependencies
COPY requirements.txt .
RUN uv venv && uv pip install -r requirements.txt
# Copy the application source
COPY . .
# Build the static frontend
ARG PORT
ARG API_URL
RUN --mount=type=cache,target=/root/.bun/install/cache \
REFLEX_API_URL=${API_URL:-http://localhost:$PORT} reflex export --frontend-only --no-zip
# Runtime stage
FROM python:3.13-slim
# Install Redis
RUN apt-get update -y && \
apt-get install -y --no-install-recommends redis-server && \
rm -rf /var/lib/apt/lists/*
# Install Caddy
COPY --from=caddy:2 /usr/bin/caddy /usr/bin/caddy
ARG PORT
ENV PATH="/app/.venv/bin:$PATH"
ENV PORT=$PORT
ENV REFLEX_REDIS_URL=redis://localhost
ENV PYTHONUNBUFFERED=1
WORKDIR /app
# Create a non-root application user
RUN adduser --disabled-password --gecos "" --home /app reflex && \
chown reflex /app
# Copy application files from the build stage
COPY --chown=reflex --from=builder /app/.venv .venv
COPY --chown=reflex --from=builder /app/.web/backend .web/backend
COPY --from=builder /app/.web/build/client /srv
COPY --chown=reflex . .
USER reflex
EXPOSE $PORT
# Start the application services
CMD if [ -d alembic ]; then reflex db migrate; fi && \
caddy start && \
redis-server --daemonize yes && \
exec reflex run --env prod --backend-only
The Dockerfile uses the following components:
Python 3.13 slim: Provides the base image for both build and runtime stages.uv: Creates the virtual environment and installs the Python dependencies.Bun: Handles the frontend build process.Caddy: Serves the compiled frontend and forwards backend requests.Redis: Provides the state management service required by the Reflex application.
The builder stage exports the frontend using reflex export --frontend-only --no-zip. The runtime stage copies the compiled frontend and backend files into the final image.
The application runs under a dedicated non-root user named reflex.
Create the Caddyfile
Caddy acts as the web server and reverse proxy inside the container. It serves the static frontend files and forwards requests that require backend processing to the Reflex server.
Create the Caddy configuration:
nano Caddyfile
Add the following configuration:
:{$PORT}
encode gzip
@backend_routes path /_event/* /ping /_health /_upload /_upload/*
handle @backend_routes {
reverse_proxy localhost:8000
}
root * /srv
route {
try_files {path} {path}/ /404.html
file_server {
precompressed br zstd gzip
}
}
The configuration listens on the port specified by the PORT environment variable. It forwards Reflex backend routes to port 8000 and serves the compiled frontend files from /srv.
The encode gzip directive enables response compression, while try_files handles frontend routes by serving the appropriate file or falling back to the 404 page.
Create the .dockerignore file
Docker sends the project directory to the Docker daemon as the build context. Including unnecessary files can increase the build context size and copy local development data into the build process.
Create a .dockerignore file to exclude files that the Docker image does not need:
nano .dockerignore
Add the following entries:
.venv
.git
.web
.states
__pycache__
*.pyc
*.pyo
node_modules
.env
*.log
.DS_Store
The file excludes the local virtual environment, Git metadata, frontend build artifacts, cache files, environment files, and logs.
Step 5: Configure Docker Compose
Docker Compose uses a YAML file to define the application's image, container, build arguments, port mappings, environment variables, and restart policy.
The configuration maps host port 80 to container port 8080. It also passes the server's public IP address to the Docker build as the API URL.
Retrieve the public IP address:
# curl -4 ifconfig.me
Create the Compose file:
nano compose.yaml
Add the following configuration:
services:
reflex:
build:
context: .
dockerfile: Dockerfile
args:
PORT: "8080"
API_URL: http://SERVER_IP
image: reflex-demo:latest
container_name: reflex-demo
ports:
- "80:8080"
environment:
PORT: "8080"
restart: unless-stopped
Replace SERVER_IP with the server's public IP address you obtained previously. Do not include a port number.
The main configuration settings include:
build: Defines the Docker build context and Dockerfile.args: Passes the port and public API URL to the build process.image: Assigns a name and tag to the Docker image.container_name: Sets a fixed name for the container.ports: Maps host port 80 to container port 8080.environment: Sets the container's runtime port.restart: Configures Docker to restart the container unless it was explicitly stopped.
The API_URL build argument is important because Reflex uses it when compiling the frontend. It ensures that browser requests target the public server address rather than an internal container address.
Step 6: Build and deploy the application
After creating the Docker configuration files, build the image. Docker reads the Dockerfile, installs the dependencies, compiles the frontend, and prepares the runtime image.
Build the image:
docker compose build

List the available Docker images:
docker images

The output should include reflex-demo:latest.
Start the application in detached mode:
docker compose up -d

The -d option runs the container in the background and returns control to the terminal.
Step 7: Verify the deployment
After starting the container, verify that Docker Compose reports it as running and that the application responds through the exposed HTTP port.
Check the container status:
docker compose ps

Send an HTTP request to the local server:
# curl -I http://127.0.0.1
The command displays the HTTP response headers. A successful response confirms that the web server responds to requests.
Check the Reflex backend health endpoint:
# curl http://127.0.0.1/ping
The expected response is pong.
Open the server address in a browser:
http://SERVER_IP
Replace SERVER_IP with your server's public IP address.
The Reflex starter application should load. Verify that its interactive functionality works.
Option 3: Deploy Reflex Using systemd
This method deploys Reflex directly on the Ubuntu server without PM2, Nginx, or Docker.
Instead, systemd manages the application as a Linux service. It starts Reflex in production mode, monitors the process, and can restart it if it exits unexpectedly.
The application is exposed directly on port 3000. Users access it through the server's public IP address and application port.
Step 1: Install uv and Python
Reflex recommends uv as its default Python package and project manager. It simplifies Python version management, dependency installation, and virtual environment creation.
In this deployment, uv creates the project's .venv directory and installs Reflex and its dependencies there. The systemd service later runs the Reflex executable directly from this environment.
Install uv:
# curl -LsSf https://astral.sh/uv/install.sh | sh
Reload the shell configuration:
source ~/.bashrc
Install Python 3.12:
uv python install 3.12
Verify the Python installation and display its location:
uv python find 3.12

Step 2: Create the Reflex application
Before configuring systemd, create the Reflex project and install its dependencies. The application runs from its project directory, so the service configuration must reference the same location.
Create and enter the project directory:
mkdir ~/reflex_demo
cd ~/reflex_demo
Initialize the Python project:
uv init --python 3.12
Install Reflex 0.11.0:
uv add "reflex==0.11.0"
Verify the installed version:
uv run reflex --version
Initialize the application:
uv run reflex init

Select the blank template (0) when prompted.
Step 3: Test the application
Before creating the systemd service, confirm that Reflex starts successfully in production mode. This verifies that the application and its dependencies work independently of the service manager.
Start Reflex:
uv run reflex run --env prod

Open another SSH session and check the local health endpoint:
# curl http://127.0.0.1:3000/ping
The expected response is pong.
The response confirms that the Reflex backend is running and responding on port 3000.
Stop the application with Ctrl+C before proceeding.
Step 4: Configure the firewall
This deployment exposes the Reflex application directly on port 3000, rather than using Nginx or another reverse proxy.
Allow SSH connections before enabling the firewall:
sudo ufw allow OpenSSH
Allow incoming TCP traffic on port 3000:
sudo ufw allow 3000/tcp
Enable the firewall:
sudo ufw enable
Check the firewall status:
sudo ufw status

Confirm that the SSH and port 3000 rules are active. If the firewall remains inactive, enable it before continuing.
Also, allow inbound TCP traffic on port 3000 in the BMC firewall configuration. Both the server firewall and the BMC firewall must permit the application traffic.
Step 5: Create the systemd service
systemd is the service manager used by Ubuntu to start and manage system services. It can launch applications during system startup, monitor their processes, and restart them according to the configured policy.
Create a dedicated systemd unit file for the Reflex application:
sudo nano /etc/systemd/system/reflex-demo.service
Add the following configuration:
[Unit]
Description=Reflex Demo Application
After=network.target
[Service]
Type=simple
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/reflex_demo
Environment="HOME=/home/ubuntu"
Environment="PATH=/home/ubuntu/reflex_demo/.venv/bin:/home/ubuntu/.local/bin:/home/ubuntu/.local/share/reflex/bun/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Environment="PYTHONUNBUFFERED=1"
ExecStart=/home/ubuntu/reflex_demo/.venv/bin/reflex run --env prod
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
The service configuration defines how systemd runs the Reflex application.
The main settings include:
Description: Provides a short description of the service.After=network.target: Orders the service after the network target.Type=simple: Defines the service as a process that runs directly.UserandGroup: Run the application under the ubuntu account.WorkingDirectory: Sets the Reflex project directory.Environment: Defines the home directory, executable search path, and Python output behavior.ExecStart: Runs Reflex in production mode using the executable inside the project's virtual environment.Restart=on-failure: Restarts the application if the process exits with an error.RestartSec=5: Waits five seconds before attempting to restart the application.WantedBy=multi-user.target: Allows the service to start during normal system startup when enabled.
The ExecStart directive uses the absolute path to the Reflex executable. This allows systemd to start the application without relying on the user's interactive shell environment.
Step 6: Enable and start the service
After creating the unit file, reload systemd so it recognizes the new service definition.
Run:
sudo systemctl daemon-reload
Enable the service and start it immediately:
sudo systemctl enable --now reflex-demo

The enable option configures the service to start automatically at boot. The --now option starts it immediately without requiring a separate start command.
Check the service status:
sudo systemctl status reflex-demo
The output should show active (running):

This confirms that systemd successfully started the Reflex application.
Step 7: Verify the deployment
After starting the systemd service, verify that the Reflex backend responds locally.
Run:
# curl http://127.0.0.1:3000/ping
The expected response is pong.
Open the following address in a browser:
http://SERVER_IP:3000
Replace SERVER_IP with the server's public IP address.
The Reflex starter application should load and work.
Conclusion
This tutorial demonstrated three methods for deploying a Reflex application on an Ubuntu server. Each method provides a different approach to application deployment and process management. Choose the one that best fits your infrastructure and operational requirements.
Next, see how to deploy Next.js on Linux.



