Python Website Hosting on Jenkins
GitHub Repository Link: https://github.com/Divyap8/pythonapp
Setup of Jenkins on EC2 Instance
Step-I: Install Jenkins on EC2 Instance
1. Update the system:
sudo yum update
2. Add Jenkins repository:
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-
stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
3. Upgrade the system:
sudo yum upgrade
4. Install Java and Jenkins:
sudo dnf install java-17-amazon-corretto -y
sudo yum install jenkins -y
5. Enable and start Jenkins:
sudo systemctl enable jenkins
sudo systemctl start jenkins
6. Verify Jenkins status:
sudo systemctl status jenkins
7. Access Jenkins:
Open `http://<your_server_public_DNS>:8080` in your browser and follow setup
instructions.
To retrieve the initial admin password, run:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Step-II: Resize `/tmp` Directory
1. Check the current size of /tmp:
df -h /tmp
2. Resize the directory:
sudo mount -o remount,size=2G /tmp
3. Persist the change by adding the following line to `/etc/fstab`:
tmpfs /tmp tmpfs defaults,size=2G,noatime,nosuid 0 0
4. Verify and apply changes:
df -h /tmp
sudo reboot
sudo systemctl restart Jenkins
Step-III: Set Up Python and Virtual Environment
1. Check Python version:
python3 --version
2. Install pip and virtual environment tools:
sudo yum install python3-pip
pip -V
sudo yum install python3-virtualenv.noarch
On Jenkins Website
Step-I: Install Plugins
1. Install the Git Plugin and configure it under "Manage Jenkins" > "Global Tool
Configuration".
2. Install the SSH Plugin and configure "SSH Remote Host" in "System Configuration".
Step-II: Build Job
1. Create a new freestyle project named "Build Job".
2. In the "Source Code Management" section, select Git and add your repository URL.
3. Specify the branch to build (e.g., main).
4. In "Build Triggers", select "GitHub hook trigger for GITScm polling".
5. Add the following build steps in "Execute Shell":
python3 -m venv myenv
source myenv/bin/activate
pip install -r requirements.txt
Step-III: Test Job
1. Create a new freestyle project named "Test Job".
2. In the "Source Code Management" section, select Git and add your repository URL.
3. Specify the branch to build (e.g., main).
4. In "Build Triggers", select "Build after other projects are built".
- Projects to watch: Select your "Build Job".
- Trigger only if the build is stable.
5. Add the following build steps in "Execute Shell":
python3 -m venv myenv
source myenv/bin/activate
pip install -r requirements.txt
python3 -m unittest discover -s test
Step-IV: Deploy Job
1. Create a new freestyle project named "Deploy Job".
2. In the "Source Code Management" section, select Git and add your repository URL.
3. Specify the branch to build (e.g., main).
4. In "Build Triggers", select "Build after other projects are built".
- Projects to watch: Select your "Test Job".
- Trigger only if the test is stable.
5. Add the following build steps to execute on a remote host using SSH:
cd pythonapp
git pull origin main
pip install -r requirements.txt
killall gunicorn
gunicorn --workers 3 --bind 0.0.0.0:5000 app:app&
Setup of Live Server
1. Check Python version:
python3 --version
2. Install necessary tools:
sudo yum install python3-virtualenv.noarch
sudo yum install git
3. Clone your repository:
git clone https://github.com/Divyap8/pythonapp.git
4. Set up a virtual environment and install dependencies:
cd pythonapp/
python3 -m venv myenv
source myenv/bin/activate
pip install -r requirements.txt
5. Start the Gunicorn server:
gunicorn --workers 3 --bind 0.0.0.0:5000 app:app&
6. Configure Nginx as a proxy:
sudo yum install nginx
sudo service nginx start
Edit the Nginx configuration file:
sudo nano /etc/nginx/nginx.conf
Add the following location block:
location / {
proxy_pass http://localhost:5000;
}
Reload Nginx:
sudo service nginx reload