Python Auto deploy LAMP in centos7 and FreeBSD 10.x

In this Article we will show you how to  deploy and full Lamp  ( Apache /MariaDB server /Php) stack  installation from your laptop  or  local computer to your Centos 7 or FreeBSD 10.x server. In this small test i will show how i installed lamp on Centos 7 from another  Centos machine

Lets show you Python Auto deploy LAMP script how it work .

Behavior

Server1  : puppetnode1

[[email protected] ~]# lsb_release
LSB Version:    :core-4.1-amd64:core-4.1-noarch
[[email protected] ~]# lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.2.1511 (Core)
Release:        7.2.1511
Codename:       Core
[[email protected] ~]# ifconfig  | grep   192
        inet 192.168.28.142  netmask 255.255.255.0  broadcast 192.168.28.255
[[email protected] ~]#

Server 2 : puppetnode2

[[email protected] ~]# ifconfig   |  grep  192
        inet 192.168.28.141  netmask 255.255.255.0  broadcast 192.168.28.255
[[email protected] ~]# lsb_release  -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.2.1511 (Core)
Release:        7.2.1511
Codename:       Core

Show  /etc/hosts  file  on both  machines 

 cat  /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.28.142 puppetnode1.unixmen.com puppetnode1
192.168.28.141 puppetnode2.unixmen.com puppetnode2

Lets   Start   from  downloading script  in   Node2

Downlaod script  from Github

[[email protected] ~]# wget https://github.com/jamalshahverdiev/freebsd-centos7-amp/archive/master.zip
--2016-05-26 06:10:57--  https://github.com/jamalshahverdiev/freebsd-centos7-amp/archive/master.zip
Connecting to 10.133.80.35:8080... connected.
Proxy request sent, awaiting response... 302 Found
Location: https://codeload.github.com/jamalshahverdiev/freebsd-centos7-amp/zip/master [following]
--2016-05-26 06:10:57--  https://codeload.github.com/jamalshahverdiev/freebsd-centos7-amp/zip/master
Connecting to 10.133.80.35:8080... connected.
Proxy request sent, awaiting response... 200 OK
Length: 59418 (58K) [application/zip]
Saving to: ‘master.zip’

100%[=======================================================================================================>] 59,418      49.6KB/s   in 1.2s

2016-05-26 06:10:59 (49.6 KB/s) - ‘master.zip’ saved [59418/59418]

Extract  the zip  master

[[email protected] ~]# unzip  master.zip
Archive:  master.zip
81ccf91ee0ee7775c90dbc5a7b50faae4650fdb0
   creating: freebsd-centos7-amp-master/
  inflating: freebsd-centos7-amp-master/README.md
  inflating: freebsd-centos7-amp-master/add-vhost-apmyph.py
  inflating: freebsd-centos7-amp-master/apmyph.py
   creating: freebsd-centos7-amp-master/jinja2temps/
  inflating: freebsd-centos7-amp-master/jinja2temps/apvhost.conf
  inflating: freebsd-centos7-amp-master/jinja2temps/c7httpd.conf
  inflating: freebsd-centos7-amp-master/jinja2temps/cmy.cnf
  inflating: freebsd-centos7-amp-master/jinja2temps/fhttpd.conf
  inflating: freebsd-centos7-amp-master/jinja2temps/fphp.ini
  inflating: freebsd-centos7-amp-master/jinja2temps/lhttpd.conf
  inflating: freebsd-centos7-amp-master/jinja2temps/lphp.ini
  inflating: freebsd-centos7-amp-master/jinja2temps/my.cnf
  inflating: freebsd-centos7-amp-master/jinja2temps/newindex.php
  inflating: freebsd-centos7-amp-master/jinja2temps/ngindex.html
  inflating: freebsd-centos7-amp-master/jinja2temps/nginx.conf
  inflating: freebsd-centos7-amp-master/jinja2temps/ngphp.conf
  inflating: freebsd-centos7-amp-master/jinja2temps/ngphp.lan
  inflating: freebsd-centos7-amp-master/jinja2temps/ngsec.conf
  inflating: freebsd-centos7-amp-master/jinja2temps/tempindex.html
  inflating: freebsd-centos7-amp-master/jinja2temps/tempindex.php
  inflating: freebsd-centos7-amp-master/jinja2temps/www.conf
