This project aims to deploy a Python application behind Nginx that interacts with RabbitMQ/Celery for email sending and logging functionality. The application is a Flask-based web application that sends emails asynchronously using Celery and logs messages to a file. The application also uses Nginx as a reverse proxy and Ngrok for exposing the local server to the internet.
RabbitMQ is a message broker that enables applications to communicate with each other by sending and receiving messages.
Celery is a distributed task queue that allows you to run tasks asynchronously.
Nginx is a web server that can also be used as a reverse proxy, load balancer, and HTTP cache.
-
Local Setup:
- Install RabbitMQ and Celery on your local machine.
- Set up a Python application with the following functionalities:
- An endpoint that can accept two parameters:
?sendmail
and?talktome
.
- An endpoint that can accept two parameters:
-
Endpoint Functionalities:
-
?sendmail
: When this parameter is passed, the system should:- Send an email using SMTP to the value provided (e.g.,
?sendmail=destiny@destinedcodes.com
). - Use RabbitMQ/Celery to queue the email sending task.
- Ensure the email-sending script retrieves and executes tasks from the queue.
- Send an email using SMTP to the value provided (e.g.,
-
?talktome
: When this parameter is passed, the system should:- Log the current time to
/var/log/messaging_system.log
.
- Log the current time to
-
-
Nginx Configuration:
- Configure Nginx to serve your Python application.
- Ensure proper routing of requests to the application.
-
Endpoint Access:
- Use Ngrok or a similar tool to expose your local application endpoint for external access.
- Provide a stable endpoint for testing purposes.
-
Documentation and Walk-through:
- Record a screen-captured walk-through of the entire setup and deployment process.
- Ensure the video covers:
- RabbitMQ/Celery setup.
- Python application development.
- Nginx configuration.
- Sending email via SMTP.
- Logging current time.
- Exposing the endpoint using Ngrok.
- Submit the endpoint and screen recording.
- Asynchronous email sending using Celery and RabbitMQ.
- Logging system that writes to a log file with elevated permissions.
- Nginx configuration for serving the Flask application.
- Ngrok integration for exposing the local server to the internet.
- SSL setup for secure communication.
- Install RabbitMQ and Celery on your local machine.
- Set up a Python application with the following functionalities:
- An endpoint that can accept two parameters:
?sendmail
and?talktome
.
- An endpoint that can accept two parameters:
- Configure Nginx to serve your Python application.
- Use Ngrok or a similar tool to expose your local application endpoint for external access.
Below are all the dependencies to be put in `requirements.txt` so as to be installed altogether:
Python 3.10
Flask
Flask-Mail
Celery
RabbitMQ
Redis
Nginx
Ngrok
OpenSSL
- Install RabbitMQ, start RabbitMQ, and check status
sudo apt-get update
sudo apt-get install rabbitmq-server
sudo systemctl enable rabbitmq-server
sudo systemctl start rabbitmq-server
sudo systemctl status rabbitmq-server
-
Install Nginx:
sudo apt-get install nginx
-
Install NGROK:
curl -s https://ngrok-agent.s3.amazonaws.com/ngrok.asc | \
sudo gpg --dearmor -o /etc/apt/keyrings/ngrok.gpg && \
echo "deb [signed-by=/etc/apt/keyrings/ngrok.gpg] https://ngrok-agent.s3.amazonaws.com buster main" | \
sudo tee /etc/apt/sources.list.d/ngrok.list && \
sudo apt update && sudo apt install ngrok
-
Clone the repository:
git clone https://github.com/yourusername/Messaging-System.git cd Messaging-System
-
Create a virtual environment and activate it:
python3 -m venv .venv source .venv/bin/activate
-
Install the dependencies:
pip install -r requirements.txt
-
Create a
.env
file for environment variables:SMTP_MAIL_SERVER=smtp.your-email-provider.com SMTP_MAIL_PORT=587 SMTP_MAIL_USERNAME=your-email@example.com SMTP_MAIL_PASSWORD=your-email-password SMTP_MAIL_USE_TLS=True SMTP_MAIL_USE_SSL=False RABBITMQ_ADDRESS=pyamqp://guest@localhost// REDIS_ADDRESS=redis://localhost:6379/0
-
Generate SSL certificates:
openssl genrsa -out localhost.key 2048 openssl req -new -key localhost.key -out localhost.csr openssl x509 -req -days 365 -in localhost.csr -signkey localhost.key -out localhost.crt
-
Configure Nginx:
- Create configuration file for flask application:
sudo nano /etc/nginx/sites-available/messaging_system
- Use the following configuration:
server { listen 80; listen 443 ssl; server_name localhost; ssl_certificate /path/to/localhost.crt; ssl_certificate_key /path/to/localhost.key; location / { proxy_pass http://127.0.0.1:5000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } location /log { alias /var/log/messaging_system.log; default_type text/plain; add_header Content-Type text/plain; } }
- Enable the Configuration:
sudo ln -s /etc/nginx/sites-available/messaging_system /etc/nginx/sites-enabled
- Test the configuration:
sudo nginx -t
- Restart Nginx:
sudo systemctl restart nginx
-
Run the Celery worker:
celery -A app.celery worker --loglevel=info
-
Run the Flask application:
python app.py
-
Expose the application using Ngrok:
ngrok http 80 --host-header=localhost
- Visit
http://localhost
to access the application. - Use
http://localhost?sendmail=recipient@example.com
to queue an email. - Use
http://localhost?talktome
to log the current time. - Use
http://localhost/log
to see logs.
Logs are written to /var/log/messaging_system.log
. Ensure your user has the necessary permissions to write to this file.
Run the following commands to set the appropriate permissions:
sudo touch /var/log/messaging_system.log
sudo chown yourusername:yourusername /var/log/messaging_system.log
sudo chmod 664 /var/log/messaging_system.log
sudo usermod -a -G yourusername $USER
```
To get your username
```sh
- whoami
- $USER
```
Troubleshooting:
- If you encounter permission errors, ensure that the log file has the correct ownership and permissions.
- Check the Nginx error logs for any issues related to logging.
## Contributing
Guidelines for contributing to the project.
1. Fork the repository
2. Create a new branch (git checkout -b feature-branch)
3. Commit your changes (git commit -m 'Add some feature')
4. Push to the branch (git push origin feature-branch)
5. Open a Pull Request
**Coding Standards**:
- Follow PEP 8 for Python code.
- Write clear and concise commit messages.
- Include comments and documentation for your code.
**Guidelines**:
- Ensure your code passes all tests before submitting a pull request.
- Provide a detailed description of your changes in the pull request.
## License
This project is licensed under the GNU GENERAL PUBLIC LICENSE - see the LICENSE file for details.
**Summary**:
The GNU General Public License is a free, copyleft license for software and other kinds of works. It guarantees end users the freedom to run, study, share, and modify the software.