# Guide: Setting up LAMP in vmware for development/deployment



## streetfighter 2 (Feb 19, 2011)

*Setting up rPath LAMP VMware Appliance for Development*
*What’s LAMP?*
LAMP is an acronym which stands for Linux Apache MySQL Perl/PHP/Python.  The Windows version of LAMP is . . . WAMP.  If you’re not sure what any of that is or why it’s useful, you should probably move along . 

*What’s rPath?*
rPath is a company that happens to provide a linux distribution aptly named rPath Linux.  rPath uses the conary package system.

*What’s it to you?*
I needed a safe, simple and free development environment for SQL and my various ASP.NET/PHP web apps, and this post explains how I went about it.  The rPath LAMP image isn’t particularly special (or is it?), it was just the first one to pop-up on a google search.  Be warned, security is not a priority in this tutorial!

*Prerequisites to use this tutorial*
-basic understand of file structure in Windows/linux
-basic file/folder manipulation skills in bash
-WinSCP or other such file transfer client
-some experience using vmware player
-rudimentary scripting capabilities (editing XML files, etc.)
-some conceptual knowledge of SQL, linux, apache, .NET/mono, etc. is a plus 

*LAMP version information used in tutorial*


*Component*
|
*Version*

rPath LAMP|3.0.1
kernel|2.6.29.3
apache|2.2.9
mono|1.2.6
mod_mono|1.2.1
php|5.2.8
vbnc (optional)|2.1 (see attached)
MySQL|5.0.51a

This is my 1000th post.  I hope you find it useful! 



*Configuration*
[HIGHLIGHT]*NOTICE:*[/HIGHLIGHT] Other than the first two steps, none of the steps are required to run rPath LAMP.  If you do not care for a particular feature that I added then simply skip the step.

*1* [HIGHLIGHT]Getting started[/HIGHLIGHT]
Download and install vmware player if you don’t have it.
Download and unzip LAMP vmware appliance where ever you want to run it from.

*2* [HIGHLIGHT]Accessing phpMyAdmin[/HIGHLIGHT]

Boot up the LAMP VM by selecting it through vmware player or double-click the _vmx_ file.  You're almost done !
(If you’re following the official instructions it’ll tell you to login to the web interface to configure your password.  I’d recommend doing that if you’re making it publicly accessible.)
Once the VM is booted up you’ll see a screen to login; ignore it.  Instead, hit ctrl+alt+space (to disable the ctrl+alt key combo) followed by ctrl+alt+f2.  Doing so will bring you to the *real* login screen which gives you terminal access.  
Use the login “root" with no password.  I’d suggest setting passwords for the _root_ and _web_ account:

```
passwd root
Passwd web
```

Once a password is set for the _web_ account you can use it to login to phpMyAdmin by browsing to _http://<vm_ip>/phpMyAdmin/_

*3* [HIGHLIGHT]Adding MySQL exception to firewall rules[/HIGHLIGHT]
Even though phpMyAdmin is an excellent administration tool it’s still not as interactive as MySQL Workbench and it lacks the charm of the classic MySQL CLI.  If you agree with me you’ll need to add an exception for MySQL in the firewall rules.  The order of rules listed in iptables is important, which is why the last rule needs to be temporarily removed.  Here are the commands:

```
iptables -D RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
iptables -A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
iptables -A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
service iptables save
```

