Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Remote Debugging Django Applications with PyCharm and Server Configuration

Tech 1

Server Environment Setup

Using a fresh CentOS 7 system on Alibaba Cloud as an example:

Security Group Configuration

Add security group rules specifying port ranges and authorized IP addresses.

SSH Connection Setup

yum install openssh-server -y
service sshd restart

# If Xshell cannot connect due to password authentication being disabled
# Edit SSH configuration:
vim /etc/ssh/sshd_config

# Set PasswordAuthentication to yes
# Restart SSH service
service sshd restart

MySQL Installation and Configuration

wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server

# Restart MySQL service
service mysqld restart

# Configure bind address
vim /etc/my.cnf
# Add under [mysqld] section:
# bind-address = 0.0.0.0

# Access MySQL
mysql -u root

# Grant remote access permissions
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mysql' WITH GRANT OPTION;
FLUSH PRIVILEGES;

# Set MySQL password
SET PASSWORD = PASSWORD('mysql');
FLUSH PRIVILEGES;

Python 3.6 and Pip Installation

# Install pip
wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate
sudo python get-pip.py

# Prerequisites for Python 3.6
yum -y install zlib*
yum install openssl-devel -y

# Download and install Python 3.6
cd /tmp
wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
tar -xzvf Python-3.6.2.tgz
cd Python-3.6.2/

./configure --prefix=/usr/local
make
make altinstall

# Create symbolic link
ln -s /usr/local/bin/python3.6 /usr/bin/python3

Virtual Environment Setup

yum install python-setuptools python-devel
pip install virtualenvwrapper

# Configure virtual environment wrapper
vim ~/.bashrc

# Add these lines:
export WORKON_HOME=$HOME/.virtualenvs
source /usr/bin/virtualenvwrapper.sh

# Reload configuration
source ~/.bashrc

# Create virtual environment with specific Python version
mkvirtualenv project_env --python=python3.6
workon project_env

# Install dependencies from local export
pip install -r requirements.txt

# Handle mysqlclient installation issues
# For CentOS 7:
yum install python-devel mariadb-devel -y
# For Ubuntu:
sudo apt-get install libmysqlclient-dev
# Then:
pip install mysqlclient

Database Migration with Navicat

Connnect to remote database using:

  • Host: Server IP address
  • Uesrname: root
  • Password: Your password

Create new databace and transfer local data to remote server.

PyCharm Remote Development Configuration

Deployment Configuration

  1. Navigate to Tools >> Deployment >> Configuration
  2. Set up Connection and Mappings
  3. Test SFTP connection and upload project files
  4. Upload to remote server via Tools >> Deployment >> Upload

Server Application Execution

python manage.py runserver 0.0.0.0:8000

Required Django settings modifications:

ALLOWED_HOSTS = ['*']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'project_db',
        'USER': 'root',
        'PASSWORD': 'password123',
        'HOST': 'your_server_ip',
        'PORT': '3306',
        "OPTIONS": {"init_command": "SET default_storage_engine=INNODB;"}
    }
}

Remote Debugging Setup

Configure PyCharm interpreter to use the server's virtual environment Python executable. This enables running the application directly on the remote server through the IDE. After synchronization, set host to 0.0.0.0 and port to 8000 for remote access.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.