• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Creating your own installation scipt's for CentOS?

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Mr.Guvernment

Member
Joined
Feb 26, 2003
So i am daring to venture into a new world!

Creating scripts to run to install things.

I wanted to start to read up on how i can create my own scripts post-installation to install packages into CentOS and do configurations.

i have a document i wrote, but i would love to turn it into a single .sh file i can run and poof! server is set up and done!!

This is my test environment guide i wrote for setting up a test CentOS 6.* / PHP-FPM / MySQL and NGINX for word press sites.

I know most commands i can just enter in one after the other, the hard part seems to be when i want to modify files with nano and such or allowing user input where needed like the DB user creation...

Any direction the pro's can lead me in?

Code:
NGINX WebServer set up

## Install Remi Dependency on CentOS 6 64BIT##
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

## Install Nano for editing files
yum --enablerepo=remi,remi-test install nano -y

## Disable SELinux
nano /etc/selinux/config

## Change:
SELINUX=enforcing
to
SELINUX=disabled

## Turn off IPTables
chkconfig iptables off

## Reboot system to let SELinux changes take affect
reboot


## Update centos
yum --enablerepo=remi,remi-test update -y

## Add nginx official centos repository
nano /etc/yum.repos.d/nginx.repo

## Paste
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

## Install nginx and php, mysql and other modules
yum --enablerepo=remi,remi-test install curl nginx php-cli php php-fpm php-common php-pear php-soap php-pdo php-mysql php-pgsql php-gd php-mbstring php-mcrypt php-xml gcc gcc-c++ spawn-fcgi wget mysql mysql-devel php-devel net-snmp net-snmp-utils ntp -y

# Start nginx on system start
chkconfig --add nginx
chkconfig --level 35 nginx on

## Start php-fpm on system start
chkconfig --add php-fpm
chkconfig --levels 235 php-fpm on

##Start mysqld on system start
chkconfig --add mysqld
chkconfig --level 345 mysqld on


#Start ntp on system start
chkconfig ntpd on

#Syncronize ntpd
ntpdate pool.ntp.org

#Turn on ntpd
service ntpd start

## Start nginx
service nginx start

##Start php-fpm
service php-fpm start

##Start mysql
service mysqld start

## Issue the following commands to create virtual hosting directories:
mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled

##  Configure Nginx config file to add your sites
nano /etc/nginx/nginx.conf

