netarky.com

How to set up a web server on your Arch Linux VPS

Updated 18 Nov 2013

Getting started * Download, compile and install Apache * Get Apache up and running * Configure Apache to host multiple websites

You will most likely want to install a web server to serve up your website(s). Apache and Nginx are the two most common. There may be more documentation and examples for Apache, but perhaps Nginx is faster and uses less memory when you're dealing with a huge volume of visitors.

You will need to know how to create, delete, and modify Apache or Nginx virtual hosts for each website on your server.

If you expect a lot of traffic to your site you may also want to install Varnish, Memcache, or other caching programs to optimize performance.

 

Getting started

The web's most widely-used web server is called Apache. It's open source and free to use.

Many commonly-used programs can be installed on Arch by issuing a simple pacman command. At the time of writing, Arch is still in the Arch User Repository (AUR), so it must be compiled and installed manually. (You could of course use Yaourt instead.)

Here you will download the Apache package from the AUR direct to your VPS, compile it and then install it.

First off, for the sake of tidiness, create a directory where all future build packages can be downloaded to.

cd /
sudo mkdir builds
ls
binbuildsetcliblost+found optrootsbinsysusr
bootdevhomelib64mnt procrunsrvtmpvar

The above commands create a new directory called 'builds' under the root directory and ls lists the contents under the root directory. There should now be a builds directory in there. Make it the current directory:

cd builds

 

Download, compile and install Apache

Now find Apache on the AUR. Point your browser to aur.archlinux.org. Input 'apache24' into the package search box. (Apache 2.4 is the most recent version at the time of writing.)

The search will return a couple of relevant results. You are looking for the one with the description A high performance Unix-based HTTP server. Click through to the package details.

Assuming that you're accessing the AUR from your local browser, you'll need to download the tarball directly to your VPS and not to your local machine. So don't click but copy the download tarball link. Then from the command line use curl and paste the link in. It should look something like this:

curl -O https://aur.archlinux.org/packages/ap/apache24/apache24.tar.gz

curl gets the content of a URL and displays it in the command terminal. But used with the option -O (capital o), curl will save the URL's output to a file in your VPS' filesystem, and will use the URL's filename (apache24.tar.gz) as the name of the saved file.

Issue the command man curl to read more about curl.

Because the builds directory is the current directory, you should find the tarball (apache24.tar.gz) there. Use the command ls to list the directory's contents:

ls
apache24.tar.gz

The tar (tape archive) command is used to convert a group of files into an archive. An archive is a single file that contains individual files plus information to allow them to be extracted to their original form. Although tar was originally designed for backups on magnetic tape, it can now be used to create archive files anywhere on a filesystem.

Archives that have been created with tar are commonly referred to as tarballs.

tar does not perform compression itself. apache24.tar.gz is a tarball compressed with a utility called gzip, hence the .gz at the end.

To extract the archive of files inside apache24.tar.gz issue the command:

tar -xvzf apache24.tar.gz

where -x means extract, -v (verbose) will display the files in the archive, -z means decompress with the gzip utility, and following the -f will be the tar filename to be extracted. Here's what to expect:

apache24/
apache24/PKGBUILD
apache24/apache.install
apache24/httpd.logrotate
apache24/httpd
apache24/apache.tmpfiles.conf
apache24/mpm-itk.patch
apache24/httpd.service
apache24/arch.layout

The extraction process created a directory under builds called 'apache24' and inside this are Apache's files, including the all-important PKGBUILD. The PKGBUILD file is used by the command makepkg, which uses the instructions contained within it to create a pacman-installable, binary package with the extenstion '.pkg.tar.xz'.

cd apache24
makepkg -Acs

The -A option ignores the target Arch architecture. The -c option cleans up the directory after makepkg is done, and -s installs any needed dependencies automatically.

It may take some time to finish, but makepkg should have created a file inside the apache24 directory with the filetype .pkg.tar.xz. ls in long listing format (-l) should give the following files:

ls -l
PKGBUILD
apache.install
apache.tmpfiles.conf
apache24-2.4.4-4-x86_64.pkg.tar.xz
arch.layout
httpd
httpd-2.4.4.tar.bz2
httpd-2.4.4.tar.bz2.asc
httpd.logrotate
httpd.service
mpm-itk-2.4.4-04.tar.gz
mpm-itk.patch
pkg
src

And there's the package: apache24-2.4.4-4-x86_64.pkg.tar.xz. Install this package by using the -U option with pacman (replace apache24-2.4.4-4-x86_64 with the actual package name in your case):

sudo pacman -U apache24-2.4.4-4-x86_64.pkg.tar.xz

Great, you've got Apache installed on your VPS.

 

Get Apache up and running

Apache needs a little bit of configuration before it will serve up your webpages. Navigate to Apache's configuration file and open it using Nano:

sudo nano /etc/httpd/conf/httpd.conf

