This file is to tell how to configure /var/www/ dir. So, here is the plan Lets say we have 3 developers working in a company:
- Alice
- Bob
- Mac
Our main site is at http://example.com Each dev will work in their own environments:
- Alice => alice.example.com
- Bob => bob.example.com
- Mac => mac.example.com
Assuming we setup each devs directory like this:
/var/www
/example.com #Main serving site
/alice.example.com
/bob.example.com
/mac.example.com
We consider following requirements:
-
Alice should not be able to enter into bob's and mac's directory. Bob cant in alice's and mac's. Each user should be confined to his directory only and their home directory.
-
apache process should be able to access all the directories within
/var/www
-
Users can read+write in their dev directories, as well as apache can read and execute files and directories and can write in defined directories such as uploads.
- Add all three users: (same for bob and mac)
useradd alice
- Create group for users:
groupadd alice
- Add each user to their own group
usermod -aG alice alice
- Add apache user (the default user for apache) to each user's group
note: use ps aux | grep httpd
(or apache2) to know the user for apache
usermod -aG alice apache
Now, execute following commands to give alice permission
sudo find /var/www/alice.example.com -type d -exec chmod 2775 {} +
sudo find /var/www/alice.example.com -type f -exec chmod 0664 {} +
sudo chmod -R 750 /var/www/alice.example.com
sudo chgrp -R apache /var/www/alice.example.com
sudo chown -R alice /var/www/alice.example.com
sudo chmod g+s /var/www/alice.example.com
To make some directory writeable by apache (server), run following command:
`sudo chmod -R 775 /var/www/alice.example.com/uploads`
Finally, you should get following output for la -al /var/www/alice.example.com
:
drwxr-s--- 21 alice apache 4096 Feb 11 02:48 .
(for directories)
-rwxr-x--- 1 alice apache 4518 Dec 30 11:24 README.txt
(for files)
#Granting User access from their home directories We can create symlinks in their respective home directories. Better, use a subdirectory in user's home directory to serve their content on their subdomains
ln -s /var/www/alice.example.com /home/alice/
#Generating user's key for SSH and SFTP logins:
- Switch to user
sudo su - alice
- Create .ssh in user's home dir
mkdir ~/.ssh && cd ~/.ssh
sudo touch authorized_keys
- Logout from user
exit
- Generate user's key
ssh-keygen -b 2048 -t rsa -f alicekey -C alice
cat > /home/alice/.ssh/authorized_keys < alicekey.pub
chmod 0700 /home/alice/.ssh; chmod 0600 /home/alice/.ssh/authorized_keys
cat > ~/alicekey.pem < alicekey
- Handover alicekey.pem secretly (also keep a copy to yourself). Ask alice to login using the above key like this:
ssh -i /path/to/alice/pemkey/on/her/machine [email protected]
if she can login, we are good to go