LAMP on CentOS 7

by hmorris
43 deployments · 24 still active · last rev. 6 months ago

Feel free to contribute here: https://github.com/hmorris3293/LAMP-on-Centos7

Linux Apache MariaDB PHP fail2ban install on Centos7

Very similar to the guide below, with a few tweaks:
https://www.linode.com/docs/websites/lamp/lamp-on-centos-7

Compatible with: CentOS 7
						#!/bin/bash
##
#<UDF name="ssuser" Label="New user" example="username" />
#<UDF name="sspassword" Label="New user password" example="Password" />
#<UDF name="hostname" Label="Hostname" example="examplehost" />
#<UDF name="website" Label="Website" example="example.com" />

# <UDF name="db_password" Label="MySQL root Password" />
# <UDF name="db_name" Label="Create Database" default="" example="Create database" />
# <UDF name="db_user" Label="Create MySQL User" default="" example="Create non-root DB-user" />
# <UDF name="db_user_password" Label="MySQL User's Password" default="" example="non-root DB-password" />

# add sudo user
useradd $SSUSER && echo $SSPASSWORD | passwd $SSUSER --stdin
usermod -aG wheel $SSUSER

#remove the garbage
yum remove -y avahi chrony

# yum updates
yum update -y
yum upgrade -y
yum install -y epel-release
yum upgrade -y

#fail2ban
yum install -y fail2ban
cd /etc/fail2ban
cp fail2ban.conf fail2ban.local
cp jail.conf jail.local
sed -i -e "s/backend = auto/backend = systemd/" /etc/fail2ban/jail.local
systemctl enable fail2ban
systemctl start fail2ban

#SET HOSTNAME	
hostnamectl set-hostname $HOSTNAME


#INSTALL APACHE
sudo yum install httpd -y

# Before changing any configuration files, it is advised that you make a backup of the file. To make a backup:
cp /etc/httpd/conf/httpd.conf ~/httpd.conf.backup

# configuration of vhost file
cat <<END >/etc/httpd/conf.d/vhost.conf
NameVirtualHost *:80
        
<VirtualHost *:80>
    ServerAdmin webmaster@$WEBSITE
    ServerName $WEBSITE
    ServerAlias www.$WEBSITE
    DocumentRoot /var/www/html/$WEBSITE/public_html/
    ErrorLog /var/www/html/$WEBSITE/logs/error.log
    CustomLog /var/www/html/$WEBSITE/logs/access.log combined
</VirtualHost>
END

# make directory for website files/logs
mkdir -p /var/www/html/$WEBSITE/{public_html,logs}

# Enable&restart apache
sudo systemctl enable httpd.service
sudo systemctl restart httpd.service

#install mariadb
sudo yum install mariadb-server -y

# Enable&restart mariadb
sudo systemctl enable mariadb.service
sudo systemctl start mariadb.service

#configuring the database
mysql -e "UPDATE mysql.user SET Password = PASSWORD('$DB_PASSWORD') WHERE User = 'root'" 
mysql -e "CREATE USER '$DB_USER'@'localhost' IDENTIFIED BY '$DB_USER_PASSWORD'"
mysql -e "CREATE DATABASE $DB_NAME"
mysql -e "GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'localhost'"
mysql -e "DROP DATABASE test"

#installing php
sudo yum install php php-pear -y

#editing your php.ini file for better logging
sed -i -e "s/error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR E_ALL & ~E_DEPRECATED & ~E_STRICT/error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR/" /etc/php.ini
sed -i -e "s/;error_log = syslog/error_log = /var/log/php/error.log/" /etc/php.ini
sed -i -e "s/max_input_time =/max_input_time = 30/" /etc/php.ini

# making directory for php? giving apache permissions to that log? restarting php
mkdir /var/log/php
chown apache /var/log/php
systemctl reload httpd

#rebooting Linode 
reboot