Deployment Overview of 3X-UI on Server¶
Prerequisites and Basic Requirements¶
The deployment requires a Linux server running either Ubuntu, Debian, or a RHEL-based distribution (such as CentOS or Rocky Linux). The system must have root privileges to install Docker and configure the network. The following ports must be accessible through the firewall:
- Port
22/tcpfor SSH access. - Port
80/tcpfor HTTP traffic and SSL certificate validation. - Port
443/tcpfor HTTPS traffic. - A custom internal port (defined as
{{ internal_port }}) for the application backend.
File and Directory Structure¶
The application and its dependencies utilize the following directory structure on the host server:
/root/3x-ui/: Contains the Docker Compose configuration file (compose.yml)./data/3x-ui/db/: Stores the application database files./data/3x-ui/cert/: Reserved for certificate storage (mapped via Docker volume)./data/nginx/: Contains Nginx configuration files and environment variables./data/nginx/user_conf.d/: Stores custom Nginx server block configurations./data/nginx/nginx-certbot.env: Environment file for the Nginx and Certbot container./etc/docker/daemon.json: Docker daemon configuration file.
Application Installation Process¶
The application is deployed using Docker Compose. The installation process involves setting up the Docker Engine, creating the necessary directory structure, and launching the containers.
-
Docker Engine Installation:
- For Debian/Ubuntu systems, the Docker CE repository is added, and the following packages are installed:
docker-ce,docker-ce-cli,containerd.io,docker-buildx-plugin,docker-compose-plugin,net-tools, anddnsutils. - For RHEL-based systems, the Docker CE repository is added, and the following packages are installed:
docker-ce,docker-ce-cli,containerd.io,docker-buildx-plugin,docker-compose-plugin,net-tools, andbind-utils. - The Docker daemon is configured via
/etc/docker/daemon.jsonand started as a systemd service.
- For Debian/Ubuntu systems, the Docker CE repository is added, and the following packages are installed:
-
Directory Creation:
- The directories
/data,/root/3x-ui,/data/3x-ui,/data/3x-ui/db,/data/3x-ui/cert,/data/nginx/, and/data/nginx/user_conf.dare created withrootownership.
- The directories
-
Container Deployment:
- A Docker Compose file is generated at
/root/3x-ui/compose.yml. - The application is started using the command
docker compose up -dexecuted from the/root/3x-uidirectory.
- A Docker Compose file is generated at
Access Rights and Security¶
Firewall rules are configured to allow necessary traffic while blocking unauthorized access.
- UFW Configuration:
- SSH (
22/tcp) is allowed. - HTTP (
80/tcp) is allowed. - HTTPS (
443/tcp) is allowed.
- SSH (
- Firewalld Configuration:
- The
firewalldservice is disabled and stopped on systems where it is present.
- The
- Application Access Control:
- The Nginx configuration enforces strict host validation. Requests not matching the specific domain
{{ prefix }}{{ server_id }}.{{ zone }}are rejected with a444status code. - Default administrative credentials are set during the initial deployment:
- Username:
admin - Username:
root
- Username:
- The application listens on the internal port
{{ internal_port }}and is exposed externally via the Nginx reverse proxy on port443.
- The Nginx configuration enforces strict host validation. Requests not matching the specific domain
Databases¶
The application uses a local file-based database stored within the Docker container.
- Storage Location: The database files are persisted in the host directory
/data/3x-ui/db/. - Mount Point: This directory is mounted to
/etc/x-ui/inside the3x-uicontainer. - Configuration: No external database connection strings are required; the application manages its data internally within the mounted volume.
Docker Containers and Their Deployment¶
The deployment consists of two primary containers managed by Docker Compose:
-
3x-ui Container:
- Image:
ghcr.io/mhsanaei/3x-ui:latest - Container Name:
3x-ui - Hostname:
3x-ui - Volumes:
/data/3x-ui/db/mounted to/etc/x-ui/nginx_secretsmounted to/root/cert/
- Environment:
XRAY_VMESS_AEAD_FORCEDis set tofalse. - Ports: Exposes
{{ internal_port }}internally; port443is mapped in the compose file but handled by the Nginx container. - Restart Policy:
unless-stopped.
- Image:
-
Nginx Container:
- Image:
jonasal/nginx-certbot:latest - Container Name:
nginx-certbot - Environment:
CERTBOT_EMAILis set to[email protected].- Additional variables are loaded from
/data/nginx/nginx-certbot.env.
- Volumes:
nginx_secretsmounted to/etc/letsencrypt./data/nginx/user_conf.dmounted to/etc/nginx/user_conf.d.
- Ports:
80:80{{ internal_port }}:{{ internal_port }}
- Dependencies: Depends on the
3x-uicontainer. - Restart Policy:
unless-stopped.
- Image:
-
Docker Volumes:
- A named volume
nginx_secretsis created externally to store SSL certificates and secrets shared between the Nginx and 3x-ui containers.
- A named volume
Proxy Servers¶
Nginx acts as a reverse proxy and SSL terminator for the 3x-ui application.
- Configuration Location: Custom server blocks are stored in
/data/nginx/user_conf.d/with the filename format{{ prefix }}{{ server_id }}.{{ zone }}.conf. - SSL Configuration:
- Certificates are managed by the
nginx-certbotcontainer and stored in thenginx_secretsvolume. - The Nginx configuration references certificates at
/etc/letsencrypt/live/{{ prefix }}{{ server_id }}.{{ zone }}/. - SSL parameters include
ssl_certificate,ssl_certificate_key,ssl_trusted_certificate, andssl_dhparam.
- Certificates are managed by the
- Proxy Settings:
- Nginx listens on
{{ internal_port }}with SSL and HTTP/2 enabled. - Traffic is proxied to the
3x-uicontainer athttp://3x-ui:{{ internal_port }}. - Headers
Host,X-Real-IP,X-Forwarded-For,Upgrade, andConnectionare forwarded to the backend. - Proxy buffering is disabled (
proxy_buffering off). - Timeouts are set to
5sfor connection and60sfor read operations.
- Nginx listens on
Permission Settings¶
File and directory permissions are set during the deployment process to ensure secure access:
- Directories:
/data,/root/3x-ui,/data/3x-ui,/data/3x-ui/db,/data/3x-ui/cert,/data/nginx/, and/data/nginx/user_conf.dare owned byroot:rootwith mode0640.
- Configuration Files:
/root/3x-ui/compose.ymlis owned byroot:rootwith mode0644./data/nginx/user_conf.d/{{ prefix }}{{ server_id }}.{{ zone }}.confis owned byroot:rootwith mode0644./data/nginx/nginx-certbot.envis owned byroot:rootwith mode0644./etc/docker/daemon.jsonis owned byroot:rootwith mode0644.
Starting, Stopping, and Updating¶
The application is managed via Docker Compose and Docker commands.
- Starting the Application:
- Execute
docker compose up -dfrom the/root/3x-uidirectory.
- Execute
- Restarting the Application:
- Execute
docker restart 3x-uito restart the main application container.
- Execute
- Updating the Application:
- Pull the latest image using
docker pull ghcr.io/mhsanaei/3x-ui:latest. - Recreate the containers using
docker compose up -d.
- Pull the latest image using
- Service Management:
- The Docker daemon service is managed via
systemctl(e.g.,systemctl status docker). - The application containers are managed via
docker composecommands.
- The Docker daemon service is managed via