Skip to content

Instantly share code, notes, and snippets.

@Jkotheimer
Created June 28, 2020 01:42
Show Gist options
  • Save Jkotheimer/ba52ce571dded32af31cff284fef8383 to your computer and use it in GitHub Desktop.
Save Jkotheimer/ba52ce571dded32af31cff284fef8383 to your computer and use it in GitHub Desktop.
A quick and easy script to get Apache HTTPD and PHP7 configured and running on Arch Linux.
#!/usr/bin/bash
# If the user does not have elevated privileges, do that now
[[ $(whoami) != 'root' || $EUID != 0 ]] && {
sudo "$0" "$@"
exit $?
}
function uncomment {
sudo sed -i "/${1}/s/^#//g" ${2}
}
function comment {
sudo sed -i "/${1}/s/^/#/g" ${2}
}
function line_exists {
line=$(grep $1 $2)
[[ $? != 0 || $line == "#"* ]] && return 1
return 0
}
# Download and install all dependencies
packages=(apache php php-fpm php-cgi mod_fcgid 6tunnel abook)
downloads=()
for i in "${!packages[@]}"; do
# If the package isn't there, add it to the download list
pkg=${packages[$i]}
pacman -Q $pkg > /dev/null 2>&1
[ $? != 0 ] && downloads=("$pkg" "${downloads[@]}")
done
[ ${#downloads[@]} != 0 ] && pacman -S ${downloads[@]}
# Copy a backup of the conf file, then either overwrite with
# an original or create an original
conf=/etc/httpd/conf/httpd.conf
original=/etc/httpd/conf/httpd.original
sudo cp $conf "$conf.backup"
if [ -f $original ]; then
sudo cp $original $conf
else
sudo cp $conf $original
fi
# Comment out the mpm_event module and uncomment all php modules
comment mod_mpm_event.so $conf
uncomment mod_mpm_prefork.so $conf
uncomment mod_proxy_fcgi.so $conf
uncomment mod_proxy.so $conf
DR=()
while [[ ! -d $DR || -z $DR ]]; do
read -e -p "Where would you like your DocumentRoot? [default=/srv/http]: " DR
[ -z $DR ] && DR=/srv/http
# The path is absolute, which is what we want
if [[ "$DR" = /* ]]; then
# But it doesn't exist - prompt to create it
if [ ! -d $DR ]; then
echo -n "$DR is not currently a directory. Would you like to create it? [Y/n]: "
read create
[[ "${create,,}" = y || -z $create ]] && {
mkdir -p $DR
break
}
else
break
fi
else
echo "$DR is not an absolute path"
fi
done
# Set the document root, and if no index.php file exists, create a lil hello world
sed -i "s/DocumentRoot.*\".*/DocumentRoot \"${DR//\//\\\/}\"/" $conf
[ ! -f $DR/index.php ] && echo "<?php echo 'Hello, world!'; ?>" >> $DR/index.php
sudo echo "LoadModule php7_module modules/libphp7.so
AddHandler php7-script .php
Include conf/extra/php7_module.conf
Include conf/extra/php-fpm.conf" >> $conf
# Create the php processor configuration
sudo echo "DirectoryIndex index.php index.html
<FilesMatch \.php$>
SetHandler 'proxy:unix:/run/php-fpm/php-fpm.sock|fcgi://localhost/'
</FilesMatch>" > /etc/httpd/conf/extra/php-fpm.conf
# Enable the service if it isn't yet, then start it if it's stopped or restart it if it isn't
start_service() {
sudo systemctl list-unit-files | grep -E "${1}.*enabled" > /dev/null
[ $? != 0 ] && sudo systemctl enable $1 > /dev/null
sudo systemctl status $1 > /dev/null 2>&1
if [ $? != 0 ]; then
sudo systemctl start $1
else
sudo systemctl restart $1
fi
}
start_service php-fpm
start_service httpd
# Wait for the services to boot up before displaying the information
sleep 1
echo ''
netstat -tulpn
echo ''
grep -E 'DocumentRoot.*".*' $conf
echo ''
sudo -u $USER xdg-open http://localhost >/dev/null 2>&1 &
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment