Ads Top

Configuring VirtualHosts in XAMPP on Mac

Kalian tahu apa itu virtual Host. Virtual Host adalah alamat website / URL dalam 1 mesin atau 1 IP yang bisa diakses lewat browser. Nah dalam hal ini saya akan berikan cara mengatur virtualhost pada komputer local kalian menggunakan xampp pada Macbook. cekidot artikel dibawah ini yang saya kutip dari sumber aslinya. selanjutnya akan saya tulis membuat virtual host menggunakan xampp pada windows. Jadi nikamati saja dulu yang ini.

What are VirtualHosts?

VirtualHosts allow Apache to map a hostname to a directory on the filesystem. You can set up as many VirtualHosts as you need, so that each website operates under its own hostname. For example, you might want to map mysite.local to/Users/yourusername/mysite. To test your development site all you would need to do is plug “http://mysite.local” into your browser’s address bar.

Enable VirtualHosts

The first thing you’ll need to do is open the file/Applications/XAMPP/xamppfiles/etc/httpd.conf in your favourite text editor. Look for the following lines:
# Virtual hosts
#Include /Applications/XAMPP/etc/extra/httpd-vhosts.conf
Uncomment the second line by removing the hash (#), so that Apache loads your custom VirtualHosts configuration file:
# Virtual hosts
Include /Applications/XAMPP/etc/extra/httpd-vhosts.conf

Create your VirtualHosts

Open the file /Applications/XAMPP/xamppfiles/etc/extra/httpd-vhosts.conf. Towards the bottom of the file you will see some example VirtualHosts, which you should comment out or delete.
At the bottom of the file, add ‘localhost’ as the default named VirtualHost:
# localhost

    ServerName localhost
    DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs"
    
        Options Indexes FollowSymLinks Includes execCGI
        AllowOverride All
        Require all granted
    
This step is necessary to ensure that http://localhost still points at XAMPP’s htdocsdirectory once we’ve created our custom VirtualHosts. Personally I don’t use thehtdocs directory a lot, but occasionally it’s useful to have somewhere to perform quick tests.
Now you are ready to create your own VirtualHosts. After the default localhost that you just created, add:
# My custom host

    ServerName mysite.local
    DocumentRoot "/Users/yourusername/path/to/your/site"
    
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    
    ErrorLog "logs/mysite.local-error_log"
In the above example you should replace “mysite.local” with your own hostname. This can be anything you wish, but make sure you choose a hostname that won’t conflict with a real domain name. Using a .local extension makes it obvious that the site is hosted locally rather than on a public web server.
The path to your website can point at any folder in your OS X user directory. I store most of my sites inside of Dropbox so that I can access them on both my home and work machines. If your path includes spaces, make sure you enclose it in quotes, like in my example.

Edit your hosts file

Once you’ve saved your httpd.conf and httpd-vhosts.conf files, the next step is to edit your OS X hosts file so it knows how to handle your new ServerName. The hosts file is used by OS X to map hostnames to IP addresses. In this case we want to map your new ServerName to the IP address 127.0.0.1, which is your localhost.
Fire up a Terminal instance, and at the prompt type the following command:
sudo nano /etc/hosts
Enter your OS X password when prompted, and the hosts file will be opened in thenano text editor. You’ll see that the hosts file already contains some default hostname mappings (e.g. “127.0.0.1 localhost”). Use your keyboard’s arrow keys to navigate to the bottom of the file and add your own mapping:
# XAMPP VirtualHost mappings
127.0.0.1 mysite.local
Save the host file using the key combo control+o, and pressing return when prompted to choose the filename. Close the file using control+x.

Restart Apache

So that your changes take effect, restart Apache. This can be done using XAMPP Control, which is found at /Applications/XAMPP/XAMPP Control.app.
Point your browser at http://mysite.local (or whatever ServerName you chose) and you should see your website. However, there’s a chance that instead you’ll be faced with a…

No comments:

Powered by Blogger.