*4* [HIGHLIGHT]Using ASP.NET v1.0/1.1 assemblies (precompiled or C#)[/Highlight]
rPath comes with mod_mono preconfigured, but it only supports .NET v1.0/1.1 assemblies that are precompiled or written in C#.  All you need to do to is put your _aspx_ and _cs_ files in _/srv/www/html_.

*5* [HIGHLIGHT]Changing mod_mono to ASP.NET v2.0[/Highlight]

Open _/etc/httpd/conf.d/mod_mono.conf_
Add the following line just below _AddMonoApplications default "/:/srv/www/html"_:

```
MonoServerPath default /usr/bin/mod-mono-server2
```

To enable debug information open _/etc/mono/2.0/web.config_
Add _<customErrors mode="Off"/>_ near the top according to:

```
<configuration>
	  <system.web>
		<customErrors mode="Off"/>
```

Restart the httpd service with the command:

```
service httpd restart
```


*6* [HIGHLIGHT]Adding MySQL Connector/NET to mono[/Highlight]
(This won’t work unless you completed step 5.)

Download Connector/NET for _.NET & Mono_.  The file should be named something like _mysql-connector-net-6.3.6-noinstall.zip_,
Copy the file _mysql.data.dll_ in the zip’s _v2_ folder to the LAMP VM (I suggest _~/connector-net_).
Run the following commands to correct the case and permissions:

```
mv mysql.data.dll mysql.data.dll.old
cp mysql.data.dll.old MySql.Data.dll
rm mysql.data.dll.old
chmod ugo+x MySql.Data.dll
```

(OPTIONAL) Copy _MySql.Data.dll_ to _/usr/lib_ (in case you get reference errors).
Register _MySql.Data.dll_ so it can be used in mono using the following command:

```
gacutil /i MySql.Data.dll
```

Open _/etc/mono/2.0/web.config_ and add the line _<add assembly="MySql.Data" />_ like so:

```
<configuration>
	  <system.web>
		<compilation>
		  <assemblies>
			<add assembly="MySql.Data" />
```

Restart the httpd service with the command:

```
service httpd restart
```


*7* [HIGHLIGHT]Adding VB.NET support to mod_mono[/Highlight]
(This won’t work unless you completed step 5.)

Download the pre-packed vbnc for rPath.
Unzip it and copy it somewhere innocuous on the LAMP VM (like _~/vbnc_)
I’ve included an install script for _vbnc_.  You’ll need to run it as root:

```
chmod 755 ./install.sh
sudo ./install.sh
```

Assuming the installer completes successfully, just restart the httpd service:

```
service httpd restart
```



*Examples*
*Script Examples*
To use the following examples you must have completed steps 1, 2, 5 & 6.  You will also need a single table database that I provided with the included tpu.sql.  (My example database is only for proving the functionality of the example scripts and is not suitable for learning SQL.  If you’re interested in learning SQL try the sakila sample db.)

*Loading the sample database*
I suggest using phpMyAdmin which can be found by opening a browser and going to the following URL:
http://<vm_ip>/phpMyAdmin/

Login to phpMyAdmin with the _web_ account and the password you made in step 2 of the configuration.
Click on the _Import_ link on the main page.
Click the button _Choose..._ and browse to the location of _tpu.sql_ that you downloaded.
Press _Go_ and the database will be created and the data entered.
All the tutorial scripts were written for user _tut_user_ with password _password_.  To simplify the tutorial process repeat steps 3 & 4 of this section with the file _tut_user.sql_.

_phpSQL.php_ – A php based MySQL query example

```
<html>
  <head>
    <meta charset="utf-8" />
    <title>php MySQL Database Query</title>
  </head>
  <body>
	<?php
	$db = mysql_connect('localhost', 'tut_user', 'password') or die(mysql_error());
	mysql_select_db ('tpu', $db) or die(mysql_error());
	
	$output = "<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse\\;\">\n";
	$output .= "<tr><td>thread</td><td>time_rel</td><td>user_name</td><td>posts</td></tr>\n";
	$query = mysql_query('SELECT * FROM latest',$db) or die(mysql_error());
	While( $rows = mysql_fetch_array($query)) {
	  $thread      = $rows['thread'];
	  $time_rel    = $rows['time_rel'];
	  $user_name      = $rows['user_name'];
	  $posts      = $rows['posts'];
	  $output   .= "<tr><td>$thread</td><td>$time_rel</td><td>$user_name</td><td>$posts</td></tr>\n";
	}
	mysql_free_result($query);
	mysql_close($db);
	
	echo "$output\n";
	echo "</table>\n";
	?>
	</body>
</html>
```

_csSQL.aspx_ – An ASP.NET C# based MySQL query example

```
<%@ Page Language="C#" debug="true" %>
<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "MySql.Data.MySqlClient" %>
<script language="C#" runat="server">
	protected override void OnLoad(EventArgs e)
	{
		DisplayQuery();
		base.OnLoad (e);
	}

	private void DisplayQuery()
	{
		DataSet dataset = new DataSet();
		string connectionString =
		  "Server=localhost;" +
		  "Database=tpu;" +
		  "User ID=tut_user;" +
		  "Password=password;" +
		  "Pooling=false";
		string sql = "SELECT * " + "FROM latest";
		MySqlConnection conn = new MySqlConnection(connectionString);
		MySqlDataAdapter adapter = new MySqlDataAdapter();
		adapter.SelectCommand = new MySqlCommand(sql, conn);
		adapter.Fill(dataset);

		MySQLDataGrid.DataSource = dataset;
		MySQLDataGrid.DataBind();
	}
</script>

<html>
	<head>
		<title>C# MySQL Database Query</title>
	</head>
	<body>
		<form runat="server">
			<asp:DataGrid id="MySQLDataGrid" runat="server" />
		</form>
	</body>
</html>
```

_vbSQL.aspx_ – An ASP.NET VB based MySQL query example (requires step 7)

```
<%@ Page Language="VB" debug="true" %>
<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "MySql.Data.MySqlClient" %>
<script language="VB" runat="server">

Sub Page_Load(sender As Object, e As EventArgs)

    Dim myConnection  As MySqlConnection
    Dim myDataAdapter As MySqlDataAdapter
    Dim myDataSet     As DataSet

    Dim strSQL        As String
    Dim iRecordCount  As Integer

    myConnection = New MySqlConnection("Server=localhost;Database=tpu;User ID=tut_user;Password=password;Pooling=false;")

    strSQL = "SELECT * FROM latest;"

    myDataAdapter = New MySqlDataAdapter(strSQL, myConnection)
    myDataSet = New Dataset()
    myDataAdapter.Fill(myDataSet, "latest")

    MySQLDataGrid.DataSource = myDataSet
    MySQLDataGrid.DataBind()

End Sub

</script>

<html>
	<head>
		<title>VB MySQL Database Query</title>
	</head>
	<body>
		<form runat="server">
			<asp:DataGrid id="MySQLDataGrid" runat="server" />
		</form>
	</body>
</html>
```

That’s all for now.  I have code-behind examples that I may upload later.  Please help yourself to the references that I used in writing this tutorial, and have fun!

*References:*
http://wiki.rpath.com/wiki/Appliance:Apache_Appliance
http://docs.rpath.com/rbuilder/5.5/rBuilder_Administration_Guide/ch-command-line_access.html
http://wiki.rpath.com/wiki/Appliance:LAMP_Appliance
http://wiki.rpath.com/wiki/Appliance_Talk:LAMP_Appliance
http://www.codeproject.com/KB/cross-platform/introtomono2.aspx
http://www.mono-project.com/Mod_mono#ASP.NET_2_apps_do_not_work
http://dev.mysql.com/doc/refman/5.1/en/connector-net-installation.html
http://www.mono-project.com/FAQ:_ASP.NET
http://msdn.microsoft.com/en-us/library/015103yb.aspx
http://www.mono-project.com/VisualBasic.NET_support
http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples.html
https://github.com/mono/mono-basic/archives/master


----------

