To deploy a Python Django project on Linux and use the venv virtual environment to start the project, you can follow the steps below:
-
First, make sure you have installed Python and virtualenv on your Linux system. If it is not installed, use a package manager (such as apt, yum, etc.) to install it.
-
Create a new virtual environment under the project root directory. Assuming your project root directory is
/path/to/project
, create a virtual environment with the following command:cd /path/to/project python3 -m venv venv
venv
This will create a virtual environment called at the root of the project . -
Activate the virtual environment. Activate the virtual environment with the following command:
source venv/bin/activate
Note: After activating the virtual environment, you will notice a change in the terminal prompt, appearing in the prefix
(venv)
. -
Install dependencies in the activated virtual environment. Execute the following commands in the virtual environment to install Django and other dependencies:
pip install django
You can
requirements.txt
list all dependencies in a file and install them with:
pip install -r requirements.txt
-
Perform database migrations. Go to the root directory of your Django project and execute the database migrations with the following command:
python manage.py migrate
-
Start the Django project. In the virtual environment, start the Django project with the following command:
python manage.py runserver
By default, Django will start the project locally on
127.0.0.1
port8000
. You canhttp://127.0.0.1:8000/
view the project in your browser by visiting .
Note that the virtual environment will need to be reactivated every time you open a new terminal or log back in to use it. If you want to stop using the virtual environment, you can exit the virtual environment with the following command:
deactivate