[[email protected] ~]#

Enter  the Directory 

you can also check the  content  script  of apmyph.py that we  will use

cat  apmyph.py add  the  bellow code

#!/usr/bin/env python2.7

import sys
import os
import jinja2

from fabric.api import *
from fabric.tasks import execute
import getpass

templateLoader = jinja2.FileSystemLoader( searchpath="/" )
templateEnv = jinja2.Environment( loader=templateLoader )
TEMPAVFILE = os.getcwd()+'/jinja2temps/apvhost.conf'
TEMPHFILE = os.getcwd()+'/jinja2temps/tempindex.html'
TEMPPFILE = os.getcwd()+'/jinja2temps/tempindex.php'

tempav = templateEnv.get_template( TEMPAVFILE )
tempht = templateEnv.get_template( TEMPHFILE )
tempphp = templateEnv.get_template( TEMPPFILE )

env.host_string = raw_input('Please enter WEB server IP address: ')
env.user = raw_input('Please enter username for UNIX/Linux server: ')
env.password = getpass.getpass()
sitename = raw_input('Please enter site name: ')

tempavVars = { "sname" : sitename, "domain" : sitename, }

outputavText = tempav.render( tempavVars )
outputahText = tempht.render( tempavVars )

with settings(
        hide('warnings', 'running', 'stdout', 'stderr'), 
        warn_only=True
):
    osver = run('uname -s')
    lintype = run('cat /etc/redhat-release | awk \'{ print $1 }\'')
    ftype = run('uname -v | awk \'{ print $2 }\' | cut -f1 -d \'.\'')
    if osver == 'FreeBSD' and ftype >= 10:
        print(' This is FreeBSD server...')
        getfhttpdpack = run('which httpd')
        httpdpidfile = run('cat /var/run/httpd.pid')
        httpdpid = run('ps waux | grep httpd | grep root | grep -v grep | awk \'{ print $2 }\'')
        if getfhttpdpack == '/usr/local/sbin/httpd' and httpdpidfile == httpdpid:
            print(' You have already installed and running Apache web server...')
            print(' If you want add new VirtualHost, please use ./add-vhost-apmyph.py script. ')
            sys.exit()
        elif getfhttpdpack != '/usr/local/sbin/httpd':
            run('pkg install -y apache24')
            run('echo \'apache24_enable="YES"\' >> /etc/rc.conf')
            ip = run('ifconfig em0 | grep \'inet \' | awk \'{ print $2 }\'')
            name = run('hostname')
            run('echo \"'+ip+' '+name+'.lan '+name+'\" >> /etc/hosts')
            put(os.getcwd()+'/jinja2temps/fhttpd.conf', '/usr/local/etc/apache24/httpd.conf')
            run('mkdir -p /usr/local/domen /var/log/httpd/ /var/www/'+sitename+'_public_html')
            with open(sitename+".conf", "wb") as apvhostfile:
                apvhostfile.write(outputavText)
            put(sitename+'.conf', '/usr/local/domen/'+sitename+'.conf')
            with open("index.html", "wb") as aphtfile:
                aphtfile.write(outputahText)
            put('index.html', '/var/www/'+sitename+'_public_html')
            run('/usr/local/etc/rc.d/apache24 start')
            print('Apache 24 server installed and configured...')
            print('If you want install and configure MySQL PHP just press "Enter"!!!')
            print('If you want to exit from script write "n" and press Enter button. ')
            inst = raw_input('Please select: ')
            if inst == "":
                print('You are selected "Enter" button')
                run('pkg install -y mysql55-server')
                run('pkg install -y mod_php56')
                run('pkg install -y php56-bz2 php56-mysql php56-mysqli php56-calendar php56-ctype php56-curl php56-dom php56-exif php56-fileinfo php56-filter php56-gd php56-gettext php56-hash php56-iconv php56-json php56-mbstring php56-mcrypt php56-openssl php56-posix php56-session php56-simplexml php56-tokenizer php56-wddx php56-xml php56-xmlreader php56-xmlwriter php56-xmlrpc php56-xsl php56-zip php56-zlib')
                put(os.getcwd()+'/jinja2temps/my.cnf', '/etc/my.cnf')
                put(os.getcwd()+'/jinja2temps/fphp.ini', '/usr/local/etc/php.ini')
                run('echo \'mysql_enable="YES"\' >> /etc/rc.conf')
                run('touch /var/log/mysql.log ; chown mysql:mysql /var/log/mysql.log')
                run('/usr/local/etc/rc.d/mysql-server start')
                run('echo -e "\n\nfreebsd\nfreebsd\n\n\n\n\n" | mysql_secure_installation 2>/dev/null')
                msqlpidfile = run('ps waux|grep mysql | grep -v grep| grep -v safe | awk \'{ print $2 }\'')
                msqlpid = run('cat /var/db/mysql/*.pid')
                if msqlpidfile == msqlpid:
                    print('MySQL service already running...')
                    pass
                else:
                    sys.exit()
                sitedb = raw_input('Enter name for new database: ')
                sitedbuser = raw_input('Enter new mysql user name: ')
                sitedbpasswd = getpass.getpass('Enter pass for '+sitedbuser+': ')
                sitedbpasswd1 = getpass.getpass('Repeat pass for '+sitedbuser+': ')
                if sitedbpasswd == sitedbpasswd1:
                    pass
                else:
                    print('The password for site db username must be the same!!! ')
                run('mysql -u root -p\'freebsd\' -e "CREATE DATABASE '+sitedb+';"')
                run('mysql -u root -p\'freebsd\' -e "GRANT ALL PRIVILEGES ON '+sitedb+'.* TO '+sitedbuser+'@localhost IDENTIFIED BY \''+sitedbpasswd+'\';"')
                run('mysql -u root -p\'freebsd\' -e "FLUSH PRIVILEGES;"')
                tempphVars = { "sitedb" : sitedb, "sitedbuser" : sitedbuser, "sitedbpasswd" : sitedbpasswd}
                outputphpText = tempphp.render( tempphVars )
                with open("index.php", "wb") as aphtfile:
                    aphtfile.write(outputphpText)
                put('index.php', '/var/www/'+sitename+'_public_html/index.php')
                run('/usr/local/etc/rc.d/apache24 restart')
                print('MySQL, Apache24 and PHP installed and configured...')
            elif inst == "n":
                print('You entered "n" button.')
            else:
                print('You can only press "Enter" button or write "n" and after press enter button!!!')
    elif osver == 'Linux' and lintype == 'CentOS':
        print(' This is CentOS server...')
        getlhttpdpack = run('which httpd')
        httpdpidfile = run('cat /var/run/httpd/httpd.pid')
        httpdpid = run('ps waux | grep httpd | grep root | grep -v grep | awk \'{ print $2 }\'')
        if getlhttpdpack == '/usr/sbin/httpd' and httpdpidfile == httpdpid:
            print(' You have already installed and running Apache web server...')
            print(' If you want add new VirtualHost, please use ./add-vhost-apmyph.py script')
            sys.exit()
        elif getlhttpdpack != '/usr/sbin/httpd':
            run('yum install -y httpd')
            ip = run('ifconfig `ifconfig | head -n1 | cut -f1 -d\':\'` | grep \'inet \' | awk \'{ print $2 }\'')
            name = run('hostname')
            run('echo \"'+ip+' '+name+'.lan '+name+'\" >> /etc/hosts')
            put(os.getcwd()+'/jinja2temps/c7httpd.conf', '/etc/httpd/conf/httpd.conf')
            run('mkdir -p /usr/local/domen /var/www/'+sitename+'_public_html')
            with open(sitename+".conf", "wb") as apvhostfile:
                apvhostfile.write(outputavText)
            put(sitename+'.conf', '/usr/local/domen/'+sitename+'.conf')
            with open("index.html", "wb") as aphtfile:
                aphtfile.write(outputahText)
            put('index.html', '/var/www/'+sitename+'_public_html')
            run('systemctl start httpd.service ; systemctl enable httpd.service')
            print('Apache 24 server installed and configured...')
            print('If you want install and configure MySQL PHP just press "Enter"!!!')
            print('If you want to exit from script write "n" and press Enter button. ')
            inst = raw_input('Please select: ')
            if inst == "":
                print('You are selected "Enter" button')
                run('yum -y install mariadb-server mariadb')
                run('yum -y install php php-mysql')
                run('yum -y install php-bcmath.x86_64 php-cli.x86_64 php-gd.x86_64 php-mbstring.x86_64 php-xml.x86_64')
                put(os.getcwd()+'/jinja2temps/cmy.cnf', '/etc/my.cnf')
#                put(os.getcwd()+'/jinja2temps/fphp.ini', '/etc/php.ini')
                run('touch /var/log/mysql.log ; chown mysql:mysql /var/log/mysql.log')
                run('systemctl start mariadb ; systemctl enable mariadb')
                run('echo -e "\n\nfreebsd\nfreebsd\n\n\n\n\n" | mysql_secure_installation 2>/dev/null')
                msqlpidfile = run('ps waux|grep mysql | grep -v grep| grep -v safe | awk \'{ print $2 }\'')
                msqlpid = run('cat /var/run/mariadb/mariadb.pid')
                if msqlpidfile == msqlpid:
                    print('MySQL service already running...')
                    pass
                else:
                    print('MySQL service is not running...')
                    sys.exit()
                sitedb = raw_input('Enter name for new database: ')
                sitedbuser = raw_input('Enter new mysql user name: ')
                sitedbpasswd = getpass.getpass('Enter pass for '+sitedbuser+': ')
                sitedbpasswd1 = getpass.getpass('Repeat pass for '+sitedbuser+': ')
                if sitedbpasswd == sitedbpasswd1:
                    pass
                else:
                    print('The repeated password for '+sitedbuser+' must be the same!!! ')
                run('mysql -u root -p\'freebsd\' -e "CREATE DATABASE '+sitedb+';"')
                run('mysql -u root -p\'freebsd\' -e "GRANT ALL PRIVILEGES ON '+sitedb+'.* TO '+sitedbuser+'@localhost IDENTIFIED BY \''+sitedbpasswd+'\';"')
                run('mysql -u root -p\'freebsd\' -e "FLUSH PRIVILEGES;"')
                tempphVars = { "sitedb" : sitedb, "sitedbuser" : sitedbuser, "sitedbpasswd" : sitedbpasswd}
                outputphpText = tempphp.render( tempphVars )
                with open("index.php", "wb") as aphtfile:
                    aphtfile.write(outputphpText)
                put('index.php', '/var/www/'+sitename+'_public_html/index.php')
                run('systemctl restart httpd.service')
                print('MySQL, Apache24 and PHP installed and configured...')
            elif inst == "n":
                print('You entered "n" button.')
            else:
                print('You can only press "Enter" button or write "n" and after press enter button!!!')
    else:
        print(' This script supports FreeBSD or CentOS7 server...')