Scroll down through the file using CTRL+v until you get to Listen. Check that Listen is uncommented (no preceding #):

...
Listen 80
...

Scroll down to Servername and edit so:

...
ServerName localhost:80
...

Hit CTRL+o then ENTER to write your modifications and then CTRL+x to exit Nano. Now open up the hosts file:

sudo nano /etc/hosts

Edit the line beginning 127.0.0.1 by appending your hostname (as defined in your VPS control panel) to the end. Then save and exit.

127.0.0.1 localhost.localdomain localhost yourhostname

Open the hostname file and add your hostname underneath localhost. Save and exit.

sudo nano /etc/hostname

Restart Apache to effect the changes:

sudo systemctl restart httpd

Apache serves up webpages from the /srv/http directory. It will look for a page called index.html by default, which will serve as your website's homepage. Edit the default index.html page:

sudo nano /srv/http/index.html

Your first webpage should be something nice. Try this old chestnut (if you must):

<html>
<title>My VPS Webserver</title>
<body>
<h2>Hello world!</h2>
</body>
</html>

Save and exit. Then point your browser to your VPS' IP address and voila!

 

Configure Apache to host multiple websites

Using Apache, you can run many websites on the same server using the same IP address, even for different domains. This is called name-based virtual hosting. First, configure httpd.conf to allow virtual hosting:

sudo nano /etc/httpd/conf/httpd.conf

Scroll down through the configuration file using CTRL+v until you get to #Virtual hosts. Uncomment the following line by removing the preceding hash #:

Include conf/extra/httpd-vhosts.conf

CTRL+o then ENTER to write and then CTRL+x to quit Nano.

Suppose you have registered two unique domains called yourdomain1.com and yourdomain2.net. You will need to update the DNS records for each domain name so that they point to the nameservers of your VPS hosting company. This is done through your VPS' control panel and also through the control panel of your domain registrar (where you bought the domains).

Access your VPS' account control panel. You should be able to add domains to your VPS. Add both yourdomain1.com and yourdomain2.net. Check your VPS host's nameservers in the domain properties. (The records you're looking for are the NS records.) There will be at least two, maybe three nameservers (in case one goes offline, there is a backup so your domain name can still be resolved). They should look something like ns1.yourVPShost.com, ns2.yourVPShost.com.

Also check that both domains have an 'A' record pointing to the IP address of your VPS.

Then access your control panel at your domain registrar. You will need to configure each domain so that it points to the aforementioned nameservers.

While you're waiting for your domain registrar to effect the changes (which can take a few minutes to several hours), configure Apache's virtual hosts configuration file httpd-vhosts.conf to tell Apache about your domains:

sudo nano /etc/httpd/conf/extra/httpd-vhosts.conf

Delete the existing lines of virtual host examples (using CTRL+k) and replace them with the following:

<VirtualHost *:80>
ServerAdmin john@yourdomain1.com
DocumentRoot "/srv/http/www/yourdomain1.com"
ServerName yourdomain1.com
ServerAlias www.yourdomain1.com 123.45.678.90
ErrorLog "/var/log/httpd/yourdomain1.com-error_log"
CustomLog "/var/log/httpd/yourdomain1.com-access_log" common
</VirtualHost>

<VirtualHost *:80>
ServerAdmin john@yourdomain2.net
DocumentRoot "/srv/http/www/yourdomain2.net"
ServerName yourdomain2.net
ServerAlias www.yourdomain2.net
ErrorLog "/var/log/httpd/yourdomain2.net-error_log"
CustomLog "/var/log/httpd/yourdomain2.net-access_log" common
</VirtualHost>

DocumentRoot is the directory from where the domain's webpages will be served from. To keep things organised between websites, you should create your own directory structure inside the /srv/http directory. In my case, I created a subdirectory called www, and then further subdirectories for each website, a yourdomain1.com directory and a yourdomain2.net directory.

Now, as per the configuration above, when you go to yourdomain1.com (or the alias www.yourdomain1.com), Apache will serve the files from the /srv/http/www/yourdomain1.com directory.

When you go to yourdomain2.net (or www.yourdomain2.net), the same Apache running on the same server will serve the files from the /srv/http/www/yourdomain2.net directory.

Notice that in ServerAlias for yourdomain1.com I also included the IP address 123.45.678.90 of the VPS. In case someone physically types the IP address into a browser, Apache will serve yourdomain1.com as default, and not yourdomain2.net.

CTRL+o then ENTER to write and then CTRL+x to quit Nano. Restart Apache to effect the changes:

sudo systemctl restart httpd

Try it out by putting your different website addresses into your browser. (Of course, each website directory should have an index.html file in it first.)

Just to reiterate, for name-based virtual hosting to work properly, the DNS entry for both these websites should be pointing to the same IP address of your VPS where Apache is running.

If you want Apache to start when your VPS boots up issue the following command:

sudo systemctl enable httpd.service

 

Sources:
www.thegeekstuff.com/2011/07/apache-virtual-host
httpd.apache.org/docs/2.0/vhosts/examples.html#page-header