## Add the following lines to your /etc/nginx/nginx.conf file, immediately after the line for “include /etc/nginx/conf.d/*.conf”:
include /etc/nginx/sites-enabled/*;

## Create virtual directory for your website(s), replace 'www.example.com' with your sites name
mkdir -p /srv/www/www.example.com/public_html
mkdir /srv/www/www.example.com/logs
chown -R nginx:nginx /srv/www/www.example.com

## Create virtual host file for your site, replace 'www.example.com' with your sites name
nano /etc/nginx/sites-available/www.example.com

## paste and edit, replace 'www.example.com' with your sites name



server {
       listen 80;
       server_name www.example.com;
        access_log /srv/www/www.example.com/logs/access.log;
        error_log /srv/www/www.example.com/logs/error.log;
        root /srv/www/www.example.com/public_html;
       if ($http_host != "www.example.com") {
                 rewrite ^ http://www.example.com$request_uri permanent;
       }

       location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
       }
       # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
       location ~ /\. {
                deny all;
                access_log off;
                log_not_found off;
       }
       location / {
                try_files $uri $uri/ /index.php?$args;
       }
       

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        #fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
       fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}




## Create phpinfo file to verify PHP is working, replace 'www.example.com' with your sites name
nano /srv/www/www.example.com/public_html/test.php

##Paste in file
<?php echo phpinfo(); ?>

## Goto the enabled sites folder
cd /etc/nginx/sites-enabled/

## Create a symlink to enable the site
ln -s /etc/nginx/sites-available/www.example.com

## Restart nginx
service nginx restart

## Place an entry in your computers host file if needed to point your URL to your server for testing

##MySQL User Configuration:

mysql_secure_installation

grant all privileges on dbname.* to 'example'@'%' identified by 'example' with grant options;

## Configure SNMP
echo rocommunity public > /etc/snmp/snmpd.conf

##Restart snmp
service snmpd restart

#Run snmp on boot
chkconfig snmpd on

##Test SNMP
snmpwalk -v 1 -c public -O e 127.0.0.1
 
Given the right environment I would actually suggest using something like Puppet/Ansible/CFEngine to do this.

If you like the idea of a script you should really be doing something like this:

Code:
## Install Remi Dependency on CentOS 6 64BIT##
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

## Install Nano for editing files
yum --enablerepo=remi,remi-test install nano -y

sed -i 's/SELINUX=enforcing/SELINUX=premissive/g' /etc/selinux/config

## Turn off IPTables
/sbin/chkconfig iptables off

## Update centos
yum --enablerepo=remi,remi-test update -y

## Add nginx official centos repository
cat << EOF > /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

EOF

## Install nginx and php, mysql and other modules
yum --enablerepo=remi,remi-test install curl nginx php-cli php php-fpm php-common php-pear php-soap php-pdo php-mysql php-pgsql php-gd php-mbstring php-mcrypt php-xml gcc gcc-c++ spawn-fcgi wget mysql mysql-devel php-devel net-snmp net-snmp-utils ntp -y

# Start nginx on system start
/sbin/chkconfig --add nginx
/sbin/chkconfig --level 35 nginx on

## Start php-fpm on system start
/sbin/chkconfig --add php-fpm
/sbin/chkconfig --levels 235 php-fpm on

##Start mysqld on system start
/sbin/chkconfig --add mysqld
/sbin/chkconfig --level 345 mysqld on

#Start ntp on system start
/sbin/chkconfig ntpd on

#Syncronize ntpd
ntpdate pool.ntp.org

#Turn on ntpd
service ntpd start

## Start nginx
service nginx start

##Start php-fpm
service php-fpm start

##Start mysql
service mysqld start

## Issue the following commands to create virtual hosting directories:
mkdir /etc/nginx/{sites-available,sites-enabled}

##  Configure Nginx config file to add your sites
nano /etc/nginx/nginx.conf

## Add the following lines to your /etc/nginx/nginx.conf file, immediately after the line for “include /etc/nginx/conf.d/*.conf”:
include /etc/nginx/sites-enabled/*;

## Create virtual directory for your website(s), replace 'www.example.com' with your sites name
mkdir -p /srv/www/www.example.com/{public_html,logs}
chown -R nginx:nginx /srv/www/www.example.com

## Create virtual host file for your site, replace 'www.example.com' with your sites name
cat << EOF > /etc/nginx/sites-available/www.example.com

server {
       listen 80;
       server_name www.example.com;
        access_log /srv/www/www.example.com/logs/access.log;
        error_log /srv/www/www.example.com/logs/error.log;
        root /srv/www/www.example.com/public_html;
       if ($http_host != "www.example.com") {
                 rewrite ^ http://www.example.com$request_uri permanent;
       }

       location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
       }
       # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
       location ~ /\. {
                deny all;
                access_log off;
                log_not_found off;
       }
       location / {
                try_files $uri $uri/ /index.php?$args;
       }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        #fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
       fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

EOF


## Create phpinfo file to verify PHP is working, replace 'www.example.com' with your sites name
cat << EOF > /srv/www/www.example.com/public_html/test.php
<?php echo phpinfo(); ?>
EOF

## Create a symlink to enable the site
ln -s /etc/nginx/sites-available/www.example.com /etc/nginx/sites-enabled/

## Restart nginx
service nginx restart

## Place an entry in your computers host file if needed to point your URL to your server for testing

##MySQL User Configuration:

mysql_secure_installation

grant all privileges on dbname.* to 'example'@'%' identified by 'example' with grant options;

## Configure SNMP
echo rocommunity public > /etc/snmp/snmpd.conf

##Restart snmp
service snmpd restart

#Run snmp on boot
chkconfig snmpd on

##Test SNMP
snmpwalk -v 1 -c public -O e 127.0.0.1

## Reboot system to let SELinux changes take affect
reboot

NOTE: I personally prefer SELINUX in permissive not disabled so that there is some logging while allowing everything. Additionally, I moved the reboot because I dont think you need to reboot in the middle of the script, that would break the flow of the script. Finally, I am pretty sure there is a way to do that mysql stuff without actually logging into the mysql shell which will further automate this process
 
cat - that is how it is done! was curious.

Will check out Puppet/Ansible/CFEngine.

Going to read up on the commands you added as well since this is all completely new to me but has me interested, since automating things like this could make my life a lot easier
 
Back