This configuration is tailored for Ubuntu 20.04 and Ansible version [core 2.15.4]. It includes essential components such as Nginx, PHP 8.2, MySQL, and phpMyAdmin. The Nginx configurations provided cater to use cases involving React and Django applications. Furthermore, the setup is highly customizable, allowing you to adapt it to your specific project needs.
- Ansible installed on your local machine. You can install Ansible using your operating system's package manager.
Deploy a complete LEMP (Linux, Nginx, MySQL, PHP) stack on your local machine with a single Ansible command.
Modify variables in the Ansible playbook to customize Nginx, MySQL, and PHP configurations according to your specific project requirements.
Ensure compatibility with different versions of Nginx, MySQL, and PHP. Easily update version numbers in the configuration to match your project's needs.
.
├── ansible.cfg
├── inventroy
├── lemp-server
│ ├── README.md
│ ├── defaults
│ │ └── main.yml
│ ├── files
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ │ └── main.yml
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ │ └── server.conf
│ ├── tests
│ │ └── test.yml
│ └── vars
│ └── main.yml
└── lemp-server.yaml
10 directories, 11 files
Clone the Repository:
git clone https://github.com/sabarishOfficial/ansible-lemp-installation.git
cd ansible-lemp-installation
Below is the NGINX configuration that can be utilized for React, Django, and Laravel projects. This configuration is designed to be added to the template directory file, allowing you to choose the specific configuration you need. Please add the required configuration to your task directory's main.yaml file.
# Nginx Server Block Configuration
server {
# Server Name
server_name _;
# Client Max Body Size
client_max_body_size 100m;
# React Use Case
location / {
root /var/www/html/frontend;
try_files $uri /index.html;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Origin' '*' always;
}
# Django Use Case
location /api/ {
# Handle OPTIONS request for CORS
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' '*' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,*' always;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Content-Length' 0;
add_header 'Content-Type' 'text/plain charset=UTF-8';
return 200;
}
# Proxy requests to the upstream server
alias /var/www/html/backend/;
proxy_pass http://127.0.0.1:8000;
# Proxy headers if needed
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# Configure proxy timeouts if needed
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
# CORS Headers
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Origin' '*' always;
}
# phpMyAdmin Configuration
location /phpmyadmin {
root /usr/share/;
index index.php;
try_files $uri $uri/ =404;
# Deny access to specific phpMyAdmin paths
location ~ ^/phpmyadmin/(doc|sql|setup)/ {
deny all;
}
# Pass PHP requests to php-fpm
location ~ /phpmyadmin/(.+\.php)$ {
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
include snippets/fastcgi-php.conf;
proxy_read_timeout 600s;
}
}
}
Laravel Nginx Configuration
server {
server_name _;
root /var/www/html/frontend/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
add_header 'Access-Control-Allow-Origin' '*' always;
proxy_set_header X-Forwarded-Proto $scheme;
add_header 'Content-Security-Policy' 'upgrade-insecure-requests';
}
location ~ ^/(.+\.php)$ {
root /var/www/html/frontend/public/$1;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
location /phpmyadmin {
root /usr/share/;
proxy_read_timeout 600s;
index index.php;
try_files $uri $uri/ =404;
location ~ ^/phpmyadmin/(doc|sql|setup)/ {
deny all;
}
location ~ /phpmyadmin/(.+\.php)$ {
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
include snippets/fastcgi-php.conf;
proxy_read_timeout 600s;
}
}
}
Update the inventory file with your server details
server ansible_host=IPADDRESS ansible_user=Linux_username ansible_ssh_private_key_file=example.pem
ansible-playbook lemp-server.yaml --syntax-check
ansible -i inventory -m ping server
You should receive a successful ping response
ansible-playbook -i inventory lemp-server.yaml