Apache folder in linux

How do I change the root directory of an Apache server? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

How can I change the document root of the Apache server? I basically want localhost to come from /users/spencer/projects directory instead of /var/www . 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.

Viewed 815k times, I never get stackoverflow moderators. So many stackoverflow users have to deal with apache cause it’s how they distribute what the programmed. That it is so close to the field SO is about that I would not even consider this for closing due to being offtopic.

15 Answers 15

Please note, that this only applies for Ubuntu 14.04 LTS (Trusty Tahr) and newer releases.

In my Ubuntu 14.04 LTS, the document root was set to /var/www/html . It was configured in the following file:

/etc/apache2/sites-available/000-default.conf 
sudo nano /etc/apache2/sites-available/000-default.conf 

and change the following line to what you want:

sudo nano /etc/apache2/apache2.conf 
 Options Indexes FollowSymLinks AllowOverride None Require all granted 

And change /var/www/html to your preferred directory and save it.

After you saved your changes, just restart the Apache 2 web server and you’ll be done 🙂

sudo service apache2 restart 

If you prefer a graphical text editor, you can just replace the sudo nano with a gksu gedit .

@Paul Lo: I haven’t tried this on Ubuntu 13.10, only on Ubuntu 14.04; As I’m pretty sure that the first time I set up apache (which was on Ubuntu 12.04), there was no such directory. So yes, I guess that this is only available on Ubuntu 14.04 LTS and newer. However, you can have a look at the other answers. The upvotes suggest that they helped other people, maybe they also work with Ubuntu 13.10.

However thanks for asking, I just edited this information into the answer so that noone else walks into the trap of trying this with older Ubuntu versions (y)

@mozzbozzm, you are right. Only 14.04. Really a weird choice, now suddenly having var/www/html as default root. Are they actually trying to waste peoples time? 🙂

I’m getting a forbidden error You don’t have permission to access / on this server. when I hit localhost:80 in my browser. Do I need to change the port?

Читайте также:  Терминальный сервер linux wayland

@Costa I solved the problem by adding Options Indexes FollowSymLinks AllowOverride None Require all granted in the /etc/apache2/apache2.conf

You need to change the DocumentRoot setting in your httpd.conf file. Chances are it will be under something like /etc/apache2/conf/httpd.conf .

Use your favourite editor (I recommend Vim) and look for the DocumentRoot and change it to /users/spencer/projects . Also look a little further down for a setting that looks like this:

You will also want to change what is in the quotes to your new directory. This gives Apache access to read from that directory when a user makes a request that call on it.

Now restart your Apache service ( httpd -k restart ) and you should be good to go.

Apache 2 site configuration files are now typically kept in /etc/apache2/sites-available/ (Debian, Ubuntu, etc.).

the httpd.conf file is empty but I found a file at etc/apache2/sites-available/default and changed the root directory there. I prob should have mentioned that this is not on an actual server. I am setting up a local development environment. Not sure if that makes a difference

Would it be possible to enable both /var/www and /home/myusername/projects_folder at the same time? And how should I config? thanks.

The «answer» is really obsolete. Correct answer according to Ubuntu 18.x docs is Either change the root directory of Apache or move the project to /var/www/html . To change Apache’s root directory, run: cd /etc/apache2/sites-available. Then open the 000-default.conf file using the command: nano 000-default.conf. Edit the DocumentRoot option: DocumentRoot /path/to/my/project. (see askubuntu.com/questions/337874/…)

I had to edit /etc/apache2/sites-available/default . The lines are the same as mentioned by RDL.

A suggested edit by @pabloab had this to say: «Apache 2.4 and above requires .conf at the end, and by default have a 000- as prefix». The result is /etc/apache2/sites-available/000-default.conf . I haven’t tested this out, but it may help some folks.

^^ this fixed it. Where is it documented that I require .conf at the end of my sites-available files. I have about 20 virtual hosts that were disabled and offline after an update, and after a couple hours, finding this and renaming my sites-available files fixed it for me.

To run Apache in a user directory you need to add the user to the Apache group and the Apache to the user group. I suggest adding the following 3 commands to the answer: Open the terminal with your user and run: sudo usermod -a -G www-data $USER; sudo adduser $USER www-data; sudo adduser www-data $USER;

In file /etc/apache2/apache2.conf it should be as below without the directory name:

 Options Indexes FollowSymLinks AllowOverride All Require all granted 

