โดยที่ไฟล์เว็บไซต์จะอยู่ที่
/home/user1/public_html
/home/user2/public_html
ให้เข้าไปแก้ไขไฟล์ /etc/apache2/sites-available/000-default.conf
โดยแก้ไขไฟล์ประมาณด้านล่าง
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 |
<VirtualHost *:80> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. #ServerName example.com ServerAdmin webmaster@localhost #เอาใว้เผื่อเข้าผ่าน IP เช่น x.xxx.xxx.x/user1 Alias /user1 /home/user1/public_html Alias /user2 /home/user2/public_html DocumentRoot /var/www/html # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf </VirtualHost> <VirtualHost *:80> ServerName user1.mydomain.com #ServerAlias sub.mydomain.com DocumentRoot /home/user1/public_html ErrorLog /home/user1/log/error.log CustomLog /home/user1/log/access.log combined </VirtualHost> <VirtualHost *:80> ServerName user2.mydomain.com #ServerAlias sub.mydomain.com DocumentRoot /home/user2/public_html ErrorLog /home/user2/log/error.log CustomLog /home/user2/log/access.log combined #https RewriteEngine on RewriteCond %{SERVER_NAME} =user2.mydomain.com RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] </VirtualHost> # vim: syntax=apache ts=4 sw=4 sts=4 sr noet |
จากนั้นเข้าไปแก้ไขไฟล์ /etc/apache2/apache2.conf โดยเพิ่มโค๊ดด้านล่างเข้าไป
1 2 3 4 5 |
<Directory /home/> Options FollowSymLinks AllowOverride None Require all granted </Directory> |
จากนั้นเข้าไปเพิ่มในไฟล์ /etc/hosts
1 2 3 |
127.0.0.1 localhost x.xxx.xxx.x user1.mydomain.com user1 x.xxx.xxx.x user2.mydomain.com user2 |
*ตัวอย่างที่ใช้บน ec2
1 2 3 4 5 6 7 8 9 10 11 12 |
127.0.0.1 localhost localhost.localdomain mydomain.com 127.0.0.1 localhost xxx.xx.xx.xxx user1.mydomain.com user1 xxx.xx.xx.xxx user2.mydomain.com user2 # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ff02::3 ip6-allhosts |
*xxx.xx.xx.xxx คือ Private IPs
จากนั้นให้ไป Edit DNS Zone file
1 |
user1.mydomain.com | A| IPv4_Public_IP |
ดูเพิ่มเติม
https://stackoverflow.com/questions/13837978/apache-multiple-sub-domains-with-one-ip-address
https://stackoverflow.com/questions/45656861/apache2-creating-virtual-hosts-on-one-ip-address
ป้ายกำกับ:Linux, Ubuntu, VirtualHost