Samba setup on the webroot in Ubuntu


What this is about:
LAMP, MAMP or WAMP… there are lots of packages out there you can install to enable you as a developer to set up a development environment on your machine. Products such as easyPHP, XAMP or Wampserver will get the job done with varying levels of difficulties and annoyance…

In my opinion the best environment to develop a web app is on a closer replication to your production server as possible. So, why not just use VMware? (guide to come on setting up a vm)

Download an iso of you OS of choice (for myself that would by Ubuntu), create a new Virtual Machine with you OS image and configure accordingly as you would do on your production server… simple.

There is then left one issue, how to access the files on the virtual machine as if local to the computer you working on.. well one answer is SAMBA.


How to install and configure Samba to your local apache webroot
1 – First install Samba:

apt-get install samba smbfs

2 – Configure the config file:
Set the workgroup and access, create a ‘share’ and create the respective users and groups for the samba and var/www.
As per usual make a copy of the config file before you start working (it is a lot easier to rollback to safe file than reinstall or write from scratch).

cp /etc/samba/smb.conf /etc/samba/smb.conf.original

3 – Now open the original to edit

nano /etc/samba/smb.conf

4 – Set the work group (this can be anything you choose) and the security within the [global] section:

[global]
workgroup = Ubuntu Precise Group
security = user

5 – Now create the share (add it to the bottom of the file). A ‘share’ is basically a block with some set params, ie the path to the shared folder.. etc ect. You can have mutliple ‘share’ blocks in your config file:

[share-www]
comment = Precise File
Serverpath = /var/www
browsable = yes
guest ok = no
read only = no
create mask = 0755

6 – Now the trickiest bit, although still quite straight forward:
Create a new group for the var/www directory

groupadd webdev

7 – Change owner of the var/www to be apache ‘www-data’, add the new group you created to the var/www directory (make sure you are in var here)

chown  www-data:webdev www

8 – Change the permissions of var/www to 0775

chmod 0755 www

(For more on permissions see this guide)

9 – Now, create a ‘samba’ user.

adduser samba

10 – Now create a password for the ‘samba’ server/program for the user:

smbpasswd -a samba

11 – Restart the samba server:

/etc/init.d/smbd restart

You should now be able to connect to you VM from you mac, ‘cmd’ + k: smb://yourvmdomain

Enter the samba username and password and away you go.

If you are having write issues (as in it asks for another password), ensure the permissions are ok as this is usually the issue..

Time and timezone on Ubuntu


I recently created a VM of an Ubuntu server and lazily clicked the ‘Easy install’. However after creating a few users I quickly noticed that their creation timestamps were all wrong.. after some digging found the servers timezone was incorrect.

So to find the time of your sever just run the data program:

date

If you timezone is out of sync then run this package which has some cli auto prompts to correction

dpkg-reconfigure tzdata

For more information on Ubuntu server time visit here

Change a username on Ubuntu


To change a username in Ubuntu is simple. For example, say I wanted to change the username:
“jhon” to the correct spelling “john” i would simply run the following command:


usermod -l jhon john

 That’s it.. the username would be changed from jhon to john.

FTP locked to the webroot on ubuntu 12 with vsftpd

Sequence of events:

  1. Instal vsftpd
  2. Configure the vsftpd config file to correct upload permissions and lock user to their home directory
  3. Create an ftp user
  4. Make the then home directory of the ftp user that of the web root
  5. VSFTPD bug fix: make the webroot readonly, place a subfolder in the webroot which is writable
  6. Point apache to the new webroot

Install vsftpd

sudo apt-get install vsftpd

Configure the config file

1 – Access the config file for the vsftpd:

sudo vi /etc/vsftpd.conf

2 – Configure to the following setup. This will lock users to their home directory and ensure uploads are set to 0755 and not 0600 (meaning that the files are readable from a browser). The notes are pretty self explanitory (#notes):

#these settings essentially mean that the uploads will be uploaded with 0755 as their permissions

After making the changes to the conf file you will need to restart vsftpd for the changes to take effect

/etc/init.d/vsftpd restart

Create an ftp user

This guide should be enough to get you sorted with the creation of a new user.

https://help.ubuntu.com/community/AddUsersHowto#Command-line 

  
Make the home directory of the ftp user that of the web root
The main thing to remember here, if you haven’t done this before, is that the main details of all the users on you server are stored in a file at the following direction:

/etc/passwd

Open this file up and scroll down to the ftp user you have just created (note that this file is a ‘:’ file). The penultimate entry on your users name is the users home directory. Simple change this to the current webroot of your Apache server, eg:

ftp_user:x:1003:1004:ftp_user,,,:/var/www:/bin/bash 

VSFTPD bug fix
1 – Make the webroot readonly, place a subfolder in the webroot which is writable. This can be done in many ways, one way would be to first create the folder within www, eg ‘public_html’ then move all the existing contents into the said folder.. You can use the command line, webmin, cpanel , filezilla over an sftp connection.
The reason you have to do this is because vsftpd doesn’t allow connections into a writable directory, bug or not it is not the best but apparently is due to a security flaw..

2. Last but not least, point Apache to the new webroot you have just created. This is very dependant on your server setup. However for a straight forward setup, ie no multiple vps the file you need to change lives here:

/etc/apache2/sites-available/default

Open and edit so the top lines look like this, remember public_html is just what I named the www sub directory.. change if needed:

Now restart apache and you should be good to go

/etc/init.d/apache2 restart