In this article, I’m going to be outlining the steps to install and configure a complete web server on a base install of Ubuntu 12.04 LTS server edition.
I personally don’t prefer to install package groups relating to “LAMP” or similar during initial installation because I’m simply too lazy to review each and every package that is included in those groups, and what the dependencies are. By installing the required packages using apt-get, this allows you to install just the software you’re looking for, without worrying about dependencies. This software stack is time tested; we’ll install on Ubuntu’s latest release, and test a few popular applications while we’re at it!
A newly installed Ubuntu 12.04 LTS Server Edition with no additional packages. Need help with this step? Check here.
Required packages: apache2, php5, mysql-server, php5-mysql
Optional packages: To configure this server remotely, you will need to have installed openssh-server. This can be achieved by the following in terminal:
apt-get install openssh-server
Note: This and many other guides on this site assuming you are performing operations as root (or other super user) unless otherwise stated. By default, root login is prohibited on Ubuntu, even in the terminal. During installation, the administrative account created has sudo permissions. I suggest sudoing to root; otherwise, feel free to append ‘sudo’ and full path names as necessary.
Step 1: Installing Apache2
Though often referred to as just ‘apache’ the name of the package we need to install is apache2. To begin, log into your system’s terminal. Run the following command:
apt-get install apache2
Type ‘Y’ as appropriate. If you have a fast network connection, this should take a matter of seconds!
To test, on another machine that has connectivity to yours, type your server’s IP in a web browser, your you can type the following:
wget localhost index.html ls
Note the created index.html file in your current working directory. If you don’t have this file, then apache2 might not be installed.
To test this further, try the following:
rm index.html service apache2 stop wget localhost index.html
This will have stopped the apache2 service. Note the error in the output of wget, about the connection being refused. Also note there is no index.html. Restart apache2 using:
service apache2 start
That completes the default installation of apache2.
Step 2: Configuring Apache2
While there are entire books written on configuring Apache, what I hope to outline here is basic default settings and typical production-ready setup. I won’t be covering how to run Apache2 from a chroot jail or anything fancy like that (which is pointless, IMO) as those steps are available elsewhere are not specific to the task at hand.
Now that apache2 has been installed, you can see the requisite startup script has been placed in /etc/rc2.d/
The number may vary, but what’s important is the S, which stands for start. This script is a soft link to /etc/init.d/apache2.
The location of the apache2 config file is
Those of you familiar with Red Hat and similar distros will note the httpd.conf file, which has been created but is empty by default. httpd is the name of the package/service of apache2 on Red Hat and similar distros.
Contained in the same directory as our config file, is a subdirectory named conf.d. Any files with the suffix .conf in this directory will be parsed by apache2 during startup. It’s common practice to put config files in this directory if you’re running multiple sites (or subdomains) on the same server. Create the following file:
and paste the following into it:
<VirtualHost *:80> ServerAdmin email@example.com DocumentRoot /var/www/mysite.com ServerName www.mysite.com ErrorLog /var/log/apache2/error.log </VirtualHost>
We have now configured our first virtual host named www.mysite.com. The first line of that file tells apache which IP and port to listen on for this virtual host. In our case, *:80 means listen on any IP, port 80. You can put in your server’s IP here if you like, but *:80 is fine.
The second line, ServerAdmin is optional, and so is the last line ErrorLog. Whatever settings or options aren’t configured inside the <VirtualHost> tags will be inherited from apache2.conf or other config files.
The third line denotes the directory where you server’s web content will reside. The default location for apache2 is /var/www. I simply have chosen to create a subdirectory named mysite.com in that directory; another common location is a subdirectory in the /home directory. This is up to you, and will vary upon your individual needs.
The fourth line is the host header apache2 will listen for, as sent by the end-user’s web browser. As you may know, web resources can be accessed via IP address of the server, or a domain name as resolved by DNS. On systems with more than one site, apache2 will read the domain name and present the appropriate content.
Now that you have created your virtual host’s config file, we need to create the directory we referenced, and assign it the appropriate permissions.
After you create the directory, create a simple index.html file inside of it, with whatever content you like. Then, perform the following:
chown root:www-data /var/www/mysite.com -R chmod 550 /var/www/mysite.com -R
The user/group www-data is the default user which will run the apache2 process. I suggest setting permissions this way, only adding write permissions as absolutely necessary by your application.
Next, we want to restart apache, so it will read our new configuration file. This should proceed without error.
Now, to test that our virtual host is working as intended, on a remote system on which you have administrative access, edit your hosts file, and add the following entry:
Substituting youriphere with…your web server’s IP! Don’t know where the host file is on your system? Look here: wikipedia
Now, on the system with the edited hosts file, type in www.mysite.com. This should display the contents of index.html that you created! If not please check the steps outlined above carefully!
Another option to test is to create the entry in /etc/hosts on the webserver itself, and use wget just as we did above, except this time use www.mysite.com/index.html as the target file!
Step 3: Installing php5
If you don’t have apache2 up and running yet, please complete that step first.
To install php5 perform the following:
apt-get install php5
This should only take a few minutes to complete. The default location for the config file is:
While most settings are appropriate by default, I suggest making the following change:
safe_mode = on
Next, let’s rename index.html to index.php, and replace the contents with the following:
<?php echo \'hello\'; ?>
Restart apache2. Now, visit www.mysite.com again. The only text on the screen should be “hello”. If this is the case, you have successfully installed php5!
Step 4: Installing MySQL
apt-get install mysql-server apt-get install php5-mysql
Set password for the root user. Make this a strong password. I recommend a 14+ character passphrase with mixed case, numbers, and special symbols. Make it something you can remember, or store it in a safe place that’s NOT ON THE WEBSERVER. Next, restart apache2.
Next, we’re going to setup a new database for our web application, create a new user, and assign that user permissions only access that db.
mysql -p #enter the root password when prompted on the next line
CREATE DATABASE webdb;
CREATE USER ‘webuser’@'localhost’ IDENTIFIED BY ‘password_here’; #Be sure to replace ‘password_here’ with a strong password.
GRANT ALL PRIVILEGES ON webdb.* to webuser@’localhost’;
This should complete the steps for MySQL. Next, let’s install WordPress!
Step 5: Install WordPress
This part is obviously optional, but I’m including it as it demonstrates that everything is working properly.
Download wordpress, or upload it via sftp to your server.
Next move to, unzip and untar the archive in our site’s document root. We’ll need to modify the root of the WordPress installation as well.
tar zxfv latest.tar.gz chmod 770 /var/www/mysite.com chown www-data:www-data /var/www/mysite.com
Now, let’s open up our web browser on our desktop, and attempt the installation!
Everything should go smoothly! I recommend changing permissions on our site’s folder back to 550 after installation is complete.
Check back at a later date for more articles relating to iptables and other ways to secure your webserver!
You can skip to the end and leave a response. Pinging is currently not allowed.