In order to learn web design and programming, I first of all decided to build a web server on my personal computer.
This is a (hopefully) quick guide to creating a web server on a Linux PC.
I performed the full installation as "root", for practical reasons.
The installation is in 4 parts:
We need to create suitable groups and users to run the different services to avoid the security issues inherent in running them as "root"
groupadd www #Add a group for the apache server if one doesn't already exist.
useradd -g www apache2 #Add an Apache server account
groupadd mysql #Add a group for the mysql server if one doesn't already exist.
useradd -g mysql mysql #Add the MySQL server account
I prefer to download directly from the vendors, and the latest versions.
MySQL has a commercial version as well as the GPL version, "so pick wisely".
Remember to check the download using the checksums povided.
MySQL "MySQL Community Server" is the free version. Version 5.0.86 is the release I use.
Apache2 Version 2.2.14 is the release I use.
PHP Version 5.3.1 is the release I use.
Extract the files from the download.
tar -xzf mysql-5.0.86.tar.gz #Creates a source directory
cd mysql-5.0.86/ #Change to the new directory
Now we`ve got to configure the compile, compile and install the software.
./configure --prefix=/usr/local/mysql-5.0.86 --with-charset=utf8 --with-collation=utf8_general_ci
# This tells the compiler where we want it installed --prefix=/usr/local/mysql-5.0.86 We are using the full name of the version for the install. Later we will link to it using a "soft link", this is because MySQL expects its software to be installed in /usr/local/mysql.
# and configures it with a default charset of utf-8 and default collation of utf8_general_ci. Discussions of charsets and collation are outside the scope of these instructions. Suffice it to say this default configuration works, for a basic server.
# There are many options that can be specified, so try ./configure --help to show them all.
Now we compile and install.
make #Compile - be patient, this may take some time. Go away, make a coffee, take a bath, grow a tree...
Don`t worry what it all means, just keep an eye open for any error messages when it stops
make install #Installs the software into the specified location
If you`re really curious about the output, you can redirect all output to a file using
> /tmp/outputfile 2>&1 and peruse it at your leisure, but the output from my "make" exceeded 1MB so it is rather large (and boring).
MySQL is basically now installed, but we have to configure it, and get it running.
Create a symbolic link to the software ln -s /usr/local/mysql-version /usr/local/mysql
To get it up and running we first create an administration database (where mysql will store details about its databases and privileges)
/usr/local/mysql/bin/mysql_install_db --user=mysql # This is a script supplied with the distribution to create this initial database.
Now we`re ready to start the server for the first time /usr/local/mysql/bin/mysqld_safe &
The "safe" mode will restart the daemon if it crashes, and the ampersand (&) runs it in the background
But you`ll probably want it to start automatically at machine startup
Fortunately, a template is provided for us with the distribution
cp /usr/local/src/mysql-5.0.86/support-files/my-medium.cnf /etc/my.cnf
There are several templates to choose from, read the comments at the top of each one, and pick the one that is most suitable for your server. "small" or "medium" will normally suffice.
Change the ownership of the file chown root:root /etc/my.cnf, then edit it in whatever way you want, and insert user = mysql immediately below the line that says [mysqld]
Again this is provided
cp /usr/local/src/mysql-5.0.86/support-files/mysql.server /etc/init.d/mysql
Then change the ownership chown root:root /etc/init.d/mysql
and make it executable chmod 700 /etc/init.d/mysql
chkconfig --add mysql
You may want to add the path for the mysql binary to you $PATH
Let`s first secure the server
Log on to the server as root (usr/local/mysql/bin/) mysql -u root -p, Initially there is no password set, so just press "enter". This is one of the things we are going to fix, unless you want to risk anybody messing up your databases
At the mysql prompt mysql> SELECT user, host FROM mysql.user; # mysql is not case sensitive with its syntax, but it is with items in databases
This will return something like:
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| | server |
| root | server |
| | localhost |
| root | localhost |
+------+-----------+
The first thing we need to do is remove the "guest" users
mysql> DELETE FROM mysql.user WHERE user="";
Now we`ll set the password for the "root" user
mysql> SET PASSWORD FOR 'root'@'127.0.0.1'=PASSWORD('newpassword'); # newpassword is the password you want to set
Then we need to repeat this for the other 2 "root" users. MySQL treats each as separate entities.
mysql> SET PASSWORD FOR 'root'@'server'=PASSWORD('newpassword');
mysql> SET PASSWORD FOR 'root'@'localhost'=PASSWORD('newpassword');
mysql> quit # Will get you out of mysql
And that's it, all ready to go. While you can use MySQL now, it's much more fun with a webserver running PHP, So now it starts to get interesting...
Install the Apache web serverThe process is basically the same as for the MySQL install, so I won`t repeat myself too much.
tar -xzf http-2.2.14.tar.gz #Creates a source directory
cd http-2.2.14/
One really useful feature, is the "Layout" feature. Basically, you can use a layout that matches your system, or customize a layout. This has the advantage that most of the configuration work will be done for you. In the installation directory is a file called config.layout. This file has predefined layouts for most Linux systems, but I use one of these to produce a "custom" layout, that fits my requirements. I use this to set the path for the web documents, logs, installation files etc. E.g.
# Custom layout <Layout Custom> prefix: /usr/local/apache2 # The installation directory exec_prefix: ${prefix} bindir: ${prefix}/bin sbindir: ${prefix}/sbin libdir: ${prefix}/lib libexecdir: ${prefix}/lib/apache mandir: ${prefix}/share/man sysconfdir: /etc/httpd # The path to the configuration file datadir: /srv/www # The path to the web files installbuilddir: ${datadir}/build errordir: ${logfiledir}/error iconsdir: ${datadir}/icons htdocsdir: ${datadir}/htdocs manualdir: ${datadir}/manual cgidir: ${datadir}/cgi-bin includedir: ${prefix}/include/apache localstatedir: /var/lib/httpd runtimedir: /var/run logfiledir: /var/log/httpd # The path to the log files proxycachedir: /var/cache/httpd </Layout>
The comments are just for clarification and were omitted from the file during the "configure".
./configure --enable-so --with-included-apr --enable-layout=Custom
make #This seems to be a lot quicker than the MySQL compile, or maybe I'm just becoming more tolerant?
make install
At this point you can configure the web server to run, although I will have to change this after PHP has been installed. Here I will just perform sufficient to check the server works, and prepare it for automatic startup at server boot.
The configuration file will have the changes from the "Layout" already included, but you still need to make a few changes. The config file used by the server is /etc/httpd/httpd.conf. How you make them is up to you.
Find the lines that start with the following
User and Group, and change them to the user and group created earlier
User apache2
Group www
ServerAdmin add your own email address
This server is a development server, so I am not overly concerned with Security, therefore I remove a good chunk of it by changing the entry for the "directory" directive.
Find the lines that starts with <Directory>, and amend them to
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
By removing the last 2 lines
Now all we need to do is start the server, for which we use /usr/local/apache2/sbin/apachectl, with start, stop or restart as the parameter.
/usr/local/apache2/sbin/apachectl start # And off you go.
Check it using ps -ef|grep http # This should show multiple processes, 1 owned by root and the rest owned by your apache2 user.
To make the webserver start automatically at boot
Copy apachectl to /etc/init.d cp /usr/local/apache2/sbin/apachectl /etc/init.d
Then chkconfig --add apachectl to add it to the startup.
The process is basically the same as for the previous installs.
tar -xjf php-5.3.1.tar.bz2
cd php-5.3.1/
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/sbin/apxs --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pear=/usr/local/php/lib/php
There are many options that can be installed in PHP, use ./configure --help to see them all, but I have chosen the ones that are most relevant to LAMP. I.e. Apache and MySQL. PEAR is a repository of re-useable code written by others that you may find useful
make
make install
PHP runs as part of the Apache web server, so we have to change the webserver to use PHP.
Hopefully the installation will have made some of the changes for you, but you need to check
Check /etc/httpd/httpd.conf for the following (and add them if they`re missing):
LoadModule php5_module lib/apache/libphp5.so
AddType application/x-httpd-php .php
You may also want to update your DirectoryIndex to put PHP first
DirectoryIndex index.php index.html
PHP also has a configuration file. Fortunately default files are provided. It is up to you which you choose but in the source directory you have 2 options php.ini-development or php.ini-production. Read the comments at the top of each file, and pick which one you want, then copy it to the lib directory of the installation.
E.g.cp php.ini-development /usr/local/php/lib/php.ini
Then all you need to do is restart the webserver /etc/init.d/apachectl restart
Create a file in your website home directory and add the following:
<?php
phpinfo();
?>
Then point your web browser at it http://localhost/test.php
This should show a web page detailing the PHP installation. If it doesn`t, recheck the configuration changes above.