
ผมเช่า cloud vps ใช้งานอยู่ ต้อง set web app ที่เป็น sub domain ให้ลูกค้าอยู่บ่อย เช่น cus1.bosscarsoft.com
เลยค้นหา วิธีการง่ายๆ สร้างโดยใช้ shell script ช่วยลดเวลาได้เยอะ เลยครับ
(server ใคร cPanel หรือ directadmin ก็ใช้ของที่มีนะครับ ของผมไม่ได้ลงเลยต้องทำเองครับ)
1.สร้างไฟล์ createSubDomain.sh
sudo nano createSubDomain.sh
2.copy code ต่อไปนี้ ลงไฟล์
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
#!/bin/bash #This script can be used to create a subdomain on your Ubuntu Server extended with apache2 echo "Welcome, do you want to create a subdomain? [Y/N]" #Read Y or N read create #Check if the user really wants to create a subdomain if [ $create == "Y" ] || [ $create == "y" ]; then echo "I'll guide you through it!" #The user wants to create a subdomain #Ask for the subdomain, without newline echo -n "What subdomain do you want to register? (ex. sub.domain.com) > " read subDomain #Ask for the directory echo -n "What directory should be used to register the subdomain? (ex. /var/www/$subDomain/public_html) > " read subDirectory #Ask for email address echo -n "What is your email address (ex. me@daanlenaerts.com) > " read email #Show all details, and ask for confirmation echo "The subdomain $subDomain will be created in $subDirectory." echo "Proceed? [Y/N]" #Check if the user wants to go further read further if [ $further == "Y" ] || [ $further == "y" ]; then #Go ahead echo "- starting configuration" #Create directory echo "- creating directory" sudo mkdir -p $subDirectory echo "- directory created" #Create index file echo "- creating index file" indexFileLocation=$subDirectory"/index.html" #Create the file sudo touch $indexFileLocation sudo sh -c "echo 'Welcome to $subDirectory' >> $indexFileLocation" echo "- index file created" #set permissions for the folder echo "- setting permissions" sudo chmod -R 757 $subDirectory echo "- permissions set" #create new virtualHost file echo "- creating VirtualHost file" sudo touch "/etc/apache2/sites-available/"$subDomain".conf" #Populating file sudo sh -c "echo '<VirtualHost *:80>\nServerName $subDomain\nServeradmin $email\nDocumentRoot $subDirectory\nErrorLog \${APACHE_LOG_DIR}/error.log\nCustomLog \${APACHE_LOG_DIR}/access.log combined\n</VirtualHost>' >> /etc/apache2/sites-available/"$subDomain".conf" echo "- VirtualHost file created" echo "- enabling VirtualHost" sudo a2ensite $subDomain".conf" echo "- VirtualHost file enabled" echo "- Restarting Apache" sudo service apache2 restart echo "- done" else #Exit echo "Okay, bye!" exit fi else #The user doesn't want to create a subdomain echo "Okay, bye!" exit fi |
3. run ไฟล์
bash createSubDomain.sh
ผมไม่ได้เป็นคนเขียน script เองนะครับ เอามาจาก อ้างอิงด้านล่างครับ ใช้ดีเลยบอกต่อครับ