CPE111 COMPUTER EXPLORATION BUILDING A WEB SERVER ASSIGNMENT You will create your own web application on your local web server in your newly installed Ubuntu Desktop on Oracle VM VirtualBox. This is a common task in computer professional everyday job. You can pair up with your friend to create a group. You will capture screen shots and create a report about results of various steps in installing softwares, configuring services or daemons, installing database management tool, create your database, test html, test php, and creating your web application. You can create a document in either doc or PDF format. Name your file with section follows by last three digits of your id and ends it with webserver. For example, A_111_222_webserver, AE_111_222_webserver. Submit your report in lms at the end of the session. INTRODUCTION After you have built a server platform, the next task is to configure server applications and user right or access. We are going to explore the alternative to start up a server of your own for your web development. You could use Microsoft products or use could use open source products. The trio that is very popular as web server and database server are Apache, PHP, and MySQL. The packages are available on both Microsoft and Linux environment. However, in the future, you may have a higher chance to be exposed to MS platform, so we are going to explore the alternative open source on Linux. Since you already have your Ubuntu Desktop installed, you only need to install a couple more softwares mentioned earlier. Apache is the name of a very popular httpd server which allows you to develop your website with HTML. Meanwhile, your data will be stored and managed by a database management system called MySQL. To retrieve data from MySQL server and create HTML webpages, a popular script language called PHP is used. We are going to explore how this platform works by installing, configuring, and developing a web site. GETTING STARTED You should already have your Ubuntu Desktop installed in your VM from the previous session. If you don t have or delete it, you will have to create a VM and install Ubuntu Desktop again. GAIN YOUR SUPER POWER After log in with your username and password, you will have to perform various administrative tasks to setup your web server and your database server. To do so, you require a super user (normally called root) privilege which can be gained with sudo and su command. Usually, su will change your privilege to other user (think of it as you can change yourself to someone else), in this case, super user (or root). However, you have to know the target user s password to use su (which you don t obviously). Instead of su, sudo will run a command with super user privilege with your own password (if you are allowed to do so, complicated stuffs, huh?). You will capture your screen shot
have to use sudo to run su command so that you don t have to know super user s password. In linux system, the super user account is root. First of all, you have to gain access to a Terminal or xterm. Use your dash home to launch a terminal. You now can access to a command shell in linux system where you can type in various commands. To gain super user privilege, use the following command which is running su with sudo. This will allow you to use your password to gain root access. $sudo su - After this command, your prompt will change from $ to # indicating that you now have root privilege. Be warned, after this step, you can pretty much destroy everything on your linux if you do something wrong. CHECK YOUR SOFTWARES We are going to install three softwares which are apache web server, php, and mysql server. First you have to verify if they are already installed. Ubuntu is a Debian-based linux which uses dpkg command to check for installed packages. The following command will show the package that you try to locate. #dpkg --get-selections apache #dpkg --get-selections php #dpkg --get-selections mysql If you don t have any result, you will need to install the missing software, for our case, all of them. It is performed by apt-get command as you can type it at the command prompt follows by install and application name. After each command, follow the installing guide instructions. Be sure to jot your mysql password down. After installing the applications, you should verify that they are properly installed. Use the following dpkg command. #apt-get install apache2 # apt-get install mysql-server # apt-get install php5 #apt-get install php5-mysql #dpkg --get-selections apa php mysql CONFIGURING SERVERS At this step, you will have to configure your servers so that they can host your web application in your user account. capture your screen shot
APACHE2 You have to enable UserDir support for apache2 httpd server. You have to create a symbolic link to available configuration. First change your directory to /etc/apache2/mods-enabled and verify if you are there. Follow these two commands for this step. #cd /etc/apache2/mods-enabled #pwd Once you are there, create symbolic links to userdir configurations from mods-available. Use this command to do so. You can also use apache utility a2enmod to do it. Pick either ln or a2enmod #ln s../mods-available/userdir.. #a2enmod userdir #ls l userdir. #xedit php5.conf & You should now have the configuration files. You will have to check php5.conf under the same directory and make sure that you comment the part that disable php engine out. Use xedit to open the php5.conf file and comment the part. Then it is time to restart the httpd server with the following command. #/etc/init.d/apache2 restart #service mysql restart At this point, we are going to test our newly created web server. First of all, we are going to gain access to another terminal on a different desktop by clicking at workspace icon. Pick one desktop that is empty and launch another terminal with dash home. You should notice that your prompt in this new terminal is $ indicating that this terminal is running as yourself. At this step you have to create a folder named public_html under your root directory (~) with this command. Then you will try to create your index.html in there with the following xedit command. $mkdir ~/public_html $xedit ~/public_html/index.html & In the xedit window, put the following html tags in the editor and save. After that you have to run a web browser by clicking firefox icon. capture your screen shot
Enter the following URL into firefix address. http://localhost/~yourname/. It should work, otherwise you have to check each steps again to make sure that you didn t do anything wrong. Your httpd server preparation is done at this point. MYSQL SERVER AND PHPMYADMIN With the current firefox browser still active, enter www.phpmyadmin.net into address bar. Download.gz version and save it. It should be under ~/download/. We are going to extract it to ~/public_html with the following commands from your normal shell. $cd ~/public_html $tar xzf ~/Downloads/phpMyAdmin-3.5.3-all-languages.tar.gz $ln s phpmyadmin-3.5.3-all-languages phpmyadmin $ ls -l Now go back to your firefox browser, put localhost/~yourname/phpmyadmin as URL then go. If there is nothing wrong, you should have a login page to MySQL databse. Use root as the user with the password you jotted down. Take another screen shot here. DATABASE CREATION AND DATA INSERTION We are going to create a database in MySQL DBMS server to store our tables and data. Click at databases and put database name under create database label. Hit create button you will see a message. You will notice your new database under Databases, click it. On the top, you will notice your database name after localhost. Create a table called topics with two columns. First column is tp_id and second column is tp_desc. Change tp_desc type to varchar with 256 as length. Click on the name of your new table you should see two fields of your table. Select the checkbox in front of tp_id capture your screen shot
then select add primary key under More. Now click insert on the top tabs and insert couple of records of data. Just put integer for tp_id and string for tp_desc then click go button. Now you can click at Browse and you should see your records of data. Take another screen shot here. PHP PAGE WITH DATA FROM MYSQL You are about to create a php page that retrieve records from topics table. First of all, your file will be named test.php under public_html. Use xedit to create it like this. $xedit ~/public_html/test.php & The first line of this file will be <? and the last line will be?>. In between is the code to :- 1. Connect to MySQL, 2. select a database, 3. perform a query, 4. Close connection, then 5. show the result. Step 1: set some variables and connect to MySQL $username="username"; $password="password"; $database="your_database"; mysql_connect( localhost,$username,$password); Step 2: select database @mysql_select_db($database) or die( "Unable to select database"); Step 3: perform a query $query="select FROM topics"; $result=mysql_query($query); $num=mysql_numrows($result); Step 4: close database capture your screen shot
mysql_close(); Step 5: show result $i=0; while ($i < $num) { $tp_id=mysql_result($result,$i,"tp_id"); $tp_desc=mysql_result($result,$i,"tp_desc"); echo "<b>$i.</b> $tp_id $tp_desc<br>"; $i++; } Combine these steps into test.php then save the file. Now you can go back to your browser and put localhost/~yourname/test.php in as URL. If there is nothing wrong, you should see a list of your data in the browser page. Capture a screen shot here also. EXTRA 1. Can you format your result in a table? 2. Can your show records in alternating colors? E.g odd number in blue, even number in green. 3. Can you create a group of checkboxes from tdoehe list you retrieved? capture your screen shot