Save and  exit

make  the  file executable
chmod  +x lamp.py
Run the File
[[email protected] ~]# python  lamp.py
Traceback (most recent call last):
  File "lamp.py", line 5, in <module>
    import jinja2
ImportError: No module named jinja2

Oww  issues  with  Python Jinja2  module.!!!

How to to solve  Python Jinja2 missing ?

Install   Jinja2 python module
[[email protected] ~]# yum   install python-jinja2
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.ams.host-engine.com
 * extras: centos.ams.host-engine.com
 * updates: centos.mirror.triple-it.nl
Resolving Dependencies
--> Running transaction check
---> Package python-jinja2.noarch 0:2.7.2-2.el7 will be installed
--> Processing Dependency: python-babel >= 0.8 for package: python-jinja2-2.7.2-2.el7.noarch
--> Processing Dependency: python-markupsafe for package: python-jinja2-2.7.2-2.el7.noarch
--> Running transaction check
---> Package python-babel.noarch 0:0.9.6-8.el7 will be installed
---> Package python-markupsafe.x86_64 0:0.11-10.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================
 Package                   Arch           Version             Repository    Size
=================================================================================
Installing:
 python-jinja2             noarch         2.7.2-2.el7         base         515 k
Installing for dependencies:
 python-babel              noarch         0.9.6-8.el7         base         1.4 M
 python-markupsafe         x86_64         0.11-10.el7         base          25 k
