Deploying your Django project to an Ubuntu server is an exciting milestone. But once it’s live, how do you keep coding, tweaking, and maintaining it? That’s where the Remote — SSH extension in Visual Studio Code (VS Code) comes in. It lets you work on your server as if the files were local, bringing all the power of VS Code to your remote environment. Here’s a step-by-step guide to setting it up and making the most of it.
Why Use Remote — SSH?
First, let’s understand why Remote — SSH is so handy. When your Django project is on a remote server, you might think you need to SSH into the server, open Vim or Nano, and start editing. While this works, it’s not as efficient as using a full-fledged IDE like VS Code. Remote — SSH lets you connect your local VS Code to your server. This means you can navigate files, use autocomplete, linting, version control, and even run your Django project from within VS Code. No more jumping between the terminal and your editor.
Step 1: Install the Remote — SSH Extension
Start by installing the Remote — SSH extension in VS Code. Open VS Code, and navigate to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window. Search for “Remote — SSH” and click Install.
Once installed, the extension adds a green button to the lower-left corner of VS Code. This button will be your gateway to remote development.
Step 2: Set Up SSH Access
Before connecting VS Code to your server, ensure you have SSH access to your Ubuntu server. Open a terminal on your local machine and test the connection with:
ssh your_username@your_server_ip
If this command logs you into the server, you’re good to go. If not, you’ll need to configure your SSH keys or troubleshoot any connection issues. You might prefer to use an SSH key instead of a password for enhanced security and convenience. You can generate an SSH key on your local machine using:
ssh-keygen -t rsa -b 4096
Then, copy the public key to your server:
ssh-copy-id your_username@your_server_ip
Now, you should be able to connect without entering your password each time.
Step 3: Connect VS Code to Your Server
With the Remote — SSH extension installed and SSH access configured, it’s time to connect. Click on the green button in the lower-left corner of VS Code. You’ll see an option to “Connect to Host…”. Select this, and you’ll be prompted to enter your SSH connection details, typically something like:
your_username@your_server_ip
VS Code will then open a new window connected to your server. This might take a few moments as VS Code sets up the remote environment.
Step 4: Open Your Django Project
Once connected, you’ll see a new VS Code window. This window is running directly on your server. To open your Django project, click on the Explorer icon in the Activity Bar, and navigate to your project directory. It will feel just like working on a local project. All your files and folders are there, ready to be edited.
Step 5: Start Coding
Now you can start coding as you would locally. Need to tweak a Django view or update a template? Just open the file and start editing. The best part is that you have access to all the features of VS Code. This includes Git integration, extensions, and terminal access. You can even open a terminal in VS Code to run Django management commands or check server logs. It’s like your server is right there with you.
Step 6: Run Your Django Project
Want to test your changes? Open a terminal in VS Code, navigate to your project’s directory, and run your Django development server:
python manage.py runserver 0.0.0.0:8000
Visit your server’s IP address in a browser to see the changes live. This workflow allows you to test updates in a controlled environment before pushing them to production.
Step 7: Disconnect When Done
When you’re finished working, you can disconnect from the server. Simply close the VS Code window, or click on the green button again and choose “Close Remote Connection.” Your changes will remain on the server, and you can reconnect at any time.
What about ‘runserver’?
When you use Django’s runserver
locally, it serves your application on your machine’s localhost and automatically reloads whenever you make changes to your code. When you switch to using Remote – SSH with VS Code to manage your deployed Django project on an Ubuntu server, you might wonder if this convenient behavior persists. The short answer is yes—but with some nuances. Let’s dive into how runserver
works in a Remote – SSH setup and what you can expect.
Understanding Remote — SSH
Remote — SSH allows you to connect your local VS Code to a remote server. This means you’re editing files directly on the server, but using the familiar VS Code interface on your local machine. All operations, including running commands, are executed on the remote server. This setup bridges the gap between local development and remote deployment, providing a seamless workflow.
Running runserver
Remotely
When you execute the runserver
command in a Remote – SSH session, here’s what happens:
- Execution on the Server: The
runserver
command runs on the Ubuntu server, not on your local machine. This means the Django development server is active on the server’s environment. - Local Access to the Server: To access the Django application, you typically need to forward the server’s port to your local machine. However, with Remote — SSH, you can use VS Code’s integrated terminal to manage and view your application directly.
- Auto-reloading: Just like locally,
runserver
watches for changes in your project files on the server. When you edit a file through VS Code, the changes are saved directly on the server. Therunserver
process detects these changes and automatically reloads the server, reflecting your updates in real-time.
Step-by-Step: Using runserver
with Remote – SSH
Here’s how you can set this up and ensure runserver
behaves as expected:
Step 1: Connect to Your Server
- Open VS Code: Launch VS Code on your local machine.
- Connect via Remote — SSH: Click the green Remote indicator in the lower-left corner and select your configured SSH host. VS Code connects to your Ubuntu server, and you’ll see the server’s file system in the Explorer pane.
Step 2: Open Your Django Project
Navigate to your Django project directory on the server using the Explorer. Open the project to access your code files.
Step 3: Open the Integrated Terminal
- Open Terminal: In VS Code, go to
View
>Terminal
or pressCtrl+`
to open the integrated terminal. - Activate Virtual Environment: If you use a virtual environment, activate it:
source venv/bin/activate
3. Navigate to Project Directory: Ensure you’re in the directory containing manage.py
.
Step 4: Run the Django Development Server
Execute the runserver
command:
python manage.py runserver 0.0.0.0:8000
- 0.0.0.0: This makes the server accessible from any IP address. If you’re accessing it locally through VS Code’s port forwarding, this is necessary.
- 8000: The port number. You can choose any available port.
Step 5: Accessing the Server
- Port Forwarding: VS Code can forward the server’s port to your local machine. When you run
runserver
, VS Code may prompt you to forward the port. Accept this to access the Django application via a browser on your local machine. - Open in Browser: Navigate to
http://localhost:8000
(or the port you specified) in your browser. You should see your Django application running.
Step 6: Making Changes and Auto-reloading
- Edit Files: Use VS Code to edit your Django project files. Since you’re connected via Remote — SSH, these changes are saved directly on the server.
- Auto-reload: The
runserver
process on the server detects these changes and automatically reloads the application. Refresh your browser to see the updates instantly.
Benefits of This Setup
- Efficiency: You work within a single IDE environment, eliminating the need to switch between local editors and remote SSH sessions.
- Real-time Feedback: Instant auto-reloading ensures that you see the effects of your changes without manual restarts.
- Consistent Environment: Your development environment on the server mirrors your production setup more closely, reducing discrepancies between development and deployment.
Considerations and Best Practices
While using runserver
with Remote – SSH is excellent for development, remember that runserver
is not intended for production use. For deploying Django applications in production, consider using robust servers like Gunicorn or uWSGI behind a web server like Nginx.
Additionally, ensure that:
- Security: Limit access to your development server and use SSH keys for authentication to enhance security.
- Performance: Be mindful of the server’s resources. Running
runserver
on a remote server can consume CPU and memory, especially with large projects. - Version Control: Use Git or another version control system to manage your code changes effectively. This complements the Remote — SSH setup by providing a safety net for your codebase.
Troubleshooting Common Issues
- Connection Problems: If VS Code fails to connect via Remote — SSH, verify your SSH configuration and ensure the server is reachable.
- Port Conflicts: If the chosen port is already in use, select a different port for
runserver
. - Auto-reload Not Working: Ensure that file changes are correctly saved on the server. Sometimes, network issues can interfere with the detection of file changes.
Thank you for following along with this tutorial. We hope you found it helpful and informative. If you have any questions, or if you would like to suggest new Python code examples or topics for future tutorials/articles, please feel free to join and comment. Your feedback and suggestions are always welcome!
You can find the same tutorial on Medium.com.