I have a PHP project named sample in my localhost and can be runned using the url localhost/workspace/sample. How can I run the same by setting up a local domain say example.com. I am using LAMP.
I solved the problem without changing domain name of localhost.
The steps done are:
1) Add hostname in /etc/hosts file
Eg: 127.0.0.1 example.com
2) Edit /etc/apache2/httpd.conf file and add the following lines.NameVirtualHost *:80
<VirtualHost *:80>
ServerName localhost
DocumentRoot /var/www
<VirtualHost *:80>
ServerName example.com
DocumentRoot "/home/user/workspace/sample"
<Directory /home/user/workspace/sample>
Options Indexes FollowSymLinks Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
3) Create a file with your domain name say, example.com in /etc/apache2/sites-available folder with the following lines.
<VirtualHost *:80>
ServerName example.com
DocumentRoot /home/user/workspace/sample
<Directory /home/user/workspace/sample>
Options Indexes FollowSymLinks Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
4) Restart apache server
sudo nano /etc/hosts
add the line:
127.0.0.1 example.com
press ctrl+x
, y
to save, then enter
this will make all requests to example.com
point to 127.0.0.1
(which is localhost
), so if you want to make this work across a network (or from a virtual machine) just repeat the steps above, though use the host machines ip address in place of 127.0.0.1
You want to look into changing your /etc/hosts file to point to your localhost.
Essentially you will capture the request before it has a chance to go to DNS and it will redirect to your localhost.
There are lots of good examples on Google.
If you want to access the site from another computer, you can use the IP address of the server. The bash command to do this is ifconfig
or ip addr
If your local address is, for example, 192.168.0.3
, then you can access the site via 192.168.0.3/workspace/sample
.