#!/bin/bash export PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin # Check if user is root if [ $(id -u) != "0" ]; then echo "Error: You must be root to run this script!" exit 1 else if [ "$SUDO_UID" != "0" ]; then acme_sh_sudo="-f" fi fi echo "+-------------------------------------------+" echo "| Manager for LNMP, Written by Licess |" echo "+-------------------------------------------+" echo "| https://lnmp.org |" echo "+-------------------------------------------+" arg1=$1 arg2=$2 lamp_start() { echo "Starting LAMP..." /etc/init.d/httpd start /etc/init.d/mysql start } lamp_stop() { echo "Stoping LAMP..." /etc/init.d/httpd stop /etc/init.d/mysql stop } lamp_reload() { echo "Reload LAMP..." /etc/init.d/httpd graceful /etc/init.d/mysql reload } lamp_kill() { echo "Kill apache,mysql process..." killall httpd killall mysqld echo "done." } lamp_status() { /etc/init.d/httpd status /etc/init.d/mysql status } Function_Vhost() { case "$1" in [aA][dD][dD]) Add_VHost ;; [lL][iI][sS][tT]) List_VHost ;; [dD][eE][lL]) Del_VHost ;; [eE][xX][iI][tT]) exit 1 ;; *) echo "Usage: lnmp vhost {add|list|del}" exit 1 ;; esac } Function_Database() { case "$1" in [aA][dD][dD]) Add_Database_Menu Add_Database ;; [lL][iI][sS][tT]) List_Database ;; [dD][eE][lL]) Del_Database ;; [eE][dD][iI][tT]) Edit_Database ;; [eE][xX][iI][tT]) exit 1 ;; *) echo "Usage: lnmp mysql {add|list|del}" exit 1 ;; esac } Function_Ftp() { case "$1" in [aA][dD][dD]) Add_Ftp_Menu Add_Ftp ;; [lL][iI][sS][tT]) List_Ftp ;; [dD][eE][lL]) Del_Ftp ;; [eE][dD][iI][tT]) Edit_Ftp ;; [eE][xX][iI][tT]) exit 1 ;; [sS][hH][oO][wW]) Show_Ftp ;; *) echo "Usage: lnmp ftp {add|list|del}" exit 1 ;; esac } Add_VHost_Config() { cat >"/usr/local/apache/conf/vhost/${domain}.conf"< ServerAdmin ${email} php_admin_value open_basedir "${vhostdir}:/tmp/:/var/tmp/:/proc/" DocumentRoot "${vhostdir}" ServerName ${domain} ErrorLog "/home/wwwlogs/${al_name}-error_log" CustomLog "/home/wwwlogs/${al_name}-access_log" combined SetOutputFilter DEFLATE Options FollowSymLinks AllowOverride All Order allow,deny Allow from all DirectoryIndex index.html index.php EOF if [ "${access_log}" != 'y' ]; then sed -i 's/^ErrorLog/#ErrorLog/g' /usr/local/apache/conf/vhost/${domain}.conf sed -i 's/^CustomLog/#CustomLog/g' /usr/local/apache/conf/vhost/${domain}.conf fi if [ "${moredomain}" != "" ]; then sed -i "/ServerName/a\ ServerAlias ${moredomain}" /usr/local/apache/conf/vhost/${domain}.conf fi echo "Test Apache configure file..." /etc/init.d/httpd configtest echo "Restart Apache..." /etc/init.d/httpd graceful } Add_VHost() { domain="" while :;do Echo_Yellow "Please enter domain(example: www.lnmp.org): " read domain if [ "${domain}" != "" ]; then if [ -f "/usr/local/apache/conf/vhost/${domain}.conf" ]; then Echo_Red " ${domain} is exist,please check!" exit 1 else echo " Your domain: ${domain}" fi break else Echo_Red "Domain name can't be empty!" fi done Echo_Yellow "Enter more domain name(example: lnmp.org *.lnmp.org): " read moredomain if [ "${moredomain}" != "" ]; then echo " domain list: ${moredomain}" fi vhostdir="/home/wwwroot/${domain}" echo "Please enter the directory for the domain: $domain" Echo_Yellow "Default directory: /home/wwwroot/${domain}: " read vhostdir if [ "${vhostdir}" == "" ]; then vhostdir="/home/wwwroot/${domain}" fi echo "Virtual Host Directory: ${vhostdir}" Echo_Yellow "Allow access log? (y/n) " read access_log if [[ "${access_log}" == "n" || "${access_log}" == "" ]]; then echo "Disable access log." al_name="${domain}" else Echo_Yellow "Enter access log filename(Default:${domain}-access_log): " read al_name if [ "${al_name}" == "" ]; then al_name="${domain}" fi echo "You access log filename: ${al_name}-access_log" fi email="" Echo_Yellow "Please enter Administrator Email Address: " read email if [ "${email}" == "" ]; then echo "Administrator Email Address will set to webmaster@example.com!" email='webmaster@example.com' else echo "Server Administrator Email:${email}" fi if [[ -s /usr/local/mysql/bin/mysql || -s /usr/local/mariadb/bin/mysql ]]; then Echo_Yellow "Create database and MySQL user with same name (y/n) " read create_database if [ "${create_database}" == "y" ]; then Verify_DB_Password Add_Database_Menu fi fi if [ -f /usr/local/pureftpd/sbin/pure-ftpd ]; then Echo_Yellow "Create ftp account (y/n) " read create_ftp if [ "${create_ftp}" == "y" ]; then Add_Ftp_Menu fi fi Echo_Yellow "Add SSL Certificate (y/n) " read create_ssl if [ "${create_ssl}" == "y" ]; then Add_SSL_Menu fi echo "" echo "Press any key to start create virtul host..." OLDCONFIG=`stty -g` stty -icanon -echo min 1 time 0 dd count=1 2>/dev/null stty ${OLDCONFIG} echo "Create Virtul Host directory......" mkdir -p ${vhostdir} echo "set permissions of Virtual Host directory......" chmod -R 755 ${vhostdir} chown -R www:www ${vhostdir} Add_VHost_Config if [ "${create_database}" == "y" ]; then Add_Database fi if [ "${create_ftp}" == "y" ]; then Add_Ftp fi if [ "${create_ssl}" == "y" ]; then Add_SSL fi Echo_Green "================================================" echo "Virtualhost infomation:" echo "Your domain: ${domain}" echo "Home Directory: ${vhostdir}" if [ "${access_log}" == "n" ]; then echo "Enable log: no" else echo "Enable log: yes" fi if [ "${create_database}" == "y" ]; then echo "Database username: ${database_name}" echo "Database userpassword: ${mysql_password}" echo "Database Name: ${database_name}" else echo "Create database: no" fi if [ "${create_ftp}" == "y" ]; then echo "FTP account name: ${ftp_account_name}" echo "FTP account password: ${ftp_account_password}" else echo "Create ftp account: no" fi if [ "${create_ssl}" == "y" ]; then echo "Enable SSL: yes" if [ "${ssl_choice}" == "1" ]; then echo " =>Certificate file" elif [ "${ssl_choice}" == "2" ]; then echo " =>Let's Encrypt" fi fi Echo_Green "================================================" } List_VHost() { echo "Apache Virtualhost list:" ls /usr/local/apache/conf/vhost/ | grep ".conf$" | sed 's/.conf//g' } Del_VHost() { echo "=======================================" echo "Current Virtualhost:" List_VHost echo "=======================================" domain="" while :;do Echo_Yellow "Please enter domain you want to delete: " read domain if [ "${domain}" == "" ]; then Echo_Red "Domain name can't be empty." else break fi done if [ ! -f "/usr/local/apache/conf/vhost/${domain}.conf" ]; then echo "==========================================" echo "Domain: ${domain} was not exist!" echo "==========================================" exit 1 else rm -f /usr/local/apache/conf/vhost/${domain}.conf echo "========================================================" echo "Domain: ${domain} has been deleted." echo "Website files will not be deleted for security reasons." echo "You need to manually delete the website files." echo "========================================================" fi } Check_DB() { if [[ -s /usr/local/mariadb/bin/mysql && -s /usr/local/mariadb/bin/mysqld_safe && -s /etc/my.cnf ]]; then MySQL_Bin="/usr/local/mariadb/bin/mysql" MySQL_Ver=`/usr/local/mariadb/bin/mysql_config --version` elif [[ -s /usr/local/mysql/bin/mysql && -s /usr/local/mysql/bin/mysqld_safe && -s /etc/my.cnf ]]; then MySQL_Bin="/usr/local/mysql/bin/mysql" MySQL_Ver=`/usr/local/mysql/bin/mysql_config --version` else MySQL_Bin="None" fi } Make_TempMycnf() { cat >~/.my.cnf</tmp/.mysql.tmp chmod 600 /tmp/.mysql.tmp Check_DB ${MySQL_Bin} --defaults-file=~/.my.cnf /tmp/.add_mysql.sql</tmp/.add_mysql.sql</tmp/.del.mysql.sql</tmp/pass${ftp_account_name}</tmp/pass${ftp_account_name}</usr/local/acme.sh/upgrade.sh</dev/null 2>&1; then yum -y update nss yum -y install ca-certificates service crond restart chkconfig crond on elif command -v apt-get >/dev/null 2>&1; then /etc/init.d/cron restart update-rc.d cron defaults fi fi . "/usr/local/acme.sh/acme.sh.env" } Add_Letsencrypt() { if [[ "${vhostdir}" == "" || "${letsdomain}" == "" ]]; then Echo_Red "Two parameters are needed!" exit 1 fi if [ ! -d "${vhostdir}" ]; then Echo_Red "${vhostdir} does not exist or is not a directory!" exit fi Install_Check_Acme.sh if [ -s /usr/local/apache/conf/ssl/${domain}/fullchain.cer ]; then echo "Removing exist domain certificate..." rm -rf /usr/local/apache/conf/ssl/${domain} fi echo "Starting create SSL Certificate use Let's Encrypt..." /usr/local/acme.sh/acme.sh ${acme_sh_sudo} --issue ${letsdomain} -w ${vhostdir} --reloadcmd "/etc/init.d/httpd graceful" lets_status=$? if [ "${lets_status}" = 0 ]; then Echo_Green "Let's Encrypt SSL Certificate create successfully." else Echo_Red "Let's Encrypt SSL Certificate create failed!" fi } Add_Buypass() { if [[ "${vhostdir}" == "" || "${letsdomain}" == "" ]]; then Echo_Red "Two parameters are needed!" exit 1 fi if [ ! -d "${vhostdir}" ]; then Echo_Red "${vhostdir} does not exist or is not a directory!" exit fi Install_Check_Acme.sh if [ -s /usr/local/nginx/conf/ssl/${domain}/fullchain.cer ]; then echo "Removing exist domain certificate..." rm -rf /usr/local/nginx/conf/ssl/${domain} fi echo "Starting register account..." /usr/local/acme.sh/acme.sh ${acme_sh_sudo} --server buypass --register-account --accountemail ${email_address} echo "Starting create SSL Certificate use Let's Encrypt..." /usr/local/acme.sh/acme.sh ${acme_sh_sudo} --server buypass --issue ${letsdomain} -w ${vhostdir} --days 170 --reloadcmd "/etc/init.d/nginx reload" lets_status=$? if [ "${lets_status}" = 0 ]; then Echo_Green "Let's Encrypt SSL Certificate create successfully." else Echo_Red "Let's Encrypt SSL Certificate create failed!" fi } Add_Zerossl() { if [[ "${vhostdir}" == "" || "${letsdomain}" == "" ]]; then Echo_Red "Two parameters are needed!" exit 1 fi if [ ! -d "${vhostdir}" ]; then Echo_Red "${vhostdir} does not exist or is not a directory!" exit fi Install_Check_Acme.sh if [ -s /usr/local/nginx/conf/ssl/${domain}/fullchain.cer ]; then echo "Removing exist domain certificate..." rm -rf /usr/local/nginx/conf/ssl/${domain} fi echo "Starting register account..." /usr/local/acme.sh/acme.sh ${acme_sh_sudo} --server zerossl --register-account --accountemail ${email_address} echo "Starting create SSL Certificate use Let's Encrypt..." /usr/local/acme.sh/acme.sh ${acme_sh_sudo} --server zerossl --issue ${letsdomain} -w ${vhostdir} --reloadcmd "/etc/init.d/nginx reload" lets_status=$? if [ "${lets_status}" = 0 ]; then Echo_Green "Let's Encrypt SSL Certificate create successfully." else Echo_Red "Let's Encrypt SSL Certificate create failed!" fi } Create_SSL_Config() { if /usr/local/apache/bin/httpd -v|grep -Eqi "Apache/2.4.*"; then Conf_H2='Protocols h2 h2c http/1.1' else Conf_H2='' fi if echo "${ssl_choice}" | grep -Eqi "^[2-4]$"; then Conf_SSLChain="SSLCertificateChainFile /usr/local/apache/conf/ssl/${domain}/ca.cer" fi sed -i 's@#Include conf/extra/httpd-ssl.conf@Include conf/extra/httpd-ssl.conf@g' /usr/local/apache/conf/httpd.conf cat >>"/usr/local/apache/conf/vhost/${domain}.conf"< ServerAdmin ${email} php_admin_value open_basedir "${vhostdir}:/tmp/:/var/tmp/:/proc/" DocumentRoot ${vhostdir} ServerName ${domain}:443 ErrorLog "/home/wwwlogs/${al_name}-error_log" CustomLog "/home/wwwlogs/${al_name}-access_log" combined SSLEngine on SSLCertificateFile ${ssl_certificate} SSLCertificateKeyFile ${ssl_certificate_key} ${Conf_SSLChain} ${Conf_H2} SetOutputFilter DEFLATE Options FollowSymLinks AllowOverride All Order allow,deny Allow from all DirectoryIndex index.html index.php EOF if [ "${access_log}" != 'y' ]; then sed -i 's/^ErrorLog/#ErrorLog/g' /usr/local/apache/conf/vhost/${domain}.conf sed -i 's/^CustomLog/#CustomLog/g' /usr/local/apache/conf/vhost/${domain}.conf fi if [ "${moredomain}" != "" ]; then sed -i "/ServerName/a\ ServerAlias ${moredomain}" /usr/local/apache/conf/vhost/${domain}.conf fi echo "Test Apache configure file..." /etc/init.d/httpd configtest echo "Restart Apache..." /etc/init.d/httpd graceful } Add_SSL() { if [ "${ssl_choice}" == "1" ]; then Create_SSL_Config elif echo "${ssl_choice}" | grep -Eqi "^[2-4]$"; then letsdomain="" if [ "${moredomain}" != "" ]; then letsdomain="-d ${domain}" for i in ${moredomain};do letsdomain=${letsdomain}" -d ${i}" done else letsdomain="-d ${domain}" fi if [ ! -s "/usr/local/apache/conf/vhost/${domain}.conf" ]; then Add_VHost_Config fi if [ ! -d "${vhostdir}" ]; then mkdir -p "${vhostdir}" fi if [ "${ssl_choice}" == "2" ]; then Add_Letsencrypt elif [ "${ssl_choice}" == "3" ]; then Add_Buypass elif [ "${ssl_choice}" == "4" ]; then Add_Zerossl fi ssl_certificate="/usr/local/apache/conf/ssl/${domain}/${domain}.cer" ssl_certificate_key="/usr/local/apache/conf/ssl/${domain}/${domain}.key" if [ "${lets_status}" = 0 ]; then Create_SSL_Config fi fi } Add_Dns_SSL() { provider=$1 if [ "${provider}" != "" ]; then dns_provider="dns_${provider}" else Echo_Red "The dns manual mode can not renew automatically, you must renew it manually." fi Install_Check_Acme.sh if [[ ! -s /usr/local/acme.sh/dnsapi/dns_${provider}.sh && "${provider}" != "" ]]; then echo "DNS Provider: ${provider} not found." exit 1 fi Add_SSL_Info_Menu if [ -s /usr/local/apache/conf/ssl/${domain}/fullchain.cer ]; then echo "Removing exist domain certificate..." rm -rf /usr/local/apache/conf/ssl/${domain} fi letsdomain="" if [ "${moredomain}" != "" ]; then letsdomain="-d ${domain}" for i in ${moredomain};do letsdomain=${letsdomain}" -d ${i}" done else letsdomain="-d ${domain}" fi if echo "${letsdomain}" | grep -q '\*\.' && echo "${letsdomain}" | grep -qi 'www\.'; then Echo_Red "wildcard SSL certificate DO NOT allow add www. subdomain." exit 1 fi echo "Starting create SSL Certificate use Let's Encrypt..." if [ "${provider}" != "" ]; then /usr/local/acme.sh/acme.sh ${acme_sh_sudo} --issue ${letsdomain} --dns ${dns_provider} --reloadcmd "/etc/init.d/httpd graceful" lets_status=$? else /usr/local/acme.sh/acme.sh ${acme_sh_sudo} --issue ${letsdomain} --dns --yes-I-know-dns-manual-mode-enough-go-ahead-please Echo_Yellow "Please add the above TXT record to the domain in 120 seconds!!!" echo Sleep_Sec 120 /usr/local/acme.sh/acme.sh ${acme_sh_sudo} --renew ${letsdomain} --yes-I-know-dns-manual-mode-enough-go-ahead-please lets_status=$? fi ssl_choice="2" if [ "${lets_status}" = 0 ] || [[ "${provider}" = "" && "${lets_status}" = 1 && -s "/usr/local/apache/conf/ssl/${domain}/${domain}.cer" ]]; then if [ ! -d "${vhostdir}" ]; then echo "Create Virtul Host directory......" mkdir -p ${vhostdir} echo "set permissions of Virtual Host directory......" chmod -R 755 ${vhostdir} chown -R www:www ${vhostdir} fi if [ ! -s "/usr/local/apache/conf/vhost/${domain}.conf" ]; then Add_VHost_Config fi ssl_certificate="/usr/local/apache/conf/ssl/${domain}/${domain}.cer" ssl_certificate_key="/usr/local/apache/conf/ssl/${domain}/${domain}.key" Create_SSL_Config Echo_Green "Let's Encrypt SSL Certificate create successfully." else Echo_Red "Let's Encrypt SSL Certificate create failed!" fi } Add_Dns_SSL_Only() { provider=$1 if [ "${provider}" != "" ]; then dns_provider="dns_${provider}" else Echo_Red "The dns manual mode can not renew automatically, you must renew it manually." fi Install_Check_Acme.sh if [[ ! -s /usr/local/acme.sh/dnsapi/dns_${provider}.sh && "${provider}" != "" ]]; then echo "DNS Provider: ${provider} not found." exit 1 fi Add_DNS_SSL_Only_Info_Menu if [ -s /usr/local/apache/conf/ssl/${domain}/fullchain.cer ]; then echo "Removing exist domain certificate..." rm -rf /usr/local/apache/conf/ssl/${domain} fi letsdomain="" if [ "${moredomain}" != "" ]; then letsdomain="-d ${domain}" for i in ${moredomain};do letsdomain=${letsdomain}" -d ${i}" done else letsdomain="-d ${domain}" fi if echo "${letsdomain}" | grep -q '\*\.' && echo "${letsdomain}" | grep -qi 'www\.'; then Echo_Red "wildcard SSL certificate DO NOT allow add www. subdomain." exit 1 fi echo "Starting create SSL Certificate use Let's Encrypt..." if [ "${provider}" != "" ]; then /usr/local/acme.sh/acme.sh ${acme_sh_sudo} --issue ${letsdomain} --dns ${dns_provider} --reloadcmd "/etc/init.d/httpd graceful" lets_status=$? else /usr/local/acme.sh/acme.sh ${acme_sh_sudo} --issue ${letsdomain} --dns --yes-I-know-dns-manual-mode-enough-go-ahead-please Echo_Yellow "Please add the above TXT record to the domain in 120 seconds!!!" echo Sleep_Sec 120 /usr/local/acme.sh/acme.sh ${acme_sh_sudo} --renew ${letsdomain} --yes-I-know-dns-manual-mode-enough-go-ahead-please lets_status=$? fi if [ "${lets_status}" = 0 ] || [[ "${provider}" = "" && "${lets_status}" = 1 && -s "/usr/local/apache/conf/ssl/${domain}/fullchain.cer" ]]; then Echo_Blue "------------------ SSL Certificate information as follows ------------------" Echo_Blue "| Domain: ${domain} ${moredomain}" Echo_Blue "| SSL Certificate: /usr/local/apache/conf/ssl/${domain}/fullchain.cer" Echo_Blue "| SSL Certificate Key: /usr/local/apache/conf/ssl/${domain}/${domain}.key" Echo_Blue "------------------------------------ ---------------------------------------" Echo_Green "Let's Encrypt SSL Certificate create successfully." else Echo_Red "Let's Encrypt SSL Certificate create failed!" fi } Color_Text() { echo -e " \e[0;$2m$1\e[0m" } Echo_Red() { echo $(Color_Text "$1" "31") } Echo_Green() { echo $(Color_Text "$1" "32") } Echo_Yellow() { echo -n $(Color_Text "$1" "33") } Echo_Blue() { echo $(Color_Text "$1" "34") } Sleep_Sec() { seconds=$1 while [ "${seconds}" -ge "0" ];do echo -ne "\r \r" echo -n ${seconds} seconds=$(($seconds - 1)) sleep 1 done echo -ne "\r" } Check_DB case "${arg1}" in start) lamp_start ;; stop) lamp_stop ;; restart) lamp_stop lamp_start ;; reload) lamp_reload ;; kill) lamp_kill ;; status) lamp_status ;; mysql) /etc/init.d/mysql ${arg2} ;; mariadb) /etc/init.d/mariadb ${arg2} ;; pureftpd) /etc/init.d/pureftpd ${arg2} ;; httpd) /etc/init.d/httpd ${arg2} ;; vhost) Function_Vhost ${arg2} ;; database) Verify_DB_Password Function_Database ${arg2} TempMycnf_Clean ;; ftp) Check_Pureftpd Function_Ftp ${arg2} ;; ssl) info="n" Add_SSL_Menu Add_SSL ;; dnsssl|dns) Add_Dns_SSL ${arg2} ;; onlyssl) Add_Dns_SSL_Only ${arg2} ;; *) echo "Usage: lnmp {start|stop|reload|restart|kill|status}" echo "Usage: lnmp {httpd|mysql|mariadb|pureftpd} {start|stop|reload|restart|kill|status}" echo "Usage: lnmp vhost {add|list|del}" echo "Usage: lnmp database {add|list|edit|del}" echo "Usage: lnmp ftp {add|list|edit|del|show}" echo "Usage: lnmp ssl add" echo "Usage: lnmp {dnsssl|dns} {cx|ali|cf|dp|he|gd|aws}" echo "Usage: lnmp onlyssl {cx|ali|cf|dp|he|gd|aws}" ;; esac exit