Even in this day and age, people still view webservers as some sort of witchcraft. In reality, on a Linux server, it is very straight-forward to set up a basic LAMP server (Linux Apache MySQL PHP) and you could be ready to roll in a few minutes.
With a LAMP set-up, you can host numerous web applications (e.g. content management systems like wordpress/joomla etc).
Such web applications can be very useful for small office setups if you need to control document access on your LAN or even if you want to make it accessible to the wider world.
So, lets set up a basic LAMP server so you can see how easy it is.
Step 1. Linux
So, the L in LAMP stands for Linux. I have set-up a CentOS/RHEL box for this demo (but frankly, the distro will matter little).
Its a standard basic install with no X-Windows (although you can have a graphical desktop if you wish).
I have installed no extra packages as this will be part of the set-up. The iptables firewall and SELinux have been switched off.
Step 2. Apache
Apache is by far the most popular webserver. Netcraft.com carry out annual surveys and Apache is consistently the market leader.
Apache handles the web requests and serves the appropriate pages/directories as you have configured.
Step 3. MySQL
MySQL is the database back-end used by a myriad of web applications.
If you host a WordPress site, the information you enter is not stored in static web pages… it is pulled from the MySQL back end to create the page according to the PHP script (which leads us on to…)
Step 4. PHP
PHP: Hypertext Preprocessor are basically scripts that pull information to dynamically create web pages (well, they can do more but lets leave it there)
PHP will need to have access to the MySQL db in order to run any web apps that need a db.
Install the applications
Use yum to install Apache (httpd), MySQL (mysql-server) and php-mysql which allows PHP to connect to the MySQL db:
[root@lab ~]# yum install php php-mysql mysql-server httpd Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.manchester.icecolo.com * extras: mirrors.manchester.icecolo.com * updates: mirrors.manchester.icecolo.com base | 1.1 kB 00:00 extras | 2.1 kB 00:00 updates | 1.9 kB 00:00 Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package httpd.i386 0:2.2.3-65.el5.centos.3 set to be updated ---> Package mysql-server.i386 0:5.0.95-1.el5_7.1 set to be updated --> Processing Dependency: mysql = 5.0.95-1.el5_7.1 for package: mysql-server --> Processing Dependency: perl(DBI) for package: mysql-server --> Processing Dependency: perl-DBD-MySQL for package: mysql-server --> Processing Dependency: libmysqlclient.so.15(libmysqlclient_15) for package: mysql-server --> Processing Dependency: perl-DBI for package: mysql-server --> Processing Dependency: libmysqlclient_r.so.15(libmysqlclient_15) for package: mysql-server --> Processing Dependency: libmysqlclient.so.15 for package: mysql-server --> Processing Dependency: libmysqlclient_r.so.15 for package: mysql-server ---> Package php-mysql.i386 0:5.1.6-39.el5_8 set to be updated --> Processing Dependency: php-common = 5.1.6-39.el5_8 for package: php-mysql --> Processing Dependency: php-pdo for package: php-mysql --> Running transaction check ---> Package mysql.i386 0:5.0.95-1.el5_7.1 set to be updated ---> Package perl-DBD-MySQL.i386 0:3.0007-2.el5 set to be updated ---> Package perl-DBI.i386 0:1.52-2.el5 set to be updated ---> Package php-common.i386 0:5.1.6-39.el5_8 set to be updated ---> Package php-pdo.i386 0:5.1.6-39.el5_8 set to be updated --> Running transaction check ---> Package php.i386 0:5.1.6-39.el5_8 set to be updated --> Processing Dependency: php-cli = 5.1.6-39.el5_8 for package: php --> Running transaction check ---> Package php-cli.i386 0:5.1.6-39.el5_8 set to be updated --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: mysql-server i386 5.0.95-1.el5_7.1 updates 9.8 M php-mysql i386 5.1.6-39.el5_8 updates 87 k php i386 5.1.6-39.el5_8 updates 2.3 M Updating: httpd i386 2.2.3-65.el5.centos.3 updates 1.2 M Installing for dependencies: mysql i386 5.0.95-1.el5_7.1 updates 4.9 M perl-DBD-MySQL i386 3.0007-2.el5 base 148 k perl-DBI i386 1.52-2.el5 base 600 k php-common i386 5.1.6-39.el5_8 updates 154 k php-pdo i386 5.1.6-39.el5_8 updates 66 k php-cli i386 5.1.6-39.el5_8 updates 2.1 M Transaction Summary ================================================================================ Install 7 Package(s) Upgrade 1 Package(s) Total download size: 17 M Is this ok [y/N]: y
You will notice there are many dependencies…. this is a good thing as it saves you specifying each one individually.
Start up the servers
So, you now have Apache and MySQL installed. Great. You need to start them up though right?
Run the following to start up the servers:
[root@lab ~]# /etc/init.d/httpd start Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName [ OK ] [root@lab ~]# /etc/init.d/mysqld start Initializing MySQL database: Installing MySQL system tables... OK Filling help tables... OK To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands: /usr/bin/mysqladmin -u root password 'new-password' /usr/bin/mysqladmin -u root -h lab password 'new-password' Alternatively you can run: /usr/bin/mysql_secure_installation which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers. See the manual for more instructions. You can start the MySQL daemon with: cd /usr ; /usr/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd mysql-test ; perl mysql-test-run.pl Please report any problems with the /usr/bin/mysqlbug script! The latest information about MySQL is available on the web at http://www.mysql.com Support MySQL by buying support/licenses at http://shop.mysql.com [ OK ] Starting MySQL: [ OK ] [root@lab ~]#
You will notice the message ‘PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !‘
There is an option to run the script /usr/bin/mysql_secure_installation
You certainly want to do this if this server is going on the wider internet as having a default password and a test db is asking for a hacker to exploit.
Apache also shows ‘Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1 for ServerName‘. This is nothing to worry about at this stage (as we haven’t configured it yet).
Start the daemons at boot
So, we want Apache and MySQL to run when the server restarts so use chkconfig to start the daemons at the appropriate run-levels.
[root@lab ~]# chkconfig --level 2345 httpd on [root@lab ~]# chkconfig --level 2345 mysqld on
This start them at init level 234 and 5 (ie every init level)
Test out Apache
So the webserver should be up and running. There is a simple way to test this. Open up a browser and type in http://IP_OF_SERVER or if you are on the box, http://localhost
You should see ‘Apache 2 Test Page powered by CentOS’ or similar.
Apache logs to /var/log/httpd. If you need to troubleshoot, look in here for access_log and error_log. /var/log/messages may show other issues if present.
A ‘good’ result from the access_log:
127.0.0.1 - - [30/Dec/2012:16:02:31 +0000] "GET /icons/apache_pb.gif HTTP/1.1" 200 2326 "http://localhost/" "Mozilla/5.0 (X11; Linux i686; rv:10.0.11) Gecko/20121120 Firefox/10.0.11"
You can see the 200 OK http message. All good.
On the test page, you should see a blurb like this:
If you are the website administrator:You may now add content to the directory /var/www/html/. Note that until you do so, people visiting your website will see this page and not your content. To prevent this page from ever being used, follow the instructions in the file /etc/httpd/conf.d/welcome.conf
So, as the man says, look at the /etc/httpd/conf.d/welcome.conf file:
# This configuration file enables the default "Welcome" # page if there is no default index page present for # the root URL. To disable the Welcome page, comment # out all the lines below. # <LocationMatch "^/+$"> Options -Indexes ErrorDocument 403 /error/noindex.html </LocationMatch>
Simple enough then, just add a # to the start of the lines:
# This configuration file enables the default "Welcome" # page if there is no default index page present for # the root URL. To disable the Welcome page, comment # out all the lines below. # #<LocationMatch "^/+$"> # Options -Indexes # ErrorDocument 403 /error/noindex.html #</LocationMatch>
Once you have this done, you will need to restart apache in order for it to take effect (this is true of any config changes).
So, either execute this:
[root@lab ~]# /etc/init.d/httpd restart
[root@lab ~]# apachectl restart
With Apache restarted, you should see a file listing of the directory you are hosting the web pages on (/var/www/html by default). You can place a file in here and you should see it in your browser.
Apache config is huge. I do not claim to be a mogul on it. If I lived to be 90, I still wouldn’t know half the configurations it can do.
There are however, a few fundamental points to understand.
The main config file is /etc/httpd/conf/httpd.conf. Some useful directives are:
|Listen 80||The port the web server listens for connections (default 80 and 443 for https)|
|Servername||Your servers DNS name|
|DocumentRoot||The directory where the web pages are stored|
|DirectoryIndex||The name of the files which the webserver will display when contacted without a specific file/page (normally index.html, index.htm, index.php, default.asp, home.htm etc)|
Do note that if you make any config changes, you must restart apache for the changes to take effect. You can restart the daemon or alternatively you can ‘gracefully’ reload the config using:
# /etc/init.d/httpd reload
# apachectl -k graceful
Now Apache is running as you want, you may want to test PHP (although this is not a daemon).
You can do this by creating a php info page which will show the full configuration as well as the valid db connections.
It is very easy to do….
Simply create a file called info.php and enter the following text:
<?php // Show all information, defaults to INFO_ALL phpinfo(); ?>
Place this file in the web directory (/var/www/html) and then click on it in your browser. It should show all PHP config.
So, what are you waiting for? That is a basic LAMP server set up.
You can now install web applications/CMS and what ever else you see fit. It is certainly suitable for a LAN and depending on your firewall, it may also be OK for the big bad web.
If you are going out on the internet, you may wish to consider SSL encryption for logins etc but that is for another day. Just start using your LAMP server.