And in file /etc/apache2/sites-available/000-default.conf you should include the custom directory name, i.e., www:

DocumentRoot /home/username/www 

If it is not as above, it will give you an error when loading the server:

Forbidden You don’t have permission to access / on this server

To run Apache in a user directory you need to add the user to the Apache group and the Apache to the user group. I suggest adding the following 3 commands to the answer: Open the terminal with your user and run: sudo usermod -a -G www-data $USER; sudo adduser $USER www-data; sudo adduser www-data $USER;

Читайте также:  Класс операционной системы linux

The right way to change directory or run from multiple directories under different port for Apache 2 is as follows:

For Apache 2, the configuration files are located under /etc/apache2 and doesn’t 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 configuration files are located in /etc/apache2/sites-available (do not edit files within sites-enabled). The default Apache installation uses virtualhost conf file 000-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 by sudo cp 000-default.conf example.com.conf , now edit the file by sudo gedit example.com.conf to:

 ServerAdmin example@localhost DocumentRoot /home/ubuntu/example.com 

I have deleted the nonimportant 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 as index.html .

Create the directory from which you want to serve the files, for example, mkdir example.com and change owner and default group of the directory, for example, if your logged in user name is ubuntu change permissions as sudo chown ubuntu:www-data example.com . This grants full access to the user ubuntu and allows read and execute access to the group www-data .

Now edit the Apache configuration file /etc/apache2/apache2.conf by issuing command sudo gedit apache2.conf and find the line and below the closing tag , add the following below:

 Options Indexes FollowSymLinks Includes ExecCGI AllowOverride All Require all granted 

Now there are two commands to enable or disable the virtualhost configuration files, which are a2ensite and a2dissite respectively. Now since our example.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 command sudo a2dissite 000-default.conf and enable our virtualhost configuration file by sudo a2ensite example.com.conf

Now restart or reload the server with command sudo service apache2 restart . Now Apache serves files from directory example.com at localhost on default port of 80 .

The a2ensite command basically creates a symbolic link to the configuration file under the site-enabled directory.

Do not edit files within sites-enabled (or *-enabled) directory, as pointed out in this answer.

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:

 ServerAdmin user@localhost DocumentRoot /home/ubuntu/work 

and editing /etc/apache2/ports.conf and adding Listen 8080 just below the line Listen 80

Читайте также:  Возможности линукс и виндовс

Now we can enable the default virtualhost configuration 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 from localhost and localhost:8080 .

Источник

How to Change Default Apache ‘DocumentRoot’ Directory in Linux

Apache web server is probably the most used web server across platforms including different Linux distributions and Windows. The web server is used to deliver web content and can serve many queries at once.

It is often the preferred choice by professionals for building different web projects. Having at least basic knowledge of this web server is vital for any young professional who wants to start a career as a Linux system administrator.

In this short tutorial, you are going to learn how to modify the root directory for Apache web server. For the purpose of this tutorial, we will use Ubuntu/Debian and RHEL/CentOS/Fedora based installations of the web server.

However the paths and directives are practically the same for other distributions as well, so you will be able to apply the learned in different OSes as well.

To make the necessary changes you need to modify the DocumentRoot directive of the web server. This is the directory from which Apache will read the contents that the visitor will access over browser. Or in other words, this is the directory that forms the tree of directories that will be accessible over web.

The default DocumentRoot for Apache is:

These paths are described in the Apache’s configuration file.

Ubuntu/Debian

/etc/apache2/sites-enabled/000-default /etc/apache/apache2.conf

CentOS/RHEL/Fedora

To change the document root for your Apache web server simply open the corresponding file with your favourite text editor and search for DocumentRoot .

# # DocumentRoot: The directory out of which you will serve your # documents. By default, all requests are taken from this directory, but # symbolic links and aliases may be used to point to other locations. # DocumentRoot "/var/www/html" 

After that change the path to the new target directory and make sure that Apache is able to read/write in that directory. Once you have modified the DocumentRoot, save the file and restart apache with:

# systemctl restart apache [For SystemD] # service httpd restart [For SysVinit]

Final thoughts

The change of default document root is an easy task that can be completed in a couple of minutes. When making such changes is important to make sure that you do not make any typos and make sure to always restart Apache after making changes to its configuration file.

Источник

Оцените статью
Adblock
detector