Skip to content

Instantly share code, notes, and snippets.

@natanfelles
Last active November 20, 2018 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natanfelles/903498837e82669b3285c31b79bae7ec to your computer and use it in GitHub Desktop.
Save natanfelles/903498837e82669b3285c31b79bae7ec to your computer and use it in GitHub Desktop.
Easy LEMP Management
#!/bin/bash
# Author: Natan Felles <natanfelles@gmail.com>
# Description: Easy LEMP Management
# Local: /usr/local/bin/lemp
# Foreground colors
declare -A COLOR
COLOR[default]=$(tput sgr0)
COLOR[red]=$(tput setaf 1)
COLOR[green]=$(tput setaf 2)
COLOR[yellow]=$(tput setaf 3)
COLOR[blue]=$(tput setaf 4)
# Check if is super-user
if [ $(id -u) != '0' ]; then
echo "${COLOR[red]}You must be root to run this script.${COLOR[default]}";
exit 1;
fi
# PHP Version used by php-fpm service and nginx vhost
PHP_VERSON=7.3
# Root path where domain folders wil be created
ROOT=/home/{USER}/Websites/ #/var/www/{USER}
# LEMP Services
SERVICES=("nginx" "php${PHP_VERSON}-fpm" "mysql" "memcached")
# Get Website owner username
user_func(){
echo -n "${COLOR[green]}Website owner username:${COLOR[default]} "
read USER
if getent passwd $USER > /dev/null 2>&1; then
ROOT=${ROOT/\{USER\}/$USER}
else
echo "${COLOR[red]}User ${USER} does not exists.${COLOR[default]}"
exit
fi
}
# Enable Services
enable_func(){
for S in "${SERVICES[@]}"; do
echo "${COLOR[green]}Enabling ${S}:${COLOR[default]} "
systemctl enable ${S}.service
done
}
# Disable Services
disable_func(){
for S in "${SERVICES[@]}"; do
echo "${COLOR[green]}Disabling ${S}:${COLOR[default]} "
systemctl disable ${S}.service
done
}
# Start Services
start_func(){
for S in "${SERVICES[@]}"; do
#systemctl start ${S}.service
/etc/init.d/${S} start
done
}
# Stop Services
stop_func(){
for S in "${SERVICES[@]}"; do
#systemctl stop ${S}.service
/etc/init.d/${S} stop
done
}
# Restart Services
restart_func(){
for S in "${SERVICES[@]}"; do
#systemctl restart ${S}.service
/etc/init.d/${S} restart
done
}
# Add Virtual Host
add_func(){
user_func
echo -n "${COLOR[green]}Domain to Add:${COLOR[default]} "
read DOMAIN
FILE="/etc/nginx/sites-available/${DOMAIN}.conf"
if [ -f "${FILE}" ]; then
echo "${COLOR[red]}This domain already exists.${COLOR[default]}"
exit
else
BASE=${ROOT}${DOMAIN}/
mkdir -p ${BASE}
PUBLIC=${BASE}public/
mkdir -p ${PUBLIC}
INDEX="${PUBLIC}index.php"
touch ${INDEX}
echo "<?= \"${DOMAIN} is live!\";" > ${INDEX}
chown -R ${USER}:${USER} ${BASE}
CONFIG="
server {
## Default port
listen 80;
## SSL configuration
listen 443 ssl;
include snippets/snakeoil.conf;
## Show folders contents
autoindex on;
## Public filesystem Path
root ${PUBLIC};
## Index files
index index.php index.html;
## Domains
server_name ${DOMAIN} www.${DOMAIN};
## Enable URL Rewrite in Web Root
location / {
try_files \$uri \$uri/ /index.php?\$args;
}
## Pass the PHP scripts to PHP-FPM Socket
location ~ \\.php\$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php${PHP_VERSON}-fpm.sock;
}
## Deny access to hidden files
location ~ /\\. {
deny all;
}
## Permit files to be accessed from other domains
# add_header Access-Control-Allow-Origin \"*\"; # Can be a domain
## Cache
# location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
# add_header Cache-Control \"public\";
# add_header X-Frame-Options \"SAMEORIGIN\";
# expires +1y;
# }
## Serve static files directly
# location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
# access_log off;
# }
## Force SSL
# if (\$scheme != \"https\") {
# rewrite ^ https://${DOMAIN}\$request_uri? permanent;
# }
# Force www
# if (\$http_host != \"www.${DOMAIN}\") {
# rewrite ^ \$scheme://www.${DOMAIN}\$request_uri? permanent;
# }
## Disable log and allow robots
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
}
"
touch ${FILE}
echo "${CONFIG}" > ${FILE}
ln -s ${FILE} /etc/nginx/sites-enabled/${DOMAIN}.conf
/etc/init.d/nginx restart >> /dev/null
echo ""
echo "${COLOR[blue]}Info:${COLOR[default]}"
echo "Add the following line in /etc/hosts:"
echo "${COLOR[yellow]}127.0.0.1 ${DOMAIN} www.${DOMAIN}${COLOR[default]}"
echo "After you can access your site in http://${DOMAIN}"
echo "The local path is ${BASE}"
fi
}
delete_func(){
echo -n "${COLOR[green]}Domain to Delete:${COLOR[default]} "
read DOMAIN
rm "/etc/nginx/sites-available/${DOMAIN}.conf"
rm "/etc/nginx/sites-enabled/${DOMAIN}.conf"
/etc/init.d/nginx restart >> /dev/null
echo ""
echo "${COLOR[blue]}Info:${COLOR[default]}"
echo "Your web files was not removed. Just the vhost config."
echo "Delete the following line in /etc/hosts:"
echo "${COLOR[yellow]}127.0.0.1 ${DOMAIN} www.${DOMAIN}${COLOR[default]}"
}
disable_vhost_func(){
echo -n "${COLOR[green]}Domain to Disable:${COLOR[default]} "
read DOMAIN
rm "/etc/nginx/sites-enabled/${DOMAIN}.conf"
/etc/init.d/nginx restart >> /dev/null
echo ""
echo "${COLOR[blue]}Info:${COLOR[default]}"
echo "Virtual Host was disabled."
}
enable_vhost_func(){
echo -n "${COLOR[green]}Domain to Enable:${COLOR[default]} "
read DOMAIN
ln -s "/etc/nginx/sites-available/${DOMAIN}.conf" /etc/nginx/sites-enabled/${DOMAIN}.conf
/etc/init.d/nginx restart >> /dev/null
echo ""
echo "${COLOR[blue]}Info:${COLOR[default]}"
echo "Virtual Host was enabled."
}
list_vhosts_func(){
echo "${COLOR[green]}Virtual Hosts:${COLOR[default]}"
# TODO: Show just one list saying if is enabled [E] or disabled [D]
ENABLED=$(ls -1 /etc/nginx/sites-enabled/)
AVAILABLE=$(ls -1 /etc/nginx/sites-available/)
echo "${COLOR[blue]}Enabled:${COLOR[default]}"
echo $ENABLED
echo "${COLOR[blue]}Available:${COLOR[default]}"
echo $AVAILABLE
}
info_func(){
echo -n "${COLOR[green]}Linux: ${COLOR[default]}"
uname -a
echo -n "${COLOR[green]}Nginx: ${COLOR[default]}"
nginx -v
echo -n "${COLOR[green]}MySQL: ${COLOR[default]}"
mysql -V
echo -n "${COLOR[green]}Memcached: ${COLOR[default]}"
memcached -V
echo -n "${COLOR[green]}PHP: ${COLOR[default]}"
php -v
}
help_func(){
echo ""
echo "${COLOR[green]}Easy LEMP Management${COLOR[default]}"
echo ""
echo "Commands"
echo " ${COLOR[yellow]}help${COLOR[default]} Show this informations."
echo " ${COLOR[yellow]}info${COLOR[default]} Show informations about the LEMP server."
echo ""
echo "Services"
echo " ${COLOR[yellow]}start${COLOR[default]} Start LEMP services."
echo " ${COLOR[yellow]}status${COLOR[default]} Show LEMP services status."
echo " ${COLOR[yellow]}stop${COLOR[default]} Stop LEMP services."
echo " ${COLOR[yellow]}restart${COLOR[default]} Restart LEMP services."
echo " ${COLOR[yellow]}enable${COLOR[default]} Enable all LEMP services on boot."
echo " ${COLOR[yellow]}disable${COLOR[default]} Disable all LEMP services on boot."
echo ""
echo "Virtual Hosts"
echo " ${COLOR[yellow]}add${COLOR[default]} or ${COLOR[blue]}av${COLOR[default]} Add a new Virtual Host."
echo " ${COLOR[yellow]}delete${COLOR[default]} or ${COLOR[blue]}dv${COLOR[default]} Delete a new Virtual Host."
echo " ${COLOR[yellow]}list${COLOR[default]} or ${COLOR[blue]}lv${COLOR[default]} List all Virtual Hosts."
echo " ${COLOR[yellow]}enable_vhost${COLOR[default]} or ${COLOR[blue]}ev${COLOR[default]} Enable a Virtual Host."
echo " ${COLOR[yellow]}disable_vhost${COLOR[default]} or ${COLOR[blue]}dv${COLOR[default]} Disable a Virtual Host."
echo ""
}
case "$1" in
start )
start_func
;;
stop )
stop_func
;;
restart )
restart_func
;;
add )
add_func
;;
delete )
delete_func
;;
info )
info_func
;;
enable )
enable_func
;;
disable )
disable_func
;;
list )
list_vhosts_func
;;
enable_vhost )
enable_vhost_func
;;
disable_vhost )
disable_vhost_func
;;
help )
help_func
;;
* )
help_func
#echo "Options: {start|stop|restart|add|delete|info|enable|disable}";
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment