Does anyone know how to change the document root of the Apache server? I basically want localhost
to come from /users/spencer/projects
directory instead of /var/www
.
Edit
I ended up figuring it out. Some suggested I change the httpd.conf
file, but I ended up finding a file in /etc/apache2/sites-available/default
and changed the root directory from /var/www
to /home/myusername/projects_folder
and that worked.
The right way to change directory or run from multiple directories under different port for apache2 is as follows:
For apache2 the configuration files are located under
/etc/apache2
and doesnt use a single configuration file as in older versions but is split into smaller configuration files, with/etc/apache2/apache2.conf
being the main configuration file. To serve files from a different directory we need a new virtualhost conf file. The virtualhost conf files are located in/etc/apache2/sites-available
(do not edit files within sites-enabled). The default apache installation uses virtualhost conf file000-default.conf
.Start by creating a new virtualhost file by copying the default virtualhost file used by the default installation of apache(the one that runs at localhost on port 80). Change into directory
/etc/apache2/sites-available
and then make copy bysudo cp 000-default.conf example.com.conf
, now edit the file bysudo gedit example.com.conf
to:I have deleted the non important lines from the above file for brevity. Here
DocumentRoot
is the path to the directory from which the website files are to be served such asindex.html
.Create the directory from which you want to serve the files, for eg;
mkdir example.com
and change owner and default group of the directory, for eg if your logged in user name isubuntu
change permissions assudo chown ubuntu:www-data example.com
. This grants full access to the userubuntu
and allows read and execute access to the groupwww-data
.Now edit the apache conf file
/etc/apache2/apache2.conf
by issuing commandsudo gedit apache2.conf
and find the line<Directory /var/www/>
and below the closing tag</Directory>
, add the following below:Now there are two commands to enable or disable the virtualhost configuration files, which are
a2ensite
anda2dissite
respectively. Now since ourexample.com.conf
file uses the same port(80
) as used by the default configuration file(000-default.conf
), we have to disable the default configuration file by issuing the commandsudo a2dissite 000-default.conf
and enable our virtualhost conf file bysudo a2ensite example.com.conf
Now restart or reload the server with command
sudo service apache2 restart
. Now apache serves files from directoryexample.com
atlocalhost
on default port of80
.The
a2ensite
command basically creates a symbolic link to the conf file under the site-enabled directoryDo not edit files within sites-enabled(or *-enabled) directoy, as pointed out in this answer https://stackoverflow.com/a/41568701/2532763
To change the port and run from multiple directories on different ports:
Now if you need to run the directory on a different port, change the port number from 80 to 8080 by editing the virtualhost file as:
and editing
/etc/apache2/ports.conf
and addingListen 8080
just below the lineListen 80
Now we can enable the default virtualhost conf file that runs on port 80 since example.com directory uses port 8080, as
sudo a2ensite 000-default.conf
Now restart or reload the server with command
sudo service apache2 restart
. Now both the directories can be accessed fromlocalhost
andlocalhost:8080
If you couldn't find
http.conf
and followed Nick's way.Restart Apache using
sudo service apache2 restart
I had made the
/var/www
to be a soft link to the required directory ( eg./users/username/projects
) and things were fine after that.However, naturally, the original
/var/www
needs to be deleted -- or renamed.Instead of changing the default directory of your web server, you can create an
Alias
.Therefore you have to create two files:
/etc/apache2/sites-available/
and/etc/apache2/sites-enabled/
Call them both something like
example.conf
and insert in both filesAlias <where you want to access (eg. /example)> "<your files directory (eg. /home/user/host/example)>"
Then you should be able to access it with
//localhost/example
.EDIT:
Maybe I forgot to mention some commands because I forget them :P
or you have to edit the authorizations of the folder/file you try to access.
If you'r using Linux Mint (personal opinion, from all distros this one is making me happy), follow this:
1- Go to /etc/apache2/sites-available and edit 000-default.conf 2- Search for DocumentRoot, example DocumentRoot /var/www/html you change to your respective directory; 3- Open terminal and type: sudo service apache2 restart
EDITED----- I realize that in Mint you go for /etc/apache2/apache.conf, replace /var/www to your respective path, than restart server (step 3).
That's it.
I was working with LAMP and To change the Document Root folder i have edited default file which is there in /etc/apache2/sites-available folder. If you want to do the same just edit as follows
After this if you type localhost in browser it will load */home/username/new_root_folder* content.