I was asked today by a colleague how I set up websites on a server without a GUI. So, I thought I would do a quick run through of the commands I use to set up a new website using Ubuntu 10.04. The following should work just fine on all subsequent versions of Ubuntu though.
This tutorial assumes that you’re comfortable logging into your server via SSH. It also assumes that you are logging in as someone other than the root user (which you should be if you’re not) and using the sudo command to run further commands with root privileges.
First of all make sure that your hostname is set:
1 2 |
hostname hostname -f |
The first command will show you the short version of the hostname. The second will show the fully qualified domain name for your system. If there is a problem with either, you must set this properly before continuing.
Next, make sure that you have apache2 installed:
1 |
sudo apt-get install apache2 |
Also, because we are going to be configuring name-based virtual hosts for our websites, we should disable the default virtual host:
1 |
sudo a2dissite default |
Ok, now we’re ready to start serving websites. To create your first site their are several steps we will go over. First of all, lets create a user:
1 |
sudo adduser example |
Replace example with the name you want to use for the website you’re setting up. Follow the instructions for setting the password etc. This will create a home directory for the user in /home/example
Now switch to the the user you just created:
1 |
su example |
Switch the user’s directory and create a web-facing folder:
1 2 |
cd /home/example mkdir public_html |
Create a file so that we can test that apache is serving our content. I use the nano editor but feel free to use any editor you want.
1 |
nano public_html/index.html |
Just add something like ‘Apache is serving my content…Woohoo’ and save. Switch back from the user you created:
1 |
exit |
Now we need to set up the virtual host. Create a file with the same name as the domain name:
1 |
nano /etc/apache2/sites-available/example.com |
and add the following content:
1 2 3 4 5 6 |
<VirtualHost *:80> ServerAdmin me@example.com ServerName example.com ServerAlias www.example.com DocumentRoot /home/example/public_html/ </VirtualHost> |
Again, replace example.com with your own domain name and change the SeverAdmin email to your own email. Notice that the DocumentRoot points to the web-facing directory we setup earlier. Now we need to enable the virtual host we just created and restart the server:
1 2 |
sudo a2ensite example.com sudo service apache2 restart |
Now, assuming that you having pointed your domain at the server’s IP, you should now be able to access your website via a browser.
One final step to go over is to enable FTP access so that you can upload data to your website. I’m going to show you how to use the more secure SFTP protocol that almost all FTP software supports. First of all, we need to allow SFTP connections. Make sure that the following line exists in /etc/ssh/sshd_config:
1 |
Subsystem sftp internal-sftp |
If not, add it in, then at the end of the file add:
1 2 3 4 5 |
Match group sftp ChrootDirectory %h X11Forwarding no AllowTcpForwarding no ForceCommand internal-sftp |
Save the file and restart SSH:
1 |
sudo /etc/init.d/ssh restart |
Add the group sftp like so:
1 |
sudo addgroup sftp |
Now all we need to do is add users that we want to give SFTP access to, to this group. To make sure they don’t go wandering outside their home directory, we’ll also set permissions to stop them:
1 2 3 |
sudo usermod -G sftp example sudo chown root:root /home/example sudo chmod 755 /home/example |
Now you should be able to SFTP into the server using the username and password you setup. The user will only be able to see their home directory and nothing else.
Hopefully this will help you to set up sites quickly in the future without having to install cpanel, plesk or the like.
Good luck!