shokti

ubuntu 12.10 - install apache mod_mono to run asp.net 4.0

Jan 31st, 2013
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. install mono:
  2. sudo apt-get install mono-runtime
  3. sudo apt-get install mono-gmcs
  4.  
  5. Install mod_mono:
  6. sudo apt-get install libapache2-mod-mono
  7. sudo apt-get install mono-apache-server2
  8.  
  9. Enable mod_mono autoconfig:
  10. sudo a2enmod mod_mono_auto
  11.  
  12. For new installation this is all that's need to enable mod_mono.
  13. For older installation with existing Apache configuration files, make sure the following directives somewhere in one of Apache configuration files.
  14. For new installation the following are already included in /etc/mono-server4/mono-server4-hosts.conf, which is included in the main configuration file.
  15.  
  16. make sure this is in /etc/mono-server4/mono-server4-hosts.conf:
  17. MonoAutoApplication enabled
  18. MonoServerPath default /usr/bin/mod-mono-server4
  19.  
  20. Restart Apache:
  21. /etc/init.d/apache2 restart
  22.  
  23.  
  24. ------------------------------------------------------------------------------------------------
  25.  
  26. test if working:
  27.  
  28.  
  29. 1. create test.aspx:
  30. sudo touch /var/www/test.aspx
  31. sudo nano /var/www/test.aspx
  32.  
  33. add entry below, save, and browse in the browser:
  34.  
  35. <%
  36. HelloWorldLabel.Text = "Hello, world!";
  37. %>
  38. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  39. <html xmlns="http://www.w3.org/1999/xhtml" >
  40. <head runat="server">
  41. <title>Untitled Page</title>
  42. </head>
  43. <body>
  44. <form id="form1" runat="server">
  45. <div>
  46. <asp:Label runat="server" id="HelloWorldLabel"></asp:Label>
  47. </div>
  48. </form>
  49. </body>
  50. </html>
  51.  
  52.  
  53. 2. create test2.aspx:
  54. sudo touch /var/www/test2.aspx
  55. sudo nano /var/www/test2.aspx
  56.  
  57. add entry below, save, and browse in the browser:
  58.  
  59. <%@ Language=C# %>
  60. <HTML>
  61. <script runat="server" language="C#">
  62. void MyButton_OnClick(Object sender, EventArgs e)
  63. {
  64. MyLabel.Text = MyTextbox.Text.ToString();
  65. }
  66. </script>
  67. <body>
  68. <form id="MyForm" runat="server">
  69. <asp:textbox id="MyTextbox" text="Hello World" runat="server"></asp:textbox>
  70. <asp:button id="MyButton" text="Echo Input" OnClick="MyButton_OnClick" runat="server"></asp:button>
  71. <asp:label id="MyLabel" runat="server"></asp:label>
  72. </form>
  73. </body>
  74. </HTML>
  75.  
  76. -----------------------------------------------------------------------------------------------
  77. test with simple database web application:
  78.  
  79. install MySQL Connector/.Net driver:
  80. sudo apt-get install libmysql6.4-cil
  81.  
  82. Creating a Database to Test the MySQL Connector(use phpmyadmin):
  83.  
  84. CREATE DATABASE sample;
  85. USE sample;
  86. CREATE TABLE test (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(25));
  87. INSERT INTO sample.test VALUES (null, 'Lucy');
  88. INSERT INTO sample.test VALUES (null, 'Ivan');
  89. INSERT INTO sample.test VALUES (null, 'Nicole');
  90. INSERT INTO sample.test VALUES (null, 'Ursula');
  91. INSERT INTO sample.test VALUES (null, 'Xavier');
  92.  
  93.  
  94. create a test user named "testuser" and give that user access to the newly created sample database:
  95.  
  96. CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'somepassword';
  97. GRANT ALL PRIVILEGES ON sample.* TO 'testuser'@'localhost';
  98. FLUSH PRIVILEGES;
  99.  
  100.  
  101. create a file called testdb.aspx in /var/www/mydotnetsite:
  102. <%@ Page Language="C#" %>
  103. <%@ Import Namespace="System.Data" %>
  104. <%@ Import Namespace="MySql.Data.MySqlClient" %>
  105. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  106. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  107. <head>
  108. <title>ASP and MySQL Test Page</title>
  109. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  110.  
  111. <script runat="server">
  112. private void Page_Load(Object sender, EventArgs e)
  113. {
  114. string connectionString = "Server=127.0.0.1;Database=sample;User ID=testuser;Password=somepassword;Pooling=false;";
  115. MySqlConnection dbcon = new MySqlConnection(connectionString);
  116. dbcon.Open();
  117.  
  118. MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM test", dbcon);
  119. DataSet ds = new DataSet();
  120. adapter.Fill(ds, "result");
  121.  
  122. dbcon.Close();
  123. dbcon = null;
  124.  
  125. SampleControl.DataSource = ds.Tables["result"];
  126. SampleControl.DataBind();
  127. }
  128. </script>
  129.  
  130. </head>
  131. <body>
  132. <h1>Testing Sample Database</h1>
  133. <asp:DataGrid runat="server" id="SampleControl" />
  134. </body>
  135.  
  136. </html>
  137.  
  138.  
  139.  
  140. create a web.config file in /var/www/mydotnetsite(Please note that Custom Errors have been turned off in this web.config for debugging purposes. The customErrors mode line should be removed in a production environment.):
  141.  
  142. <configuration>
  143. <system.web>
  144. <customErrors mode="Off"/>
  145. <compilation>
  146. <assemblies>
  147. <add assembly="MySql.Data"/>
  148. </assemblies>
  149. </compilation>
  150. </system.web>
  151. </configuration>
  152.  
  153. -----------------------------------------------------------------------------------------------
  154. test with code behind sample:
  155.  
  156. create source test3.aspx:
  157. sudo touch /var/www/mydotnetsite/test3.aspx
  158. sudo nano /var/www/mydotnetsite/test3.aspx
  159.  
  160. add entry below, save.
  161.  
  162. <%@ Page language="c#" src="test3.aspx.cs"
  163. Inherits="HelloApp.HelloPage" AutoEventWireup="true" %>
  164. <html>
  165. <head>
  166. <title>First Mono ASP.NET Application</title>
  167. </head>
  168. <body>
  169. <form runat="server">
  170. Enter your name: <asp:TextBox id="name" runat="server" />
  171. <asp:Button id="greet" Text="Greet" onClick="OnGreetClick" runat="server"/>
  172. </form>
  173. <br /><strong><asp:Label id="message" runat="server">Hello, World!
  174. </asp:Label></strong>
  175. </body>
  176. <html>
  177.  
  178. create code behind test3.aspx.cs:
  179. sudo touch /var/www/mydotnetsite/test3.aspx.cs
  180. sudo nano /var/www/mydotnetsite/test3.aspx.cs
  181.  
  182. add entry below, save.
  183.  
  184. using System;
  185. using System.Web.UI.WebControls;
  186. namespace HelloApp
  187. {
  188. public class HelloPage : System.Web.UI.Page
  189. {
  190. protected Label message;
  191. protected Button greet;
  192. protected TextBox name;
  193. public void OnGreetClick(Object sender, EventArgs e)
  194. {
  195. message.Text = "Hello, " + name.Text;
  196. }
  197. }
  198. }
  199.  
  200. -----------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment