15 KiB
| slug | title | authors | tags | |||
|---|---|---|---|---|---|---|
| Getting Started with Ansible Installing and Configuring LAMP on Ubuntu 18.04 | Getting Started with Ansible Installing and Configuring LAMP on Ubuntu 18.04 |
|
|
import CodeBlock from '@site/src/components/CodeBloack'; import php from '@site/static/img/phpinfo.png';
- Install aptitude, a tool Ansible likes to use instead of the regular apt package manager.
- Install all the necessary packages for LAMP.
- Create a special place for your website and set it up with Apache.
- Turn on this new place for your website.
- Turn off the default Apache website if you want to (you can decide).
- Set a password for the big boss of your MySQL database.
- Clean up some unnecessary things in MySQL.
- Make sure your firewall allows traffic for your website on the internet highway
- Add a little PHP script to test if everything's working.
<CodeBlock code={cd ~/ansible-playbooks git pull } />
<CodeBlock code={cd ~ git clone https://github.com/do-community/ansible-playbooks.git cd ansible-playbooks } />
<CodeBlock code={lamp_ubuntu1804 ├── files │ ├── apache.conf.j2 │ └── info.php.j2 ├── vars │ └── default.yml ├── playbook.yml └── readme.md } />
- files/info.php.j2: This file is like a blueprint for creating a PHP test page on the web server's main spot.
- files/apache.conf.j2: Think of this file as a plan for setting up the Apache VirtualHost.
- vars/default.yml: Here, you can tweak settings in the playbook to match what you want.
- playbook.yml: The real action happens in this file. It has a list of things to do on the remote server(s).
- readme.md: This is just a text file with info about how to use this playbook.
<CodeBlock code={cd lamp_ubuntu1804 nano vars/default.yml } />
<CodeBlock code={--- mysql_root_password: "mysql_root_password" app_user: "sammy" http_host: "your_domain" http_conf: "your_domain.conf" http_port: "80" disable_default: true } />
- mysql_root_password: Choose a password for the main MySQL account.
- app_user: Pick a non-root user on your server who will own the application files.
- http_host: Your domain name.
- http_conf: The name of the configuration file in Apache.
- http_port: The HTTP port for your website (usually 80).
- disable_default: Decide whether to turn off the default Apache website.
<CodeBlock code={ansible-playbook playbook.yml -l server1 -u sammy} />
<CodeBlock code={`Output
PLAY [all] ********************************************************************************************************* TASK [Gathering Facts] *********************************************************************************************************ok: [server1]
TASK [Install prerequisites] *********************************************************************************************************ok: [server1] => (item=aptitude)
...
TASK [UFW - Allow HTTP on port 80] ********************************************************************************************************* changed: [server1]
TASK [Sets Up PHP Info Page] ********************************************************************************************************* changed: [server1]
RUNNING HANDLER [Reload Apache] ********************************************************************************************************* changed: [server1]
RUNNING HANDLER [Restart Apache] ********************************************************************************************************* changed: [server1]
PLAY RECAP *********************************************************************************************************
server1 : ok=15 changed=11 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
`} />
<CodeBlock code={http://server_host_or_IP/info.php} />
<CodeBlock code={vars/default.yml} />
<CodeBlock code={--- mysql_root_password: "mysql_root_password" app_user: "sammy" http_host: "your_domain" http_conf: "your_domain.conf" http_port: "80" disable_default: true } />
<CodeBlock code={`<VirtualHost *:{{ http_port }}> ServerAdmin webmaster@localhost ServerName {{ http_host }} ServerAlias www.{{ http_host }} DocumentRoot /var/www/{{ http_host }} ErrorLog $ {APACHE_LOG_DIR}/error.log CustomLog $ {APACHE_LOG_DIR}/access.log combined
<Directory /var/www/{{ http_host }}> Options -Indexes
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm `} /><CodeBlock code={<?php phpinfo(); } />
<CodeBlock code={`---
-
hosts: all become: true vars_files:
- vars/default.yml
tasks:
- name: Install prerequisites apt: name={{ item }} update_cache=yes state=latest force_apt_get=yes loop: [ 'aptitude' ]
#Apache Configuration
-
name: Install LAMP Packages apt: name={{ item }} update_cache=yes state=latest loop: [ 'apache2', 'mysql-server', 'python3-pymysql', 'php', 'php-mysql', 'libapache2-mod-php' ]
-
name: Create document root file: path: "/var/www/{{ http_host }}" state: directory owner: "{{ app_user }}" mode: '0755'
-
name: Set up Apache virtualhost template: src: "files/apache.conf.j2" dest: "/etc/apache2/sites-available/{{ http_conf }}" notify: Reload Apache
-
name: Enable new site shell: /usr/sbin/a2ensite {{ http_conf }} notify: Reload Apache
-
name: Disable default Apache site shell: /usr/sbin/a2dissite 000-default.conf when: disable_default notify: Reload Apache
MySQL Configuration
-
name: Sets the root password mysql_user: name: root password: "{{ mysql_root_password }}" login_unix_socket: /var/run/mysqld/mysqld.sock
-
name: Removes all anonymous user accounts mysql_user: name: '' host_all: yes state: absent login_user: root login_password: "{{ mysql_root_password }}"
-
name: Removes the MySQL test database mysql_db: name: test state: absent login_user: root login_password: "{{ mysql_root_password }}"
UFW Configuration
- name: "UFW - Allow HTTP on port {{ http_port }}" ufw: rule: allow port: "{{ http_port }}" proto: tcp
PHP Info Page
- name: Sets Up PHP Info Page template: src: "files/info.php.j2" dest: "/var/www/{{ http_host }}/info.php"
handlers:
-
name: Reload Apache service: name: apache2 state: reloaded
-
name: Restart Apache service: name: apache2 state: restarted `} />