Setting up a web folder

Submitted by Andy on Fri, 25/01/2019 - 14:13

After installing an testing your web server you will want to add your own site for it to serve. This guide is to help you set up a folder that will contain your websites. It follows on from the Setting up a development Apache 2 web-server guide. Steps 1 to 3 are required while step 4 is optional. The use of virtual servers will be covered in the next tutorial.

In this guide substitute [username] for your username.

Step 1 - User permissions

The standard Apache set-up (on Linux) uses a www-data user and www-data group. Therefore all files used by Apache need to be readable by the www-data group /user.

On method to work with your website folders and files is to work as the root user. However for security reasons it's not recommended to work as a user with root privileges. Therefore we need to find a way to limit our use of the root user and still be able to work with the site files.

The best and most convenient way to do this is by adding your user to the www-data group. This will give you access to folders and files that Apache can use.

sudo adduser [username] www-data

Step 2 - Create sites folder

Now you have you user in the www-data group we can make a sites folder. The default install of Apache places all the websites in folder under /var/www. Placing our site(s) directly under this folder will require us to use root privileges and potentially need extra work setting permissions. It also will get quite difficult to manage multiple sites.

It is therefore best to create a new folder under /var/www that will contain separate folders for each of our sites. For this guide we will create a folder called sites.

Open a Terminal window (pressing Ctrl-Alt-t).

cd /var/www/
sudo mkdir sites 
sudo chown -R www-data:www-data sites/ 

The first line changes directory to the /var/www folder.

The second line creates the new folder (using root privileges). This will require you to enter your password to create the folder.

The final line sets the owner and group of the new folder and all its contents to www-data. This will allow Apache to read, write and create files within our new folder.

Step 3 - Change folder permissions

We now have a folder but we need to make sure that the folder permissions are correct

sudo chmod -R 775 sites/

This will set the permissions so that the owner and group have full control but other users can only read /list the folders and files. The -R in the command sets the permissions for the folder and all files and folders within it.

Step 4 - Create a  link to the new folder (optional)

We can now create a link to our new sites folder in our home folder. This step is entirely optional and only added for convenient navigation.

ln -s /var/www/sites/ ~/websites

What next?

Now you have a sites folder the next steps are to create your websites in their own sub-folders and set-up virtual hosts to serve them.