Remote Debugging Django Applications with PyCharm and Server Configuration
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
- Navigate to Tools >> Deployment >> Configuration
- Set up Connection and Mappings
- Test SFTP connection and upload project files
- 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.