Giter Site home page Giter Site logo

ansible-lemp-installation's Introduction

Ansible LEMP Installation for Ubuntu 20.04

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.

Prerequisites

  • Ansible installed on your local machine. You can install Ansible using your operating system's package manager.

Features

Automated LEMP Stack Installation:

Deploy a complete LEMP (Linux, Nginx, MySQL, PHP) stack on your local machine with a single Ansible command.

Customizable Configuration:

Modify variables in the Ansible playbook to customize Nginx, MySQL, and PHP configurations according to your specific project requirements.

Version Compatibility:

Ensure compatibility with different versions of Nginx, MySQL, and PHP. Easily update version numbers in the configuration to match your project's needs.

Project Structure

.
├── 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

Setup

Clone the Repository:

git clone https://github.com/sabarishOfficial/ansible-lemp-installation.git
cd ansible-lemp-installation

Nginx Configurations

NGINX Configuration for React, Django, and Laravel Projects

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;
        }
    }

}

Configure Inventory:

Update the inventory file with your server details

server ansible_host=IPADDRESS ansible_user=Linux_username ansible_ssh_private_key_file=example.pem

Check Ansible Playbook Syntax

ansible-playbook lemp-server.yaml --syntax-check

Test Server Connection

ansible -i inventory -m ping server

You should receive a successful ping response

Run Ansible Playbook:

ansible-playbook -i inventory lemp-server.yaml

ansible-lemp-installation's People

Contributors

sabarishofficial avatar sabarishsudalaimuthu avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.