Try again
[[email protected] ~]# python  lamp.py
Traceback (most recent call last):
  File "lamp.py", line 7, in <module>
    from fabric.api import *
ImportError: No module named fabric.api
[[email protected] ~]#

 

Jinja2 solved . we  need  fabric

First   install  epel-release for centos 7
yum install epel-release

Install  Fabric

yum install fabric 


=================================================================================
 Package                 Arch           Version               Repository    Size
=================================================================================
Installing:
 fabric                  noarch         1.6.2-1.el7           epel         133 k
Installing for dependencies:
 libtomcrypt             x86_64         1.17-23.el7           epel         224 k
 libtommath              x86_64         0.42.0-4.el7          epel          35 k
 python-paramiko         noarch         1.15.1-1.el7          epel         999 k
 python2-crypto          x86_64         2.6.1-9.el7           epel         475 k
 python2-ecdsa           noarch         0.13-4.el7            epel          83 k

 Run  and  start deploying from Node2 to  Node1

[[email protected] freebsd-centos7-amp-master]# ./apmyph.py
Please enter WEB server IP address: 192.168.28.142
Please enter username for UNIX/Linux server: root
Password:
Please enter site name: puppetnode1
 This is CentOS server...
Apache 24 server installed and configured...
If you want install and configure MySQL PHP just press "Enter"!!!
If you want to exit from script write "n" and press Enter button.
Please select:
You are selected "Enter" button
MySQL service already running...
Enter name for new database: UNIXMEN
Enter new mysql user name: test
Enter pass for test:
Repeat pass for test:
MySQL, Apache24 and PHP installed and configured...

Check if  Lamp is running on Node 1

Check  Ports:

[[email protected] ~]# netstat  -an  | grep    LISTEN | grep   tcp
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp6       0      0 :::80                   :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 :::443                  :::*                    LISTEN

Check   apache :

[[email protected] ~]# ps -ef | grep htt
root      64778      1  0 06:22 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    64780  64778  0 06:22 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    64781  64778  0 06:22 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    64782  64778  0 06:22 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    64783  64778  0 06:22 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    64784  64778  0 06:22 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root      64965  12455  0 06:22 pts/1    00:00:00 grep --color=auto htt
[[email protected] ~]#

Check   database

[[email protected] ~]# mysql  -u test  -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.47-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show   databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| UNIXMEN            |
+--------------------+
2 rows in set (0.00 sec)
MariaDB [(none)]> use UNIXMEN;
Database changed
MariaDB [UNIXMEN]>

Configure Firewall to Allow Apache  port :

[[email protected] ~]# sudo firewall-cmd --permanent --add-port=80/tcp
success
[[email protected] ~]# sudo firewall-cmd --reload
success

Check  in ur  browser if  you see  this  you are  done 

The  browser is  reading  first php file  (httpd.conf)

[[email protected] puppetnode1_public_html]# cat   index.php
<?php
ini_set('display_errors', 1); //Enable error print to the screen
$dblocation = "localhost"; //IP address for the database
$dbname = "UNIXMEN"; //Database name to which we will connect
$dbuser = "test"; //New username for new database
$dbpasswd = "test"; //Password for the new username

$dbcnx = @mysql_connect($dblocation, $dbuser, $dbpasswd);
if (!$dbcnx){
    echo "<p>So pity MySQL is not working</p>";
    exit();
}
if ([email protected]_select_db($dbname,$dbcnx)){
    echo "<p>We are sorry, to connect to the database was not successfully.</p>";
    exit();
}
$ver = mysql_query("SELECT VERSION()");
if(!$ver){
    echo "<p>Request error</p>";
    exit();
}
echo mysql_result($ver, 0);
?>

Check your  browser  now

2016-05-26_164905

it works!!. i have not tested that on FreeBSD. but i think it will  work smoothly
Please review   that and give us your feedback