Application Management and Deployment Procedures
When deploying applications, it's essential to follow a systematic shutdown and startup sequence. The general principle is to close applications in the reverse order they were opened.
Shutdown sequence: A → B
Startup sequence: B → A
After deployment, verify functionality by checking the appplication in a web browser.
Code Deployment and Backup Strategies
Before deploying new code to the server, create a backup of the existing code using timestamp-based naming conventions.
Generating Timestamps
Linux provides the date command for timestamp generation:
date
Common Timestamp Formats
- Year-month-day:
date +%Y%m%d - Hour-minute-second:
date +%H%M%S - Full timestamp:
date +%Y%m%d%H%M%S
Formatted Timestamp Examples
- Year-month-day:
date +%Y-%m-%d - Hour-minute-second:
date +%H-%M-%S - Complete timestamp:
date +%Y-%m-%d-%H-%M-%S
Backup Implementation
Here's how to create a timestamped backup:
touch file
mv file file-`date +%y%m%d%H%M%S`
ls
SSH Key Authentication
Configure passwordless SSH access between servers for streamlined deployment.
- Generate key pair (on local machine): ```
ssh-keygen -t rsa
- Configure authorized keys (on remote server): ```
cd .ssh/
vim authorized_keys
- Edit SSH configuration (on remote server): ```
sudo vim /etc/ssh/sshd_config
Uncomment the following line: ``` AuthorizedKeysFile %h/.ssh/authorized_keys - Restart SSH service (on remote server): ```
/etc/init.d/ssh restart
- Verify connection (on local machine): ```
ssh root@47.95.8.70
Environment Setup
Create a structured directory layout for organized deployment:
mkdir /data/{server,logs,backup,soft,virtual,codes,scripts} -p
ls /data/
The recommended directory structure:
├── backup # Backups
├── codes # Code repository
├── logs # Log files
├── scripts # Deployment scripts
├── server # Application services
├── soft # Software packages
└── virtual # Virtual environments
Django Environment Deployment
Python Virtual Environment
Install the virtual environment package:
apt-get install python-virtualenv -y
Basic virtual environment operations:
- Create:
virtualenv -p /usr/bin/python2.7 venv - Activate:
source venv/bin/activate - Deactivate:
deactivate - Remove:
rm -rf venv
Django Installation
Deploy Django following these steps:
cd /data/soft
tar xf Django-*.tar.gz
cd Django-*
python setup.py install
Django Project Operations
- Create project:
django-admin startproject itcast - Create application:
python manage.py startapp test1 - Register application: Add to `itcast/settings.py````
INSTALLED_APPS = [
...
'test1',
]
View and URL Configuration
Configure views in test1/views.py:
from django.shortcuts import render
from django.http import HttpResponse
def hello(request):
return HttpResponse("itcast V1.0")
Configure URLs in itcast/urls.py:
from django.conf.urls import url
from django.contrib import admin
from test1.views import *
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hello/$', hello),
]
Running Django
Start the development server:
cd /data/server/itcast
python manage.py runserver
For production, run in background:
python manage.py runserver >> /dev/null 2>&1 &
Stop the server by killing the process:
ps aux | grep python
kill <PID>
Nginx Proxy Configuration
Django's development server is only accessible locally. Use Nginx as a reverse proxy for external access.
Nginx Installation
Install Nginx with PCRE support:
cd /data/soft/
tar xf nginx-1.10.2.tar.gz
cd nginx-1.10.2/
./configure --prefix=/data/server/nginx --without-http_gzip_module
make
make install
Nginx Operations
- Test configuration:
./sbin/nginx -t - Start server:
./sbin/nginx - Stop server:
./sbin/nginx -s stop - Reload configuration:
./sbin/nginx -s reload
Proxy Configuration
Edit conf/nginx.conf to add proxy settings:
location /hello/ {
proxy_pass http://127.0.0.1:8000;
}
Manual Code Deployment
Follow these steps for manual deployment:
- Package code:
tar zcf django.tar.gz django - Transfer code:
scp root@192.168.56.12:/data/code/django.tar.gz ./ - Stop services:```
/data/server/nginx/sbin/nginx -s stop
kill
lsof -Pti :8000 - Extract code:
cd /data/codes && tar xf django.tar.gz - Backup old files:
mv /data/server/itcast/test1/views.py /data/backup/views.py-date +%Y%m%d%H%M%S`` - Deploy new files:
mv /data/codes/django/views.py /data/server/itcast/test1/ - Start services:```
source /data/virtual/venv/bin/activate
cd /data/server/itcast/
python manage.py runserver >> /dev/null 2>&1 &
deactivate
/data/server/nginx/sbin/nginx
- Verify:
netstat -tnulp | grep :80
Automated Deployment Scripts
Script Development Framework
Create a modular deployment script with functions for each step:
#!/bin/bash
# Configuration variables
LOG_FILE='/data/logs/deploy.log'
LOCK_FILE='/tmp/deploy.pid'
# Logging function
write_log() {
log_date=$(date +%F)
log_time=$(date +%T)
step="$1"
echo "${log_date} ${log_time} $0 ${step}" >> "${LOG_FILE}"
}
# Lock management functions
add_lock() {
touch "${LOCK_FILE}"
write_log "Added lock file"
}
del_lock() {
rm -f "${LOCK_FILE}"
write_log "Removed lock file"
}
# Deployment functions
tar_code() {
write_log "Starting code packaging"
ssh root@192.168.56.12 "/bin/bash /data/scripts/tar_code.sh"
write_log "Code packaging completed"
}
scp_code() {
write_log "Starting code transfer"
cd /data/codes/
[ -f django.tar.gz ] && rm -f django.tar.gz
[ -d django ] && rm -rf django
scp root@192.168.56.12:/data/code/django.tar.gz ./
write_log "Code transfer completed"
}
serv_stop() {
write_log "Stopping Nginx service"
/data/server/nginx/sbin/nginx -s stop
write_log "Stopping Django service"
kill $(lsof -Pti :8000)
}
untar_code() {
write_log "Extracting code"
cd /data/codes
tar xf django.tar.gz
write_log "Code extraction completed"
}
deploy_code() {
write_log "Backing up existing files"
mv /data/server/itcast/test1/views.py /data/backup/views.py-$(date +%Y%m%d%H%M%S)
write_log "Deploying new files"
mv /data/codes/django/views.py /data/server/itcast/test1/
}
serv_start() {
write_log "Starting Django service"
source /data/virtual/venv/bin/activate
cd /data/server/itcast/
python manage.py runserver >> /dev/null 2>&1 &
deactivate
write_log "Starting Nginx service"
/data/server/nginx/sbin/nginx
}
verify_deployment() {
write_log "Verifying deployment"
netstat -tnulp | grep :80
write_log "Deployment verification completed"
}
# Main deployment function
main() {
if [ -f "${LOCK_FILE}" ]; then
echo "Script $0 is already running..."
exit 1
fi
add_lock
tar_code
scp_code
serv_stop
untar_code
deploy_code
serv_start
verify_deployment
del_lock
}
# Script execution
case "$1" in
"deploy")
main
;;
*)
echo "Usage: $0 [deploy]"
exit 1
;;
esac
Script Optimization Features
- Logging: Comprehensive logging of all deployement steps
- Locking mechanism: Prevents concurrent script execution
- Parameter validation: Ensures correct usage
- Modular design: Each deployment step is a separate function
- Error handling: Basic error checking and reporting
Best Practices for Deployment Scripts
- Verify all manual commands work before automation
- Structure scripts with clear function boundaries
- Implement comprehensive logging for traceability
- Add locking mechanisms to prevent race conditions
- Validate input parameters and provide clear usage instructions
- Test scripts thoroughly in a staging environment
- Document dependencies and prerequisites
Common Deployment Challenges
- Shared library dependencies (e.g., PCRE for Nginx)
- Port conflicts and process management
- Configuration file synchronization
- Rollback procedures for failed deployments
- Environment consistency across servers
Advanced Scripting Techniques
Enhance deployment scripts with these techniques:
- Conditional execution based on environment variables
- Error handling with trap signals
- Progress indicators for long-running operations
- Notification systems for deployment completion
- Rollback capabilities for failed deployments