⬆️ ⬇️

Quickly create Apache virtual hosts using the bash script

Not so long ago, I moved from a VPS to a dedicated server, and there was an acute problem for me about transferring sites from an old server to a new one, namely the rapid creation of virtual hosts and databases. Of course, the ISPmanager control panel went to the server in the appendage, but in this case I didn’t like two things:



By virtue of this, I decided to adjust everything with pens. About the installation of Apache and php, I will not write, as well as on Habré and on the Internet a lot of materials on this topic. We are more interested in the rapid creation of a user, a virtual host, and a database. Who cares please in the tackle.



Logic decided to split into two scripts.



Some conditions for scripts.



Script to create a new virtual host (/ home / addsite)

#!/bin/bash IP_ADDRESS="1.2.3.4" APACHE2_DIR="/etc/apache2" UID_ROOT=0 if [ "$UID" -ne "$UID_ROOT" ]; then echo "$0 - Requires root privileges" exit 1 fi function is_user(){ local check_user="$1"; grep "$check_user:" /etc/passwd >/dev/null if [ $? -ne 0 ]; then #echo "NOT HAVE USER" return 0 else #echo "HAVE USER" return 1 fi } function generate_pass(){ CHARS="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()-_=+\\|/" LENGTH="8" while [ "${n:=1}" -le "$LENGTH" ] ; do PASSWORD="$PASSWORD${CHARS:$(($RANDOM%${#CHARS})):1}" let n+=1 done echo $PASSWORD } function is_yes(){ #TODO - add check 3-rd parameter for set default ansver (if press enter) while true do echo -n "Yes or No[Y/n]:" read x if [ -z "$x" ] then return 0; #defaul answer: Yes fi case "$x" in y |Y |yes | | | ) return 0;; n |N |no | | | ) return 1;; # * ) ; # asc again esac done } function create_user(){ local login="$1" local password="$2" `useradd -m -s /bin/bash $login` #set password echo -e "$password\n$password\n" | passwd $login >> /dev/null } USER_NAME=$1 echo -n "Check user name $USER_NAME: " if( is_user "$USER_NAME" )then USER_PASSWORD="$(generate_pass)" echo "-----------------------------------" echo "User name : $USER_NAME" echo "User password: $USER_PASSWORD" echo "-----------------------------------" echo -n "Continue? " if(! is_yes) then exit; fi echo "--- create user ---" create_user "$USER_NAME" "$USER_PASSWORD" fi if [ $# -eq 2 ]; then if [ "$2" != "delete" ]; then SITE_NAME=$2 mkdir /home/$USER_NAME/$SITE_NAME mkdir /home/$USER_NAME/$SITE_NAME/www mkdir /home/$USER_NAME/$SITE_NAME/logs mkdir /home/$USER_NAME/$SITE_NAME/tmp mkdir /home/$USER_NAME/$SITE_NAME/cgi-bin hostConf=" <VirtualHost ${IP_ADDRESS}:80> ServerName $SITE_NAME ServerAlias www.$SITE_NAME ServerAdmin webmaster@$SITE_NAME AddDefaultCharset utf-8 AssignUserID ${USER_NAME} ${USER_NAME} DocumentRoot /home/$USER_NAME/$SITE_NAME/www CustomLog log combined ErrorLog /home/$USER_NAME/$SITE_NAME/logs/error.log DirectoryIndex index.php index.html ScriptAlias /cgi-bin/ /home/$USER_NAME/$SITE_NAME/cgi-bin <FilesMatch \"\\.ph(p[3-5]?|tml)$\"> SetHandler application/x-httpd-php </FilesMatch> <FilesMatch \"\\.phps$\"> SetHandler application/x-httpd-php-source </FilesMatch> php_admin_value upload_tmp_dir "/home/$USER_NAME/$SITE_NAME/tmp" php_admin_value session.save_path "/home/$USER_NAME/$SITE_NAME/tmp" php_admin_value open_basedir "/home/$USER_NAME/$SITE_NAME/www:." </VirtualHost> <Directory /home/$USER_NAME/$SITE_NAME/www> Options +Includes +ExecCGI php_admin_flag engine on </Directory> " touch ${APACHE2_DIR}/vhosts/${SITE_NAME}.conf echo "$hostConf" >> ${APACHE2_DIR}/vhosts/${SITE_NAME}.conf touch //home/$USER_NAME/$SITE_NAME/www/index.php echo "<?php phpinfo() ?>" >> /home/$USER_NAME/$SITE_NAME/www/index.php chown $USER_NAME:$USER_NAME /home/$USER_NAME/$SITE_NAME/* service apache2 restart fi fi; #display information echo "*****************************************" echo "* Profit!" echo "*****************************************" 


In general, nothing complicated, right at the beginning we set the ip address of the server and the folder where we have the Apache settings. Do not forget to add permissions to the file

 chmod -x /home/addsite 


In order for Apache to pick up our configs at the end of the main configuration file we add

 Include vhosts/ 


Run the script just

 /home/addsite user_name site.ru 


The script will create a user, a virtual host and restart Apache. And of course, do not forget to show the password for the newly created user.

Database creation. I was a bit bothered by the creation of a database from phpMyAdmin, you must first create a database, then a user, and still remember to add the privileges of the database to the new user, so we simplify our life (/ home / addbd).

 #!/bin/bash MYSQL_PASS="derev123blog" UID_ROOT=0 if [ "$UID" -ne "$UID_ROOT" ]; then echo "$0 - Requires root privileges" exit 1 fi function generate_pass(){ CHARS="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()-_=+\\|/" LENGTH="8" while [ "${n:=1}" -le "$LENGTH" ] ; do PASSWORD="$PASSWORD${CHARS:$(($RANDOM%${#CHARS})):1}" let n+=1 done echo $PASSWORD } function is_running(){ local result="$(ps -A|grep $1|wc -l)" if [[ $result -eq 0 ]]; then return 1 else return 0 fi } if [ $# -eq 1 ]; then echo -n "Check MySQL status: " if(is_running mysqld)then echo "OK [Running]"; DB_NAME=$1 DB_PASSWORD="$(generate_pass)" mysql -uroot -p${MYSQL_PASS} --execute="create database ${DB_NAME};" mysql -uroot -p${MYSQL_PASS} --execute="GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_NAME}'@'localhost' IDENTIFIED by '${DB_PASSWORD}' WITH GRANT OPTION;" else echo "Error: need start mysql daemon!" exit fi fi; #display information echo "*****************************************" echo "* Data base name: ${DB_NAME}" echo "* Data base user: ${DB_NAME}" echo "* User password: ${DB_PASSWORD}" echo "* Profit!" echo "*****************************************" 


At the very beginning of the script we set the password for the root user from MySQL. Run the command

 /home/addsite bd_name 


The database and user will be created and the connection data will be displayed.

You can also add both files to the / bin directory for quick access to these commands.

 cp /home/addsite /bin/addsite cp /home/addbd /bin/addbd 


It seems like everyone else. I hope this way of creating virtual hosts will simplify the lives of users as well as me.


')

Source: https://habr.com/ru/post/209962/



All Articles