Skip to content

Instantly share code, notes, and snippets.

@MeenachiSundaram
Last active May 11, 2025 19:05
Show Gist options
  • Select an option

  • Save MeenachiSundaram/d28d8cc65c217bebd7a4 to your computer and use it in GitHub Desktop.

Select an option

Save MeenachiSundaram/d28d8cc65c217bebd7a4 to your computer and use it in GitHub Desktop.
(LAMP) Linux Apache MySQL Php Nginx and wordpress configuration script for CentOS & Ubuntu (Automation)
#!/bin/bash
#Installing update
dist=`cat /etc/issue | awk 'NR==1{print $1}'`
if [ "$dist" == "Ubuntu" ]; then
apt-get -y update
clear
echo "Update Done..!"
sleep 5
elif [[ "$dist" == "CentOS" ]]; then
yum -y update
clear
echo "Update Done..!"
sleep 5
else
echo "Suitable OS not found..!"
fi
while true
do
echo Installation
echo "press 1 to Install MySQL"
echo "press 2 to Install Apache"
echo "press 3 to Install php"
echo "press 4 to Install Wordpress"
echo "press 5 to Install nginx"
echo "6. Exit"
echo "Enter Input => "
read ans
case "$ans" in
1) echo "MySQL installaion starts"
sleep 3
dist=`cat /etc/issue | awk 'NR==1{print $1}'`
if [ "$dist" == "Ubuntu" ]; then
sudo apt-get install -y mysql-server
sudo apt-get install -y mysql-client
#mysql_secure_installation
clear
echo "MySQL installaion done..!"
sleep 5
elif [[ "$dist" == "CentOS" ]]; then
sudo yum install -y mysql-server
sudo service mysqld start
clear
sudo service mysqld status
sleep 3
clear
sudo yum install -y mysql
#mysql_secure_installation
clear
echo "MySQL installaion done..!"
sleep 3
sudo service mysqld stop
sleep 2
clear
#Manual process during installation
echo "Press Enter and give the following command to Flush Previlages"
echo "<mysql>"
echo "<FLUSH PRIVILEGES;>"
echo "<ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';>"
sleep 10
sudo mysqld_safe --skip-grant-tables &
else
echo "Suitable OS not found..!"
fi
;;
2) echo "Apache installaion starts"
sleep 3
dist=`cat /etc/issue | awk 'NR==1{print $1}'`
if [ "$dist" == "Ubuntu" ]; then
apt-get install -y apache2
sleep 2
echo "Apache2 has been installed"
service apache2 status
sleep 2
#Creating Virtual Hosts
mkdir -p /var/www/html
cd /var/www/html
touch index.html
echo "<h1> Welcome to Ubuntu index page..! </h1>" > index.html;
echo "chech your bowser"
#create new virtual host file in apache2 configuration directory
read -p "Continue in creating virtual host (y/n)?" CONT
if [ "$CONT" == "y" ]; then
touch /etc/apache2/conf.d/helloworld.conf
cd /etc/apache2/conf.d
echo "<VirtualHost *:80>" > helloworld.conf
echo "ServerAdmin root" >> helloworld.conf
echo "ServerName helloworld.org" >> helloworld.conf
echo "ServerAlias www.helloworld.org" >> helloworld.conf
echo "DocumentRoot /var/www/html/" >> helloworld.conf
echo "</VirtualHost>" >> helloworld.conf
else
echo "virtual host file in apache2 is not created";
fi
#Restart the apache service
service apache2 restart
elif [[ "$dist" == "CentOS" ]]; then
sudo yum install -y httpd
sudo service httpd start
clear
echo "httpd installed and running"
sleep 2
clear
echo "Direct your browser to your server’s IP address"
sleep 2
clear
echo "Creating Virtual Host"
sleep 2
sudo mkdir -p /var/www/html
cd /var/www/html
touch index.html
echo "<h1> Welcome to centOS index page..! </h1>" > index.html
clear
echo "Direct your browser to your server’s IP address"
sleep 2
sudo touch /etc/httpd/conf.d/demo.conf
cd /etc/httpd/conf.d
printf "<VirtualHost *:80>\nServerAdmin root\nServerName schoolofdevops.org\nServerAlias www.schoolofdevops.org\nDocumentRoot /var/www/html/\n</VirtualHost>" > /etc/httpd/conf.d/demo.conf
sudo service httpd restart
clear
echo "Direct your browser to your server’s IP address"
sleep 2
else
echo "Suitable OS not found..!"
fi
;;
3) echo "php installaion starts"
sleep 3
dist=`cat /etc/issue | awk 'NR==1{print $1}'`
if [ "$dist" == "Ubuntu" ]; then
apt-get install -y php5 php5-mysqlnd
sleep 5
clear
touch /var/www/html/info.php
cd /var/www/html
printf "<?php\nphpinfo();\n?>" > info.php
clear
echo "check your browser using yourip/info.php"
sleep 5
elif [[ "$dist" == "CentOS" ]]; then
sudo yum install -y php php-mysql
sudo service httpd restart
sudo touch /var/www/html/info.php
cd /var/www/html
printf "<?php\nphpinfo();\n?>" > info.php
clear
echo "check your info.php page in => http://youripaddress/info.php"
sleep 5
else
echo "Suitable OS not found..!"
fi
;;
4) echo "Wordpress installaion starts"
sleep 3
dist=`cat /etc/issue | awk 'NR==1{print $1}'`
if [ "$dist" == "Ubuntu" ]; then
if [ ! -f "/tmp/latest.tar.gz" ];
then
cd /tmp
wget http://wordpress.org/latest.tar.gz
else
clear
echo "Wordpress latest file exist"
sleep 3
fi
tar -xzvf /tmp/latest.tar.gz
chown -R root:root /tmp/wordpress
#Configure WordPress application
#Move the entire content of WordPress to html directory
cp -r /tmp/wordpress/* /var/www/html
apt-get install -y php5-gd
cp /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
clear
echo "Does DB already exist or not"
echo
read -p "choose (y/n)?" ANS
if [ "$ANS" == "y" ]; then
echo "Type DB root Password"
read -s rpass
echo
echo "Name of your DB"
read dbname
echo
echo "Your user name"
read uname
echo
echo "secrete password for your db"
read -s upass
echo
sed -i 's/database_name_here/'$dbname'/g' /var/www/html/wp-config.php
sed -i 's/username_here/'$uname'/g' /var/www/html/wp-config.php
sed -i 's/password_here/'$upass'/g' /var/www/html/wp-config.php
echo "check your browser => http://your_server_ip/wp-admin/"
sleep 5
else
echo "Proceed and create one and configure it..!"
echo "Type DB root Password"
read -s rpass
echo
echo "Give a name to your DB"
read dbname
echo
echo "Input your user name"
read uname
echo
echo "secrete password for your db"
read -s upass
echo
mysql -u root --password="$rpass" -e "CREATE DATABASE "$dbname""
mysql -u root --password="$rpass" -e "CREATE USER '"$uname"'@'localhost' IDENTIFIED BY '"$upass"'";
mysql -u root --password="$rpass" -e "GRANT ALL PRIVILEGES ON * . * TO '"$uname"'@'localhost'";
mysql -u root --password="$rpass" -e "flush privileges";
sed -i 's/database_name_here/'$dbname'/g' /var/www/html/wp-config.php
sed -i 's/username_here/'$uname'/g' /var/www/html/wp-config.php
sed -i 's/password_here/'$upass'/g' /var/www/html/wp-config.php
echo "check your browser => http://your_server_ip/wp-admin/"
sleep 5
fi
elif [[ "$dist" == "CentOS" ]]; then
if [ ! -f "/tmp/latest.tar.gz" ];
then
cd /tmp
wget http://wordpress.org/latest.tar.gz
else
clear
echo "Wordpress latest file exist"
sleep 3
fi
tar -xzvf /tmp/latest.tar.gz
chown -R root:root /tmp/wordpress
#Configure WordPress application
#Move the entire content of WordPress to html directory
cp -r /tmp/wordpress/* /var/www/html
sudo yum install -y php5-gd
cp /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
clear
echo "Does DB already exist or not"
echo
read -p "choose (y/n)?" ANS
if [ "$ANS" == "y" ]; then
echo "Type DB root Password"
read -s rpass
echo
echo "Name of your DB"
read dbname
echo
echo "Your user name"
read uname
echo
echo "secrete password for your db"
read -s upass
echo
sed -i 's/database_name_here/'$dbname'/g' /var/www/html/wp-config.php
sed -i 's/username_here/'$uname'/g' /var/www/html/wp-config.php
sed -i 's/password_here/'$upass'/g' /var/www/html/wp-config.php
echo "check your browser => http://your_server_ip/wp-admin/"
sleep 5
else
echo "Proceed and create one and configure it..!"
echo "Type DB root Password"
read -s rpass
echo
echo "Give a name to your DB"
read dbname
echo
echo "Input your user name"
read uname
echo
echo "secrete password for your db"
read -s upass
echo
mysql -u root --password="$rpass" -e "CREATE DATABASE "$dbname""
mysql -u root --password="$rpass" -e "CREATE USER '"$uname"'@'localhost' IDENTIFIED BY '"$upass"'";
mysql -u root --password="$rpass" -e "GRANT ALL PRIVILEGES ON * . * TO '"$uname"'@'localhost'";
mysql -u root --password="$rpass" -e "flush privileges";
sed -i 's/database_name_here/'$dbname'/g' /var/www/html/wp-config.php
sed -i 's/username_here/'$uname'/g' /var/www/html/wp-config.php
sed -i 's/password_here/'$upass'/g' /var/www/html/wp-config.php
echo "check your browser => http://your_server_ip/wp-admin/"
sleep 5
fi
else
echo "Suitable OS not found..!"
fi
;;
5) echo "nginx installaion starts"
sleep 3
dist=`cat /etc/issue | awk 'NR==1{print $1}'`
if [ "$dist" == "Ubuntu" ]; then
apt-get install -y nginx
#Configuring Nginx for apache
#Creating a wordpress.conf file as follows
touch /etc/nginx/conf.d/wordpress.conf
cd /etc/nginx/conf.d
printf "server {\nlisten 80;\nlocation / {\nproxy_pass http://127.0.0.1:8080/; #add you IP of apche server\nproxy_set_header Host \$host;\nproxy_set_header X-Real-IP \$remote_addr;\nproxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;\nproxy_set_header X-Forwarded-Proto \$scheme;\n}\n}" > wordpress.conf
#We need to run apache in the backend nginx in the frontend so to run both in one server we need to change the port of apache. In two locations
sudo sed -i 's|*:80|*:8080|g' /etc/apache2/ports.conf
sudo sed -i 's|Listen 80|Listen 8080|g' /etc/apache2/ports.conf
sudo sed -i 's|<VirtualHost \*:80>|<VirtualHost \*:8080>|g' /etc/apache2/sites-available/default
sudo sed -i 's|DocumentRoot /var/www|DocumentRoot /var/www/html|g' /etc/apache2/sites-available/default
echo "apache2 is restarting"
service apache2 restart
sleep 5
echo "nginx is restarting"
service nginx restart
sleep 3
elif [[ "$dist" == "CentOS" ]]; then
sudo service httpd stop
sudo grep -q "Listen 8080" /etc/httpd/conf/httpd.conf && sed -i 's|Listen 8080|Listen 80|g' /etc/httpd/conf/httpd.conf && sed -i 's|Listen 80|Listen 8080|g' /etc/httpd/conf/httpd.conf || sed -i 's|Listen 80|Listen 8080|g' /etc/httpd/conf/httpd.conf
sudo yum install -y nginx
clear
touch /etc/nginx/conf.d/wordpress.conf
cd /etc/nginx/conf.d
printf "server {\nlisten 80;\nlocation / {\nproxy_pass http://127.0.0.1:8080/; #add you IP of apche server\nproxy_set_header Host \$host;\nproxy_set_header X-Real-IP \$remote_addr;\nproxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;\nproxy_set_header X-Forwarded-Proto \$scheme;\n}\n}" > /etc/nginx/conf.d/wordpress.conf
sudo service nginx configtest
if [ -f "/etc/nginx/conf.d/default.conf" ];
then
sudo service nginx stop
mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
fi
sudo service httpd stop
sudo service nginx stop
sudo service httpd start
sudo service nginx start
clear
else
echo "Suitable OS not found..!"
fi
;;
6) echo "Exit"
clear
exit
;;
*) echo "Invalid Entry"
;;
esac
sleep 2
clear
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment