One place for hosting & domains

      Configure

      Configure Apache with Salt Stack


      Updated by Linode Written by Linode

      Salt is a powerful configuration management tool. In this guide you will create Salt state files that are capable of installing and configuring Apache on Ubuntu 18.04, Debian 9, or CentOS 7.

      Before You Begin

      You will need at least two Linodes with Salt installed. If you have not already, read our Getting Started with Salt – Basic Installation and Setup Guide and follow the instructions for setting up a Salt master and minion.

      The following steps will be performed on your Salt master.

      Note

      The steps in this guide require root privileges. Be sure to run the steps below as root or with the sudo prefix. For more information on privileges, see our Users and Groups guide.

      Setting Up Your Salt Master and Managed Files

      Salt Master SLS Files

      1. Create the /srv/salt directory if it does not already exist:

        mkdir /srv/salt
        
      2. Create a Salt top file in /srv/salt that will be Salt’s entry point to the Apache configuration:

        /srv/salt/top.sls
        1
        2
        3
        4
        5
        6
        7
        8
        
        base:
          'G@os_family:Debian':
            - match: compound
            - apache-debian
        
          'G@os:CentOS':
            - match: compound
            - apache-centos

        This top file uses compound matching to target your minions by operating system using Salt Grains. This will allow Salt to choose the appropriate Apache configuration depending on the Linux distribution. These matchers could be extended to be even more specific. For instance, if you wanted to only target minions with the ID of web-server that are running on Ubuntu, you can type web* and G@os:Ubuntu.

      Pillar Files

      1. Create the /srv/pillar directory if it does not already exist:

        mkdir /srv/pillar
        
      2. Create a Pillar top file. This top file references the apache.sls Pillar file that you will create in the next step:

        /srv/pillar/top.sls
        1
        2
        3
        
        base:
          '*':
            - apache
      3. Create the apache.sls file that was referenced in the previous step. This file defines Pillar data that will be used inside our Apache state file in the next section, in this case your domain name. Replace example.com with your domain:

        /srv/pillar/apache.sls

      Website Files

      1. Create a directory for your website files in the /srv/salt directory. Replace example.com with your website domain name:

        mkdir /srv/salt/example.com
        

        This directory will be accessible from your Salt state files at salt://example.com.

      2. Create an index.html file for your website in the /srv/salt/example.com directory, substituting example.com for the folder name you chose in the previous step. You will use this file as a test to make sure your website is functioning correctly.

        /srv/salt/example.com/index.html
        1
        2
        3
        4
        5
        
        <html>
          <body>
            <h1>Server Up and Running!</h1>
          </body>
        </html>

      Configuration Files

      1. Create a folder for your additional configuration files at /srv/salt/files. These files will be accessible at salt://files.

        mkdir /srv/salt/files
        
      2. Create a file called tune_apache.conf in /srv/salt/files and paste in the following block:

        /srv/salt/files/tune_apache.conf
        1
        2
        3
        4
        5
        6
        7
        
        <IfModule mpm_prefork_module>
        StartServers 4
        MinSpareServers 20
        MaxSpareServers 40
        MaxClients 200
        MaxRequestsPerChild 4500
        </IfModule>

        This MPM prefork module provides additional tuning for your Apache installation. This file will be managed by Salt and installed into the appropriate configuration directory in a later step.

      3. If you will be installing Apache on a CentOS machine, create a file called include_sites_enabled.conf in /srv/salt/files and paste in the following:

        /srv/salt/files/include_sites_enabled.conf
        1
        
        IncludeOptional sites-enabled/*.conf

        This file will allow us to use file directories like those found on Debian installations to help organize the Apache configuration.

      Creating the Apache State File for Debian and Ubuntu

      Individual Steps

      This guide will be going through the process of creating the Apache for Debian and Ubuntu state file step by step. If you would like to view the entirety of the state file, you can view it at the end of this section.

      1. Create a state file named apache-debian.sls in /srv/salt and open it in a text editor of your choice.

      2. Instruct Salt to install the apache2 package and start the apache2 service:

        /srv/salt/apache-debian.sls
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        
        apache2:
          pkg.installed
        
        apache2 Service:
          service.running:
            - name: apache2
            - enable: True
            - require:
              - pkg: apache2
        
        ...

        Here Salt makes sure the apache2 package is installed with pkg.installed. Likewise, it ensures the apache2 service is running and enabled under service.running. Also under service.running, apache-debian.sls uses require to ensure that this command does not run before the apache2 package is installed. This require step will be repeated throughout apache-debain.sls.

        Lastly, a watch statement is employed to restart the apache2 service if your site’s configuration file changes. You will define that configuration file in a later step. Note that this configuration file is named using the domain you supplied when creating your Salt Pillar file in the first section. This Pillar data will be used throughout apache-debian.sls.

      3. Turn off KeepAlive:

        /srv/salt/apache-debian.sls
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        
        ...
        
        Turn Off KeepAlive:
          file.replace:
            - name: /etc/apache2/apache2.conf
            - pattern: 'KeepAlive On'
            - repl: 'KeepAlive Off'
            - show_changes: True
            - require:
              - pkg: apache2
        ...

        KeepAlive allows multiple requests to be sent over the same TCP connection. For the purpose of this guide KeepAlive will be disabled. To disable it, Salt is instructed to find the KeepAlive directive in /etc/apache2/apache2.conf by matching a pattern and replacing it with KeepAlive Off. show_changes instructs Salt to display any changes it has made during a highstate.

      4. Transfer tune_apache.conf to your minion and enable it:

        /srv/salt/apache-debian.sls
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        
        ...
        
        /etc/apache2/conf-available/tune_apache.conf:
          file.managed:
            - source: salt://files/tune_apache.conf
            - require:
              - pkg: apache2
        
        Enable tune_apache:
          apache_conf.enabled:
            - name: tune_apache
            - require:
              - pkg: apache2
        
        ...

        This step takes the tune_apache.conf file you created in the Configuration Files step and transfers it to your Salt minion. Then, Salt enables that configuration file with the apache_conf module.

      5. Create the necessary directories:

        /srv/salt/apache-debian.sls
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        
        ...
        
        /var/www/html/{{ pillar['domain'] }}:
          file.directory
        
        /var/www/html/{{ pillar['domain'] }}/log:
          file.directory
        
        /var/www/html/{{ pillar['domain'] }}/backups:
          file.directory
        
        /var/www/html/{{ pillar['domain'] }}/public_html:
          file.directory
        
        ...
      6. Disable the default virtual host configuration file:

        /srv/salt/apache-debian.sls
        1
        2
        3
        4
        5
        6
        7
        8
        
        ...
        
        000-default:
          apache_site.disabled:
            - require:
              - pkg: apache2
        
        ...

        This step uses Salt’s apache_site module to disable the default Apache virtual host configuration file, and is the same as running a2dissite on a Debian-based machine.

      7. Create your site’s virtual host configuration file:

        /srv/salt/apache-debian.sls
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        
        ...
        
        /etc/apache2/sites-available/{{ pillar['domain'] }}.conf:
          apache.configfile:
            - config:
              - VirtualHost:
                  this: '*:80'
                  ServerName:
                    - {{ pillar['domain'] }}
                  ServerAlias:
                    - www.{{ pillar['domain'] }}
                  DocumentRoot: /var/www/html/{{ pillar['domain'] }}/public_html
                  ErrorLog: /var/www/html/{{ pillar['domain'] }}/log/error.log
                  CustomLog: /var/www/html/{{ pillar['domain'] }}/log/access.log combined
        
        ...

        This step uses Salt’s apache module, (not to be confused with the apache_site module used in the previous step), to create your site’s virtual host configuration file. The this variable signifies what would traditionally be include with VirtualHost within angle brackets in an Apache configuration file: <VirtualHost *:80>.

      8. Enable your new virtual host configuration file:

        /srv/salt/apache-debian.sls
        1
        2
        3
        4
        5
        6
        7
        8
        
        ...
        
        {{ pillar['domain'] }}:
          apache_site.enabled:
            - require:
              - pkg: apache2
        
        ...

        This step uses the same apache_site module you used to disable the default virtual host file to enable your newly created virtual host file. apache_site.enabled creates a symlink from /etc/apache2/sites-available/example.com.conf to /etc/apache2/sites-enabled/example.com.conf and is the same as running a2ensite on a Debian-based machine.

      9. Transfer your index.html website file to your minion:

        /srv/salt/apache-debian.sls
        1
        2
        3
        4
        5
        
        ...
        
        /var/www/html/{{ pillar['domain'] }}/public_html/index.html:
          file.managed:
            - source: salt://{{ pillar['domain'] }}/index.html

        Any changes made to your index.html file on your Salt master will be propagated to your minion.

        Note

        Since Salt is not watching configuration files for a change to trigger a restart for Apache, you may need to use the command below from your Salt master.

        salt '*' apache.signal restart
        

      Complete State File

      The complete apache-debian.sls file looks like this:

      /srv/salt/apache-debian.sls
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      
      apache2:
        pkg.installed
      
      apache2 Service:
        service.running:
          - name: apache2
          - enable: True
          - require:
            - pkg: apache2
      
      Turn Off KeepAlive:
        file.replace:
          - name: /etc/apache2/apache2.conf
          - pattern: 'KeepAlive On'
          - repl: 'KeepAlive Off'
          - show_changes: True
          - require:
            - pkg: apache2
      
      /etc/apache2/conf-available/tune_apache.conf:
        file.managed:
          - source: salt://files/tune_apache.conf
          - require:
            - pkg: apache2
      
      Enable tune_apache:
        apache_conf.enabled:
          - name: tune_apache
          - require:
            - pkg: apache2
      
      /var/www/html/{{ pillar['domain'] }}:
        file.directory
      
      /var/www/html/{{ pillar['domain'] }}/log:
        file.directory
      
      /var/www/html/{{ pillar['domain'] }}/backups:
        file.directory
      
      /var/www/html/{{ pillar['domain'] }}/public_html:
        file.directory
      
      000-default:
        apache_site.disabled:
          - require:
            - pkg: apache2
      
      /etc/apache2/sites-available/{{ pillar['domain'] }}.conf:
        apache.configfile:
          - config:
            - VirtualHost:
                this: '*:80'
                ServerName:
                  - {{ pillar['domain'] }}
                ServerAlias:
                  - www.{{ pillar['domain'] }}
                DocumentRoot: /var/www/html/{{ pillar['domain'] }}/public_html
                ErrorLog: /var/www/html/{{ pillar['domain'] }}/log/error.log
                CustomLog: /var/www/html/{{ pillar['domain'] }}/log/access.log combined
      
      {{ pillar['domain'] }}:
        apache_site.enabled:
          - require:
            - pkg: apache2
      
      /var/www/html/{{ pillar['domain'] }}/public_html/index.html:
        file.managed:
          - source: salt://{{ pillar['domain'] }}/index.html

      Creating an Apache State File for CentOS

      Individual Steps

      1. Create a file called apache-centos.sls in /srv/salt and open it in a text editor of your choice.

      2. On CentOS Apache is named httpd. Instruct Salt to install httpd and run the httpd service:

        /srv/salt/apache-centos.sls
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        
        httpd:
          pkg.installed
        
        httpd Service:
          service.running:
            - name: httpd
            - enable: True
            - require:
              - pkg: httpd
            - watch:
              - file: /etc/httpd/sites-available/{{ pillar['domain'] }}.conf
        
        ...

        Here Salt makes sure the httpd package is installed with pkg.installed. Likewise, it ensures the httpd service is running and enabled under service.running. Also under service.running, apache-debian.sls uses require to ensure that this command does not run before the httpd package is installed. This require step will be repeated throughout apache-centos.sls.

        Lastly, a watch statement is employed to restart the httpd service if your site’s configuration file changes. You will define that configuration file in a later step. Note that this configuration file is named using the domain you supplied when creating your Salt Pillar file in the first section. This Pillar data will be used throughout apache-centos.sls.

      3. Turn off KeepAlive:

        /srv/salt/apache-centos.sls
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        
        ...
        
        Turn Off KeepAlive:
          file.replace:
            - name: /etc/httpd/conf/httpd.conf
            - pattern: 'KeepAlive On'
            - repl: 'KeepAlive Off'
            - show_changes: True
            - require:
              - pkg: httpd
        ...

        KeepAlive allows multiple requests to be sent over the same TCP connection. For the purpose of this guide KeepAlive will be disabled. To disable it, Salt is instructed to find the KeepAlive directive in /etc/httpd/conf/httpd.conf by matching a pattern and replacing it with KeepAlive Off. show_changes instructs Salt to display any changes it has made during a highstate.

      4. Change the DocumentRoot:

        /srv/salt/apache-centos.sls
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        
        ...
        
        Change DocumentRoot:
          file.replace:
            - name: /etc/httpd/conf/httpd.conf
            - pattern: 'DocumentRoot "/var/www/html"'
            - repl: 'DocumentRoot "/var/www/html/{{ pillar['domain'] }}/public_html"'
            - show_changes: True
            - require:
              - pkg: httpd
        
        ...

        Similar to the last step, in this step salt-centos.sls instructs Salt to search for the DocumentRoot directive in Apache’s httpd.conf file, and replaces that line with the new document root. This allows for the use of a Debian-style site directory architecture.

      5. Transfer the tune_apache.conf and include_sites_enabled.conf to your minion.

        /srv/salt/apache-centos.sls
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        
        ...
        
        /etc/httpd/conf.d/tune_apache.conf:
          file.managed:
            - source: salt://files/tune_apache.conf
            - require:
              - pkg: httpd
        
        /etc/httpd/conf.d/include_sites_enabled.conf:
          file.managed:
            - source: salt://files/include_sites_enabled.conf
            - require:
              - pkg: httpd
        
        ...
      6. Create the necessary directories:

        srv/salt/apache-centos.sls
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        
        ...
        
        /etc/httpd/sites-available:
          file.directory
        
        /etc/httpd/sites-enabled:
          file.directory
        
        /var/www/html/{{ pillar['domain'] }}:
          file.directory
        
        /var/www/html/{{ pillar['domain'] }}/backups:
          file.directory
        
        /var/www/html/{{ pillar['domain'] }}/public_html:
          file.directory
        
        ...
      7. Create your site’s virtual host configuration file:

        /srv/salt/apache-centos.sls
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        
        ...
        
        /etc/httpd/sites-available/{{ pillar['domain'] }}.conf:
          apache.configfile:
            - config:
              - VirtualHost:
                  this: '*:80'
                  ServerName:
                    - {{ pillar['domain'] }}
                  ServerAlias:
                    - www.{{ pillar['domain'] }}
                  DocumentRoot: /var/www/html/{{ pillar['domain'] }}/public_html
          file.symlink:
            - target: /etc/httpd/sites-enabled/{{ pillar['domain'] }}.conf
            - force: True
        
        ...

        This step uses Salt’s apache module to create your site’s virtual host configuration file. The this variable signifies what would traditionally be include with VirtualHost within angle brackets in an Apache configuration file: <VirtualHost *:80>.

      8. Transfer your index.html website file to your minion:

        /srv/salt/apache-debian.sls
        1
        2
        3
        4
        5
        6
        7
        
        ...
        
        /var/www/html/{{ pillar['domain'] }}/public_html/index.html:
          file.managed:
            - source: salt://{{ pillar['domain'] }}/index.html
        
        ...

        Any changes made to your index.html file on your Salt master will be propigated to your minion.

      9. Configure your firewall to allow http and https traffic:

        /srv/salt/apache-centos.sls
        1
        2
        3
        4
        5
        6
        7
        8
        9
        
        ...
        
        Configure Firewall:
          firewalld.present:
            - name: public
            - ports:
              - 22/tcp
              - 80/tcp
              - 443/tcp

        Note

        It is imperative that you list all ports you need open to your machine in this section. Failure to list these ports will result in their closure by Salt.

      Complete State File

      The complete apache-centos.sls file looks like this:

      /srv/salt/apache-centos.sls
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      
      httpd:
        pkg.installed
      
      httpd Service:
        service.running:
          - name: httpd
          - enable: True
          - require:
            - pkg: httpd
          - watch:
            - file: /etc/httpd/sites-available/{{ pillar['domain'] }}.conf
      
      Turn off KeepAlive:
        file.replace:
          - name: /etc/httpd/conf/httpd.conf
          - pattern: 'KeepAlive On'
          - repl: 'KeepAlive Off'
          - show_changes: True
          - require:
            - pkg: httpd
      
      Change DocumentRoot:
        file.replace:
          - name: /etc/httpd/conf/httpd.conf
          - pattern: 'DocumentRoot "/var/www/html"'
          - repl: 'DocumentRoot "/var/www/html/{{ pillar['domain'] }}/public_html"'
          - show_changes: True
          - require:
            - pkg: httpd
      
      /etc/httpd/conf.d/tune_apache.conf:
        file.managed:
          - source: salt://files/tune_apache.conf
          - require:
            - pkg: httpd
      
      /etc/httpd/conf.d/include_sites_enabled.conf:
        file.managed:
          - source: salt://files/include_sites_enabled.conf
          - require:
            - pkg: httpd
      
      /etc/httpd/sites-available:
        file.directory
      
      /etc/httpd/sites-enabled:
        file.directory
      
      /var/www/html/{{ pillar['domain'] }}:
        file.directory
      
      /var/www/html/{{ pillar['domain'] }}/backups:
        file.directory
      
      /var/www/html/{{ pillar['domain'] }}/public_html:
        file.directory
      
      /etc/httpd/sites-available/{{ pillar['domain'] }}.conf:
        apache.configfile:
          - config:
            - VirtualHost:
                this: '*:80'
                ServerName:
                  - {{ pillar['domain'] }}
                ServerAlias:
                  - www.{{ pillar['domain'] }}
                DocumentRoot: /var/www/html/{{ pillar['domain'] }}/public_html
        file.symlink:
          - target: /etc/httpd/sites-enabled/{{ pillar['domain'] }}.conf
          - force: True
      
      /var/www/html/{{ pillar['domain'] }}/public_html/index.html:
        file.managed:
          - source: salt://{{ pillar['domain'] }}/index.html
      
      Configure Firewall:
        firewalld.present:
          - name: public
          - ports:
            - 22/tcp
            - 80/tcp
            - 443/tcp

      Running the Apache State File

      On your Salt master, issue a highstate command:

      salt '*' state.apply
      

      After a few moments you should see a list of Salt commands and a summary of their successes. Navigate to your website’s domain name if you have your DNS set up already, or your website’s public IP address. You should see your index.html file. You have now used Salt to configure Apache. Visit the links in the section below for more information.

      More Information

      You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

      Find answers, ask questions, and help others.

      This guide is published under a CC BY-ND 4.0 license.



      Source link

      How to Install and Configure pgAdmin 4 in Server Mode


      Introduction

      pgAdmin is an open-source administration and development platform for PostgreSQL and its related database management systems. Written in Python and jQuery, it supports all the features found in PostgreSQL. You can use pgAdmin to do everything from writing basic SQL queries to monitoring your databases and configuring advanced database architectures.

      In this tutorial, we’ll walk through the process of installing and configuring the latest version of pgAdmin onto an Ubuntu 18.04 server, accessing pgAdmin through a web browser, and connecting it to a PostgreSQL database on your server.

      Prerequisites

      To complete this tutorial, you will need:

      Step 1 — Installing pgAdmin and its Dependencies

      As of this writing, the most recent version of pgAdmin is pgAdmin 4, while the most recent version available through the official Ubuntu repositories is pgAdmin 3. pgAdmin 3 is no longer supported though, and the project maintainers recommend installing pgAdmin 4. In this step, we will go over the process of installing the latest version of pgAdmin 4 within a virtual environment (as recommended by the project’s development team) and installing its dependencies using apt.

      To begin, update your server’s package index if you haven’t done so recently:

      Next, install the following dependencies. These include libgmp3-dev, a multiprecision arithmetic library; libpq-dev, which includes header files and a static library that helps communication with a PostgreSQL backend; and libapache2-mod-wsgi-py3, an Apache module that allows you to host Python-based web applications within Apache:

      • sudo apt install libgmp3-dev libpq-dev libapache2-mod-wsgi-py3

      Following this, create a few directories where pgAdmin will store its sessions data, storage data, and logs:

      • sudo mkdir -p /var/lib/pgadmin4/sessions
      • sudo mkdir /var/lib/pgadmin4/storage
      • sudo mkdir /var/log/pgadmin4

      Then, change ownership of these directories to your non-root user and group. This is necessary because they are currently owned by your root user, but we will install pgAdmin from a virtual environment owned by your non-root user, and the installation process involves creating some files within these directories. After the installation, however, we will change the ownership over to the www-data user and group so it can be served to the web:

      • sudo chown -R sammy:sammy /var/lib/pgadmin4
      • sudo chown -R sammy:sammy /var/log/pgadmin4

      Next, open up your virtual environment. Navigate to the directory your programming environment is in and activate it. Following the naming conventions of the prerequisite Python 3 tutorial, we’ll go to the environments directory and activate the my_env environment:

      • cd environments/
      • source my_env/bin/activate

      Following this, download the pgAdmin 4 source code onto your machine. To find the latest version of the source code, navigate to the pgAdmin 4 (Python Wheel) Download page and click the link for the latest version (v3.4, as of this writing). This will take you to a Downloads page on the PostgreSQL website. Once there, copy the file link that ends with .whl — the standard built-package format used for Python distributions. Then go back to your terminal and run the following wget command, making sure to replace the link with the one you copied from the PostgreSQL site, which will download the .whl file to your server:

      • wget https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v3.4/pip/pgadmin4-3.4-py2.py3-none-any.whl

      Next install the wheel package, the reference implementation of the wheel packaging standard. A Python library, this package serves as an extension for building wheels and includes a command line tool for working with .whl files:

      • python -m pip install wheel

      Then install pgAdmin 4 package with the following command:

      • python -m pip install pgadmin4-3.4-py2.py3-none-any.whl

      That takes care of installing pgAdmin and its dependencies. Before connecting it to your database, though, there are a few changes you’ll need to make to the program’s configuration.

      Step 2 — Configuring pgAdmin 4

      Although pgAdmin has been installed on your server, there are still a few steps you must go through to ensure it has the permissions and configurations needed to allow it to correctly serve the web interface.

      pgAdmin’s main configuration file, config.py, is read before any other configuration file. Its contents can be used as a reference point for further configuration settings that can be specified in pgAdmin’s other config files, but to avoid unforeseen errors, you should not edit the config.py file itself. We will add some configuration changes to a new file, named config_local.py, which will be read after the primary one.

      Create this file now using your preferred text editor. Here, we will use nano:

      • nano my_env/lib/python3.6/site-packages/pgadmin4/config_local.py

      In your editor, add the following content:

      environments/my_env/lib/python3.6/site-packages/pgadmin4/config_local.py

      LOG_FILE = '/var/log/pgadmin4/pgadmin4.log'
      SQLITE_PATH = '/var/lib/pgadmin4/pgadmin4.db'
      SESSION_DB_PATH = '/var/lib/pgadmin4/sessions'
      STORAGE_DIR = '/var/lib/pgadmin4/storage'
      SERVER_MODE = True
      

      Here are what these five directives do:

      • LOG_FILE: this defines the file in which pgAdmin’s logs will be stored.
      • SQLITE_PATH: pgAdmin stores user-related data in an SQLite database, and this directive points the pgAdmin software to this configuration database. Because this file is located under the persistent directory /var/lib/pgadmin4/, your user data will not be lost after you upgrade.
      • SESSION_DB_PATH: specifies which directory will be used to store session data.
      • STORAGE_DIR: defines where pgAdmin will store other data, like backups and security certificates.
      • SERVER_MODE: setting this directive to True tells pgAdmin to run in Server mode, as opposed to Desktop mode.

      Notice that each of these file paths point to the directories you created in Step 1.

      After adding these lines, save and close the file (press CTRL + X, followed by Y and then ENTER). With those configurations in place, run the pgAdmin setup script to set your login credentials:

      • python my_env/lib/python3.6/site-packages/pgadmin4/setup.py

      After running this command, you will see a prompt asking for your email address and a password. These will serve as your login credentials when you access pgAdmin later on, so be sure to remember or take note of what you enter here:

      Output

      . . . Enter the email address and password to use for the initial pgAdmin user account: Email address: sammy@example.com Password: Retype password:

      Following this, deactivate your virtual environment:

      Recall the file paths you specified in the config_local.py file. These files are held within the directories you created in Step 1, which are currently owned by your non-root user. They must, however, be accessible by the user and group running your web server. By default on Ubuntu 18.04, these are the www-data user and group, so update the permissions on the following directories to give www-data ownership over both of them:

      • sudo chown -R www-data:www-data /var/lib/pgadmin4/
      • sudo chown -R www-data:www-data /var/log/pgadmin4/

      With that, pgAdmin is fully configured. However, the program isn't yet being served from your server, so it remains inaccessible. To resolve this, we will configure Apache to serve pgAdmin so you can access its user interface through a web browser.

      Step 3 — Configuring Apache

      The Apache web server uses virtual hosts to encapsulate configuration details and host more than one domain from a single server. If you followed the prerequisite Apache tutorial, you may have set up an example virtual host file under the name example.com.conf, but in this step we will create a new one from which we can serve the pgAdmin web interface.

      To begin, make sure you're in your root directory:

      Then create a new file in your /sites-available/ directory called pgadmin4.conf. This will be your server’s virtual host file:

      • sudo nano /etc/apache2/sites-available/pgadmin4.conf

      Add the following content to this file, being sure to update the highlighted parts to align with your own configuration:

      /etc/apache2/sites-available/pgadmin4.conf

      <VirtualHost *>
          ServerName your_server_ip
      
          WSGIDaemonProcess pgadmin processes=1 threads=25 python-home=/home/sammy/environments/my_env
          WSGIScriptAlias / /home/sammy/environments/my_env/lib/python3.6/site-packages/pgadmin4/pgAdmin4.wsgi
      
          <Directory "/home/sammy/environments/my_env/lib/python3.6/site-packages/pgadmin4/">
              WSGIProcessGroup pgadmin
              WSGIApplicationGroup %{GLOBAL}
              Require all granted
          </Directory>
      </VirtualHost>
      

      Save and close the virtual host file. Next, use the a2dissite script to disable the default virtual host file, 000-default.conf:

      • sudo a2dissite 000-default.conf

      Note: If you followed the prerequisite Apache tutorial, you may have already disabled 000-default.conf and set up an example virtual host configuration file (named example.com.conf in the prerequisite). If this is the case, you will need to disable the example.com.conf virtual host file with the following command:

      • sudo a2dissite example.com.conf

      Then use the a2ensite script to enable your pgadmin4.conf virtual host file. This will create a symbolic link from the virtual host file in the /sites-available/ directory to the /sites-enabled/ directory:

      • sudo a2ensite pgadmin4.conf

      Following this, test that your configuration file’s syntax is correct:

      If your configuration file is all in order, you will see Syntax OK. If you see an error in the output, reopen the pgadmin4.conf file and double check that your IP address and file paths are all correct, then rerun the configtest.

      Once you see Syntax OK in your output, restart the Apache service so it reads your new virtual host file:

      • sudo systemctl restart apache2

      pgAdmin is now fully installed and configured. Next, we'll go over how to access pgAdmin from a browser before connecting it to your PostgreSQL database.

      Step 4 — Accessing pgAdmin

      On your local machine, open up your preferred web browser and navigate to your server’s IP address:

      http://your_server_ip
      

      Once there, you’ll be presented with a login screen similar to the following:

      pgAdmin login screen

      Enter the login credentials you defined in Step 2, and you’ll be taken to the pgAdmin Welcome Screen:

      pgAdmin Welcome Page

      Now that you've confirmed you can access the pgAdmin interface, all that's left to do is to connect pgAdmin to your PostgreSQL database. Before doing so, though, you'll need to make one minor change to your PostgreSQL superuser's configuration.

      Step 5 — Configuring your PostgreSQL User

      If you followed the prerequisite PostgreSQL tutorial, you should already have PostgreSQL installed on your server with a new superuser role and database set up.

      By default in PostgreSQL, you authenticate as database users using the "Identification Protocol," or "ident," authentication method. This involves PostgreSQL taking the client's Ubuntu username and using it as the allowed database username. This can allow for greater security in many cases, but it can also cause issues in instances where you'd like an outside program, such as pgAdmin, to connect to one of your databases. To resolve this, we will set a password for this PostgreSQL role which will allow pgAdmin to connect to your database.

      From your terminal, open the PostgreSQL prompt under your superuser role:

      From the PostgreSQL prompt, update the user profile to have a strong password of your choosing:

      • ALTER USER sammy PASSWORD 'password';

      Then exit the PostgreSQL prompt:

      Next, go back to the pgAdmin 4 interface in your browser, and locate the Browser menu on the left hand side. Right-click on Servers to open a context menu, hover your mouse over Create, and click Server….

      Create Server context menu

      This will cause a window to pop up in your browser in which you'll enter info about your server, role, and database.

      In the General tab, enter the name for this server. This can be anything you'd like, but you may find it helpful to make it something descriptive. In our example, the server is named Sammy-server-1.

      Create Server - General tab

      Next, click on the Connection tab. In the Host name/address field, enter localhost. The Port should be set to 5432 by default, which will work for this setup, as that's the default port used by PostgreSQL.

      In the Maintenance database field, enter the name of the database you'd like to connect to. Note that this database must already be created on your server. Then, enter the PostgreSQL username and password you configured previously in the Username and Password fields, respectively.

      Create Server - Connection tab

      The empty fields in the other tabs are optional, and it's only necessary that you fill them in if you have a specific setup in mind in which they're required. Click the Save button, and the database will appear under the Servers in the Browser menu.

      You've successfully connected pgAdmin4 to your PostgreSQL database. You can do just about anything from the pgAdmin dashboard that you would from the PostgreSQL prompt. To illustrate this, we will create an example table and populate it with some sample data through the web interface.

      Step 6 — Creating a Table in the pgAdmin Dashboard

      From the pgAdmin dashboard, locate the Browser menu on the left-hand side of the window. Click on the plus sign (+) next to Servers (1) to expand the tree menu within it. Next, click the plus sign to the left of the server you added in the previous step (Sammy-server-1 in our example), then expand Databases, the name of the database you added (sammy, in our example), and then Schemas (1). You should see a tree menu like the following:

      Expanded Browser tree menu

      Right-click the Tables list item, then hover your cursor over Create and click Table….

      Create Table context menu

      This will open up a Create-Table window. Under the General tab of this window, enter a name for the table. This can be anything you'd like, but to keep things simple we'll refer to it as table-01.

      Create Table - General tab

      Then navigate to the Columns tab and click the + sign in the upper right corner of the window to add some columns. When adding a column, you're required to give it a Name and a Data type, and you may need to choose a Length if it's required by the data type you've selected.

      Additionally, the official PostgreSQL documentation states that adding a primary key to a table is usually best practice. A primary key is a constraint that indicates a specific column or set of columns that can be used as a special identifier for rows in the table. This isn't a requirement, but if you'd like to set one or more of your columns as the primary key, toggle the switch at the far right from No to Yes.

      Click the Save button to create the table.

      Create Table - Columns Tab with Primary Key turned on

      By this point, you've created a table and added a couple columns to it. However, the columns don't yet contain any data. To add data to your new table, right-click the name of the table in the Browser menu, hover your cursor over Scripts and click on INSERT Script.

      INSERT script context menu

      This will open a new panel on the dashboard. At the top you'll see a partially-completed INSERT statement, with the appropriate table and column names. Go ahead and replace the question marks (?) with some dummy data, being sure that the data you add aligns with the data types you selected for each column. Note that you can also add multiple rows of data by adding each row in a new set of parentheses, with each set of parentheses separated by a comma as shown in the following example.

      If you'd like, feel free to replace the partially-completed INSERT script with this example INSERT statement:

      INSERT INTO public."table-01"(
          col1, col2, col3)
          VALUES ('Juneau', 14, 337), ('Bismark', 90, 2334), ('Lansing', 51, 556);
      

      Example INSERT statement

      Click on the lightning bolt icon () to execute the INSERT statement. To view the table and all the data within it, right-click the name of your table in the Browser menu once again, hover your cursor over View/Edit Data, and select All Rows.

      View/Edit Data, All Rows context menu

      This will open another new panel, below which, in the lower panel's Data Output tab, you can view all the data held within that table.

      View Data - example data output

      With that, you've successfully created a table and populated it with some data through the pgAdmin web interface. Of course, this is just one method you can use to create a table through pgAdmin. For example, it's possible to create and populate a table using SQL instead of the GUI-based method described in this step.

      Conclusion

      In this guide, you learned how to install pgAdmin 4 from a Python virtual environment, configure it, serve it to the web with Apache, and how to connect it to a PostgreSQL database. Additionally, this guide went over one method that can be used to create and populate a table, but pgAdmin can be used for much more than just creating and editing tables.

      For more information on how to get the most out of all of pgAdmin's features, we encourage you to review the project's documentation. You can also learn more about PostgreSQL through our Community tutorials on the subject.



      Source link

      How to Install and Configure Ansible on Ubuntu 18.04


      Introduction

      Configuration management systems are designed to make controlling large numbers of servers easy for administrators and operations teams. They allow you to control many different systems in an automated way from one central location.

      While there are many popular configuration management systems available for Linux systems, such as Chef and Puppet, these are often more complex than many people want or need. Ansible is a great alternative to these options because it requires a much smaller overhead to get started.

      In this guide, we will discuss how to install Ansible on an Ubuntu 18.04 server and go over some basics of how to use the software.

      How Does Ansible Work?

      Ansible works by configuring client machines from a computer that has the Ansible components installed and configured.

      It communicates over normal SSH channels to retrieve information from remote machines, issue commands, and copy files. Because of this, an Ansible system does not require any additional software to be installed on the client computers.

      This is one way that Ansible simplifies the administration of servers. Any server that has an SSH port exposed can be brought under Ansible’s configuration umbrella, regardless of what stage it is at in its life cycle. This means that any computer that you can administer through SSH, you can also administer through Ansible.

      Ansible takes on a modular approach, making it easy to extend to use the functionalities of the main system to deal with specific scenarios. Modules can be written in any language and communicate in standard JSON.

      Configuration files are mainly written in the YAML data serialization format due to its expressive nature and its similarity to popular markup languages. Ansible can interact with hosts either through command line tools or its configuration scripts, which are known as Playbooks.

      Prerequisites

      To follow this tutorial, you will need:

      • Two or more Ubuntu 18.04 servers. One of these will be used as your Ansible server, while the remainder will be used as your Ansible hosts. Each should have a non-root user with sudo privileges and a basic firewall configured. You can set this up by following our Initial Server Setup Guide for Ubuntu 18.04. Please note that the examples throughout this guide specify three Ansible hosts, but the commands and configurations shown can be adjusted for any number of clients.

      • SSH keys generated for the non-root user on your Ansible server. To do this, follow Step 1 of our guide on How to Set Up SSH Keys on Ubuntu 18.04. For the purposes of this tutorial, you can save the key pair to the default location (~/.ssh/id_rsa) and you do not need to password-protect it.

      Step 1 — Installing Ansible

      To begin using Ansible as a means of managing your various servers, you need to install the Ansible software on at least one machine.

      To get the latest version of Ansible for Ubuntu, you can add the project’s PPA (personal package archive) to your system. Before doing this, though, you should first update your package index and install the software-properties-common package. This software will make it easier to manage this and other independent software repositories:

      • sudo apt update
      • sudo apt install software-properties-common

      Then add the Ansible PPA by typing the following command:

      • sudo apt-add-repository ppa:ansible/ansible

      Press ENTER to accept the PPA addition.

      Next, refresh your system’s package index once again so that it is aware of the packages available in the PPA:

      Following this update, you can install the Ansible software:

      Your Ansible server now has all of the software required to administer your hosts.

      Step 2 — Configuring SSH Access to the Ansible Hosts

      As mentioned previously, Ansible primarily communicates with client computers through SSH. While it certainly has the ability to handle password-based SSH authentication, using SSH keys can help to keep things simple.

      On your Ansible server, use the cat command to print the contents of your non-root user’s SSH public key file to the terminal’s output:

      Copy the resulting output to your clipboard, then open a new terminal and connect to one of your Ansible hosts using SSH:

      • ssh sammy@ansible_host_ip

      Switch to the client machine’s root user:

      As the root user, open the authorized_keys within the ~/.ssh directory:

      • nano ~/.ssh/authorized_keys

      In the file, paste your Ansible server user’s SSH key, then save the file and close the editor (press CTRL + X, Y, then ENTER). Then run the exit command to return to the host’s non-root user:

      Lastly, because Ansible uses a python interpreter located at /usr/bin/python to run its modules, you’ll need to install Python 2 on the host in order for Ansible to communicate with it. Run the following commands to update the host’s package index and install the python package:

      • sudo apt update
      • sudo apt install python

      Following this, you can run the exit command once again to close the connection to the client:

      Repeat this process for each server you intend to control with your Ansible server. Next, we’ll configure the Ansible server to connect to these hosts using Ansible’s hosts file.

      Step 3 — Setting Up Ansible Hosts

      Ansible keeps track of all of the servers that it knows about through a hosts file. We need to set up this file first before we can begin to communicate with our other computers.

      Open the file with sudo privileges, like this:

      • sudo nano /etc/ansible/hosts

      Inside the file, you will see a number of example configurations that have been commented out (with a # preceding each line). These examples won’t actually work for us since the hosts listed in each one are made up. We will, however, keep these examples in the file to help us with configuration if we want to implement more complex scenarios in the future.

      The hosts file is fairly flexible and can be configured in a few different ways. The syntax we are going to use, though, looks like this:

      [group_name]
      alias ansible_ssh_host=your_server_ip
      

      In this example, group_name is an organizational tag that lets you refer to any servers listed under it with one word, while alias is just a name to refer to one specific server.

      So, in our scenario, we are imagining that we have three servers we are going to control with Ansible. At this point, these servers are accessible from the Ansible server by typing:

      You should not be prompted for a password if you have set this up correctly. For the purpose of demonstration, we will assume that our hosts' IP addresses are 203.0.113.1, 203.0.113.2, and 203.0.113.3. We will set this up so that we can refer to these individually as host1, host2, and host3, or as a group with the name servers.

      This is the block that we should add to our hosts file to accomplish this:

      /etc/ansible/hosts

      [servers]
      host1 ansible_ssh_host=203.0.113.1
      host2 ansible_ssh_host=203.0.113.2
      host3 ansible_ssh_host=203.0.113.3
      

      Hosts can be in multiple groups and groups can configure parameters for all of their members. Let's try this out now.

      With our current settings, if we tried to connect to any of these hosts with Ansible, the command would fail (assuming you are not operating as the root user). This is because your SSH key is embedded for the root user on the remote systems and Ansible will by default try to connect as your current user. A connection attempt will get this error:

      Output

      host1 | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true }

      On the Ansible server, we're using a user called sammy. Ansible will try to connect to each host with ssh sammy@server. This will not work if the sammy user is not on the remote system as well.

      We can create a file that tells all of the servers in the "servers" group to connect as the root user.

      To do this, we will create a directory in the Ansible configuration structure called group_vars. Within this folder, we can create YAML-formatted files for each group we want to configure:

      • sudo mkdir /etc/ansible/group_vars
      • sudo nano /etc/ansible/group_vars/servers

      We can put our configuration in here. YAML files start with "---", so make sure you don't forget that part.

      /etc/ansible/group_vars/servers

      ---
      ansible_ssh_user: root
      

      Save and close this file when you are finished.

      If you want to specify configuration details for every server, regardless of group association, you can put those details in a file at /etc/ansible/group_vars/all. Individual hosts can be configured by creating files named after their alias under a directory at /etc/ansible/host_vars.

      Step 4 — Using Simple Ansible Commands

      Now that we have our hosts set up and enough configuration details to allow us to successfully connect to our hosts, we can try out our very first command.

      Ping all of the servers you configured by typing:

      Ping output

      host1 | SUCCESS => {
          "changed": false,
          "ping": "pong"
      }
      
      host3 | SUCCESS => {
          "changed": false,
          "ping": "pong"
      }
      
      host2 | SUCCESS => {
          "changed": false,
          "ping": "pong"
      }
      

      This is a basic test to make sure that Ansible has a connection to all of its hosts.

      The all means all hosts. We could just as easily specify a group:

      We could also specify an individual host:

      We can specify multiple hosts by separating them with colons:

      • ansible -m ping host1:host2

      The -m ping portion of the command is an instruction to Ansible to use the "ping" module. These are basically commands that you can run on your remote hosts. The ping module operates in many ways like the normal ping utility in Linux, but instead it checks for Ansible connectivity.

      The ping module doesn't really take any arguments, but we can try another command to see how that works. We pass arguments into a script by typing -a.

      The "shell" module lets us send a terminal command to the remote host and retrieve the results. For instance, to find out the memory usage on our host1 machine, we could use:

      • ansible -m shell -a 'free -m' host1

      Shell output

      host1 | SUCCESS | rc=0 >>
                   total       used       free     shared    buffers     cached
      Mem:          3954        227       3726          0         14         93
      -/+ buffers/cache:        119       3834
      Swap:            0          0          0
      

      With that, your Ansible server configured and you can successfully communicate and control your hosts.

      Conclusion

      In this tutorial, we have configured Ansible and verified that it can communicate with each host. We have also used the ansible command to execute simple tasks remotely.

      Although this is useful, we have not covered the most powerful feature of Ansible in this article: Playbooks. Ansible Playbooks are a powerful, simple way to manage server configurations and multi-machine deployments. For an introduction to Playbooks, see this guide. Additionally, we encourage you to check out the official Ansible documentation to learn more about the tool.



      Source link