├── hosts ├── CHANGELOG.md ├── playbook ├── 01_check_connection.yml ├── 06_download_adminer.yml ├── 05_install_nginx.yml ├── 07_nginx-default_overwrite.yml ├── 04_install_php.yml ├── 08_restart_FPM-Nginx.yml ├── 02_install_mysql.yml └── 03_database_user.yml ├── nginx-conf └── default ├── LICENSE.md ├── CONTRIBUTING.md └── README.md /hosts: -------------------------------------------------------------------------------- 1 | # Ansible-Target-Server 2 | [test] 3 | 3.111.217.83 ansible_ssh_private_key_file=/home/$USER/.ssh/id_rsa -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `Ansible PHP Nginx Setup` will be documented in this file 4 | 5 | ## v1.0.0 [9th July 2023] 6 | 7 | - Initial release. -------------------------------------------------------------------------------- /playbook/01_check_connection.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: test # Add Ansible Host name based on /etc/ansible/hosts 3 | remote_user: ubuntu # Add here Remote User-name 4 | tasks: 5 | - name: Ping target server 6 | ping: -------------------------------------------------------------------------------- /playbook/06_download_adminer.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: test # Add Ansible Host name based on /etc/ansible/hosts 3 | remote_user: ubuntu # Add here Remote User-name 4 | become: true 5 | 6 | tasks: 7 | - name: Download adminer 8 | get_url: 9 | url: http://www.adminer.org/latest.php 10 | dest: /var/www/html/butterfly.php 11 | 12 | #as your need update the name of php file for access IP:adminer 13 | # 3.111.217.83/butterfly.php -------------------------------------------------------------------------------- /playbook/05_install_nginx.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: test # Add Ansible Host name based on /etc/ansible/hosts 3 | remote_user: ubuntu # Add here Remote User-name 4 | become: true 5 | 6 | tasks: 7 | - name: INSTALL NGINX 8 | become: true 9 | package: 10 | name: nginx 11 | state: present 12 | 13 | - name: START NGINX SERVICE 14 | become: true 15 | service: 16 | name: nginx 17 | state: started 18 | -------------------------------------------------------------------------------- /nginx-conf/default: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80; 3 | listen [::]:80; 4 | 5 | root /var/www/html; 6 | 7 | # Add index.php to the list if you are using PHP 8 | index index.php index.html index.htm index.nginx-debian.html; 9 | 10 | server_name _; 11 | 12 | location / { 13 | try_files $uri $uri/ /index.php?$args; 14 | } 15 | 16 | client_max_body_size 150m; 17 | 18 | location ~ \.php$ { 19 | include snippets/fastcgi-php.conf; 20 | fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; 21 | } 22 | 23 | location ~ /\.ht { 24 | deny all; 25 | } 26 | } -------------------------------------------------------------------------------- /playbook/07_nginx-default_overwrite.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Overwrite nginx configuration file 3 | hosts: test 4 | remote_user: ubuntu 5 | become: true 6 | vars: 7 | nginx_config_src: "conf/default" 8 | nginx_config_dest: "/etc/nginx/sites-available" 9 | 10 | tasks: 11 | - name: Copy nginx configuration file 12 | copy: 13 | src: "{{ nginx_config_src }}" 14 | dest: "{{ nginx_config_dest }}" 15 | owner: ubuntu 16 | group: root 17 | mode: '0644' 18 | 19 | - name: Test nginx configuration 20 | command: nginx -t 21 | register: nginx_test_result 22 | changed_when: false 23 | failed_when: "nginx_test_result.rc != 0" 24 | 25 | - name: Restart nginx 26 | service: 27 | name: nginx 28 | state: restarted 29 | when: nginx_test_result.rc == 0 -------------------------------------------------------------------------------- /playbook/04_install_php.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Install PHP 8.1 3 | hosts: test # Add Ansible Host name based on /etc/ansible/hosts 4 | remote_user: ubuntu # Add here Remote User-name 5 | become: true 6 | 7 | tasks: 8 | - name: Install software-properties-common 9 | apt: 10 | name: software-properties-common 11 | state: present 12 | 13 | - name: Add PHP 8.1 repository 14 | apt_repository: 15 | repo: ppa:ondrej/php 16 | state: present 17 | 18 | - name: Install PHP 8.1 packages 19 | apt: 20 | name: 21 | - php8.1-fpm 22 | - php8.1-common 23 | - php8.1-mbstring 24 | - php8.1-xmlrpc 25 | - php8.1-soap 26 | - php8.1-gd 27 | - php8.1-xml 28 | - php8.1-intl 29 | - php8.1-mysql 30 | - php8.1-cli 31 | - php8.1-zip 32 | - php8.1-curl 33 | state: present 34 | -------------------------------------------------------------------------------- /playbook/08_restart_FPM-Nginx.yml: -------------------------------------------------------------------------------- 1 | - name: Modify php.ini file 2 | hosts: test # Add Ansible Host name based on /etc/ansible/hosts 3 | remote_user: ubuntu # Add here Remote User-name 4 | become: true 5 | 6 | tasks: 7 | - name: Set upload_max_filesize in php.ini 8 | lineinfile: 9 | path: /etc/php/8.1/fpm/php.ini 10 | regexp: '^upload_max_filesize =' 11 | line: 'upload_max_filesize = 200M' 12 | 13 | - name: Set post_max_size in php.ini 14 | lineinfile: 15 | path: /etc/php/8.1/fpm/php.ini 16 | regexp: '^post_max_size =' 17 | line: ' = 200M' 18 | 19 | - name: Set memory_limit in php.ini 20 | lineinfile: 21 | path: /etc/php/8.1/fpm/php.ini 22 | regexp: '^memory_limit =' 23 | line: 'memory_limit = 200M' 24 | 25 | - name: Restart PHP FPM 26 | service: 27 | name: php8.1-fpm 28 | state: restarted 29 | 30 | - name: Restart Nginx 31 | service: 32 | name: nginx 33 | state: restarted 34 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 ViitorCloud Technologies 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /playbook/02_install_mysql.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: test # Add Ansible Host name based on /etc/ansible/hosts 3 | become: true 4 | remote_user: ubuntu # Add here Remote User-name 5 | vars: 6 | mysql_root_username: root 7 | mysql_root_password: admin@123 8 | tasks: 9 | - name: install mysql 10 | apt: name="{{item}}" update_cache=yes cache_valid_time=3600 state=present 11 | with_items: 12 | - mysql-server 13 | - mysql-client 14 | - python3-mysqldb 15 | - libmysqlclient-dev 16 | 17 | - name: start up the mysql service 18 | shell: "service mysql start" 19 | 20 | - name: ensure mysql is enabled to run on startup 21 | service: name=mysql state=started enabled=true 22 | 23 | - name: update mysql root password for all root accounts 24 | mysql_user: 25 | name: root 26 | host: "{{ item }}" 27 | password: "{{ mysql_root_password }}" 28 | login_user: "{{ mysql_root_username }}" 29 | login_password: "{{ mysql_root_password }}" 30 | check_implicit_admin: yes 31 | priv: "*.*:ALL,GRANT" 32 | with_items: 33 | - 127.0.0.1 34 | - ::1 35 | - localhost 36 | -------------------------------------------------------------------------------- /playbook/03_database_user.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: test # Add Ansible Host name based on /etc/ansible/hosts 3 | remote_user: ubuntu # Add here Remote User-name 4 | become: true 5 | vars: 6 | mysql_root_username: root # MySQL Current Root Username 7 | mysql_root_password: admin@123 # MySQL Current Root User Password 8 | 9 | vars_prompt: 10 | - name: UserName 11 | prompt: "Enter Mysql Username For Newuser" 12 | private: false 13 | 14 | - name: PassWord 15 | prompt: "Enter Mysql Password For Newuser" 16 | private: false 17 | 18 | - name: DbName 19 | prompt: "Enter Mysql Database Name" 20 | private: false 21 | 22 | tasks: 23 | - name: Create New Database 24 | community.mysql.mysql_db: 25 | login_user: "{{ mysql_root_username }}" 26 | login_password: "{{ mysql_root_password }}" 27 | db: "{{ DbName }}" 28 | state: present 29 | 30 | - name: Create New User 31 | community.mysql.mysql_user: 32 | name: "{{ UserName }}" 33 | password: "{{ PassWord }}" 34 | login_user: "{{ mysql_root_username }}" 35 | login_password: "{{ mysql_root_password }}" 36 | priv: "{{ DbName }}.*:ALL" 37 | state: present -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | We accept contributions via Pull Requests on [Github](https://github.com/vcian/ansible-php-nginx-setup). 6 | 7 | Please read and understand the contribution guide before creating an issue or pull request. 8 | 9 | ## Protocol 10 | 11 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 12 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 13 | extremely unfair for them to suffer abuse or anger for their hard work. 14 | 15 | ## Pull Requests 16 | 17 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 18 | 19 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). 20 | 21 | - **Create feature branches** - Don't ask us to pull from your master branch. 22 | 23 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 24 | 25 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 26 | 27 | **Happy coding**! 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ansible Playbook for PHP-Based Website Environment Setup 2 | 📜 With the help of this Ansible playbook, you can quickly set up a PHP-based website environment with Nginx Web-server on a fresh Linux machine. This playbook automates the deployment process and installation processes, making use of Ansible's strength 💪 and adaptability. It ensures consistency and minimizes manual setup efforts. 3 | 4 | ⚙️ By leveraging the power of Ansible, you can easily replicate and scale your PHP-based website environment across multiple servers with minimal effort. Enjoy the simplicity ✨ and efficiency ⚡ provided by this playbook, ensuring a consistent and reliable setup for your web applications. 🚀 5 | 6 | ⚠️ Please ensure that you have SSH access to the target server(s) with the appropriate credentials for Ansible to connect and execute commands remotely 🖥️. 7 | 8 | # Install Ansible on Linux 9 | 10 | To install Ansible on Linux, follow these steps: 11 | 12 | 1) Add the Ansible repository to your system's package manager: 13 | 14 | ``` 15 | sudo apt-add-repository ppa:ansible/ansible 16 | ``` 17 | 18 | 2) Update the package lists to include the newly added repository: 19 | 20 | ``` 21 | sudo apt update 22 | ``` 23 | 24 | 3) Install Ansible: 25 | 26 | ``` 27 | sudo apt install ansible 28 | ``` 29 | 30 | 4) Verify the Ansible installation by checking the version: 31 | 32 | ``` 33 | ansible --version 34 | ``` 35 | The output should display the installed Ansible version information, confirming that the installation was successful. 36 | 37 | # Pre-installation for run code 38 | 39 | - For the create MySQL related user on the target server need to install it in the machine where you are performing this Ansible file. 40 | 41 | ``` 42 | ansible-galaxy collection install community.mysql 43 | ``` 44 | 45 | # Setup Ansible-Hosts file 46 | 47 | The hosts file is the inventory file used by Ansible to define and organize the hosts (remote servers) that Ansible will manage. It is a text file that lists the hostnames or IP addresses of the remote servers and organizes them into groups. 48 | 49 | Where, 50 | 51 | [test] = Your target server name. 52 | 53 | Replace 3.111.217.83 with your target server IP(s). 54 | 55 | ``` 56 | sudo nano hosts 57 | ``` 58 | 59 | ``` 60 | # Ansible-Target-Server 61 | [test] 62 | 3.111.217.83 ansible_ssh_private_key_file=/home/$USER/.ssh/id_rsa 63 | ``` 64 | 65 | # Ansible Playbooks for Server Configuration 66 | 67 | This repository contains a collection of Ansible playbooks and configuration files to automate the setup and configuration of a server. Below is a brief description of each file and its purpose. 68 | 69 | ## Installing code 70 | 71 | 1) Clone code on your machine. 72 | 73 | ``` 74 | git clone https://github.com/vcian/ansible-php-nginx-setup.git 75 | ``` 76 | 77 | 2) Enter in Direcotry. 78 | 79 | ``` 80 | cd php-ansible-setup 81 | ``` 82 | 83 | 3) Run Playbook. 84 | 85 | ``` 86 | ansible-playbook playbook/*.yml -i hosts -u ubuntu 87 | ``` 88 | 89 | 90 | ## Project-structure 91 | 92 | ``` 93 | . 94 | ├── CHANGELOG.md 95 | ├── CONTRIBUTING.md 96 | ├── hosts 97 | ├── LICENSE.md 98 | ├── nginx-conf 99 | │ └── default 100 | ├── playbook 101 | │ ├── 01_check_connection.yml 102 | │ ├── 02_install_mysql.yml 103 | │ ├── 03_database_user.yml 104 | │ ├── 04_install_php.yml 105 | │ ├── 05_install_nginx.yml 106 | │ ├── 06_download_adminer.yml 107 | │ ├── 07_nginx-default_overwrite.yml 108 | │ └── 08_restart_FPM-Nginx.yml 109 | └── README.md 110 | 111 | 2 directories, 14 files 112 | ``` 113 | 114 | ### Ansible-playbook Details 115 | 116 | This repository contains a collection of Ansible playbooks that can be used to automate various tasks related to server setup and configuration. Each playbook is designed to perform a specific task and can be executed individually or as part of a larger workflow. 117 | 118 | `01_check_connection.yml` 119 | - This playbook checks the SSH connectivity to the target server to ensure that Ansible can establish a connection. 120 | 121 | `02_install_mysql.yml` 122 | - This playbook installs the MySQL database server on the target server. 123 | 124 | `03_database_user.yml` 125 | - This playbook creates a new user and database in MySQL. It prompts for the desired username, password, and database name during execution. 126 | 127 | `04_install_php.yml` 128 | - This playbook installs PHP 8.1 on the target server. 129 | 130 | `05_install_nginx.yml` 131 | - This playbook installs the Nginx web server on the target server. 132 | 133 | `06_download_adminer.yml` 134 | - This playbook downloads and sets up the Adminer database management tool. 135 | 136 | `07_nginx-default_overwrite.yml` 137 | - This playbook overwrites the default Nginx configuration file with a custom configuration specific to adminer access with IP/*.php needs. 138 | 139 | `08_restart_FPM-Nginx.yml` 140 | - This playbook restarts the PHP-FPM and Nginx services to apply the configuration changes and update the upgrade the value of 141 | `upload_max_filesize` & `post_max_size` & `memory_limit` after restart php fpm. 142 | 143 | `nginx-conf/default` 144 | - The default file is a sample Nginx configuration file that can be used as a starting point for your specific server configuration. You can modify it as needed to serve your application. 145 | 146 | Please note that before executing these playbooks, you should update the necessary variables (vars) and configurations according to your environment and requirements. 147 | 148 | ## **Warning** 149 | 150 | Assuming you've gone so far as to get Ansible running and have downloaded these playbooks, you probably understand how this stuff works and how much damage it could do. But just in case, These playbooks will remove data, destroy accounts and wreak havoc if pointed to the wrong account. Please be careful, keep backups and read the code before running it. 151 | 152 | ## Changelog 153 | 154 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 155 | 156 | ## Contributing 157 | 158 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 159 | 160 | We believe in 161 | 👇 162 | ACT NOW 163 | PERFECT IT LATER 164 | CORRECT IT ON THE WAY. 165 | 166 | ## Security 167 | 168 | If you discover any security-related issues, please email ruchit.patel@viitor.cloud instead of using the issue tracker. 169 | 170 | ## License 171 | 172 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. --------------------------------------------------------------------------------