asp.net best practices across platforms

Source: Internet
Author: User
Tags get ip connect mysql mysql commands net socket version file permissions





Objective



Eight years of perseverance but the leadership of the stubborn, eventually had to be separated from my second language has become a C #, into the Java camp. There has been a brief loss and confusion, but the technology transformation is really not as difficult as imagined. Looking back, in fact, from the language itself, C # is indeed better than Java (not C # is a natural beauty, but it is standing on the shoulders of giants).



This article is not for. NET, but only to share the author in recent years, ASP.net cross-platform research and practical experience, is a eight years of the road to make a phased summary.



. NET technology since its inception, has been widely criticized for its poor cross-platform capabilities. There are Microsoft's intention to do so, there are some people with ulterior motives in confusing. NET was initially designed according to open language specifications, and with Microsoft's gradual compromise, the. NET Framework is completely open source. NET Cross-platform technology ushered in unprecedented opportunities, various. NET Cross-platform technology is bound to spring up like mushrooms. This article will introduce the author in. NET Cross-platform best practices, with the hope of reducing it. NET Cross-platform technical difficulty, let. NET truly become Cross-platform production technology.



Environment



Operating system Select Open Source community more representative Linux Server version CentOS (this article of the Cross-platform approach can be successfully applied to the vast majority of Linux systems, including domestic operating systems such as the winning Kirin); technology platform Choose prestigious mono Web application middleware chooses Chinese-made jexus. The environmental and technical details involved are:


    • Operating system: Centos_6.4_64bit

    • . NET Framework: mono_4.0.4.1

    • Web Application middleware: jexus_5.6.5

    • Database: mysql_5.1.73


Goal



The practice described in this article will implement ASP.net applications to deploy on Linux systems and make product attempts to make ASP.net applications easy to deploy and maintain on Linux platforms.



Web application



The Web application in this paper is developed using the ASP.net MVC 4 technology, and the persistence layer chooses Ado.net Entity Framework 6 technology. You can find it in the Src/demo directory.



Cross-platform deployment



One, Installation System



Minimize the installation of the CentOS system. As an experimental environment, you can consider the use of virtual machines with snapshot capabilities, the author selected the VMware virtual machine to test. System installation no longer repeat here. After the system is installed, a snapshot is left for subsequent verification of the correctness of the product installation package.



If you are not interested in implementing a Cross-platform deployment asp.net application, just want to see the results, you can skip the following steps and go directly to the product section to deploy the ASP.net application section through the installation package.



Second, Build Linux Compiling Environment



Because this article chooses the source code installs the mono, therefore needs to build the Linux compilation environment first. Building a Linux build environment requires the system to connect to the Internet and update the system.



First, you need to get your system connected to the Internet. Take a VMware virtual machine as an example to explain how to connect to the Internet. VMware virtual machines Connect to the Internet in a number of ways, the author chooses the Nat method, first need to set the virtual machine network connection way to NAT, as shown in Figure 1.






Figure 1 VMWare virtual Machine network connection mode settings



Set VMNET8 to automatically get IP, as shown in Figure 2.






Figure 2 set VMnet8 to automatically obtain an IP address



Then set the Linux virtual machine to DHCP to automatically assign IP, edit the network card configuration file:



[Root@localhost ~]# Vi/etc/sysconfig/network-scripts/ifcfg-eth0



Modify the configuration file as follows:



Device=eth0
Hwaddr=00:0c:29:f5:09:30
Type=ethernet
uuid=74b949f0-57bb-4baa-a5f2-2c97fb533a8b
Onboot=yes
Nm_controlled=yes
Bootproto=dhcp



Restart the network service for the NIC settings to take effect:



[Root@localhost ~]# Service Network restart



Confirm that the virtual machine is connected to the Internet:



[Root@localhost ~]# Ping yilin.cnblogs.com
PING yilin.cnblogs.com (42.121.252.58) bytes of data.
Bytes from 42.121.252.58:icmp_seq=1 ttl=128 time=77.7 ms
Bytes from 42.121.252.58:icmp_seq=2 ttl=128 time=78.1 ms
Bytes from 42.121.252.58:icmp_seq=3 ttl=128 time=77.5 ms



Update system:



[Root@localhost ~]# yum–y Update



Install the components required for the mono source installation:



[root@localhost ~]# yum-y install wget glib2-devel libtiff libtiff-devel libjpeg libjpeg-devel giflib giflib-devel libpng Libpng-devel libX11 libx11-devel freetype freetype-devel fontconfig fontconfig-devel libexif libexif-devel gcc-c++ Gette XT Unzip Zip



Third, installation GDI + Component



Before installing mono, you need to install the GDI + components that it relies on. Internet download Libgdiplus Source installation package (if the Linux system directly download the source package is stagnant, please return to the Windows system download Libgdiplus source installation package, and upload it to the Linux system after the corresponding path to install.) Subsequent installation of other components this situation is similar to this, no longer repeat:



[Root@localhost ~]# CD/USR
[Root@localhost usr]# wget http://download.mono-project.com/sources/libgdiplus/libgdiplus-2.10.tar.bz2



Decompression Libgdiplus Source installation package:



[Root@localhost usr]# tar jxvf libgdiplus-2.10.tar.bz2



Configure the Libgdiplus component installation path (this specifies the installation path, which is to prepare for the later production of the product installation package, Mono, Jexus and MySQL installation is also so, no longer repeat):



[Root@localhost usr]# CD libgdiplus-2.10
[Root@localhost libgdiplus-2.10]#./configure--prefix=/usr/apollo/hostd/mono/



Compile Libgdiplus Source code:



[Root@localhost libgdiplus-2.10]# make



To install the Libgdiplus component:



[Root@localhost libgdiplus-2.10]# make install



Four, installation Mono



Mono is the. NET Framework implementation of the Linux platform, which is the choice of a. NET program porting to a Linux platform. First, the Networking download Mono source installation package:



[Root@localhost libgdiplus-2.10]# CD/USR
[Root@localhost usr]# wget http://download.mono-project.com/sources/mono/mono-4.0.4.1.tar.bz2



Decompression Mono Source installation package:



[Root@localhost usr]# tar jxvf mono-4.0.4.1.tar.bz2



To configure the Mono installation path:



[Root@localhost usr]# CD mono-4.0.4
[Root@localhost mono-4.0.4]#./configure--prefix=/usr/apollo/hostd/mono



Compile mono source code (This process is typically half an hour to one hour, depending on the system hardware and software configuration):



[Root@localhost mono-4.0.4]# make



Install Mono:



[Root@localhost mono-4.0.4]# make install



Verify that Mono was successfully installed by viewing the mono version (the following version information indicates the mono installation was successful):



[root@localhost mono-4.0.4]# cd/usr/apollo/hostd/mono/bin/
[root@localhost bin]#./mono-v
Mono JIT compiler Version 4.0.4 (Stable 4.0.4.1/5ab4c0d Fri Oct 06:56:35 CST 2015)
Copyright (C) 2002-2014 Novell, Inc., Xamarin INC nd Contributors. www.mono-project.com
        tls:            __thread
        sigsegv:        altstack
        notifications:epoll
         architecture:  AMD64
        disabled:       None
        misc:           softdebug
        llvm:           supported, not enabled.
        gc:            SGen



Five, installation Jexus



Jexus is the ASP.net Web application middleware on the Linux platform developed by the people, similar to the Windows platform IIS. Practice has proved that Jexus installation is simple, stable operation, Linux platform is set up asp.net application of a good choice. First, the Networking download Jexus installation package:



[Root@localhost ~]# CD/USR
[Root@localhost usr]# wget http://www.linuxdot.net/down/jexus-5.6.5.tar.gz



Decompression Jexus installation package:



[Root@localhost usr]# tar zxvf jexus-5.6.5.tar.gz



To modify the Jexus installation path:



[Root@localhost usr]# CD jexus-5.6.5
[Root@localhost jexus-5.6.5]# VI Install



#!/bin/sh



src_dir=$ (CD $ (dirname $);p wd)
Dat_dir=${src_dir}/data
Jws_dir= '/usr/apollo/hostd/jexus '
...



Install Jexus:



[Root@localhost jexus-5.6.5]# sudo./install



Modify the configuration of the Jexus about the mono path:



[Root@localhost jexus-5.6.5]# Cd/usr/apollo/hostd/jexus
[Root@localhost jexus]# VI JWs



#!/bin/sh



jws_home=$ (CD $ (dirname $);p wd)



Export lang= "ZH_CN. UTF-8 "
Export path=/usr/bin:${jws_home}/. /mono/bin: $PATH
Export ld_library_path=/usr/lib:${jws_home}/. /mono/lib: $LD _library_path
...



Start the Jexus service and test that the Jexus installation is correct:



[Root@localhost jexus]#./jws start



Set up the Jexus service to boot from, add the following Gaoliang::



[Root@localhost jexus]# vi/etc/rc.d/rc.local



Touch/var/lock/subsys/local
/usr/apollo/hostd/jexus/jws start



Modify the file permissions and restart:



[Root@localhost jexus]# chmod +x/etc/rc.d/rc.local



Six, Deploying Web Sites



To create a site directory:



[Root@localhost jexus]# cd/usr/apollo/
[Root@localhost apollo]# mkdir WebApps
[Root@localhost apollo]# CD webapps/
[Root@localhost webapps]# mkdir Default
[Root@localhost webapps]# CD default/
[Root@localhost default]# Touch index.html
[Root@localhost default]# VI index.html



<title>asp. NET cross-platform best practices </title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<body>
Asp. NET cross platform best practices, see this page, that your first site has been successfully deployed to the Linux system.
</body>


Modify the Jexus Web site configuration:

[Root@localhost default]# cd/usr/apollo/hostd/jexus/siteconf
[Root@localhost siteconf]# VI Default
######################
# Web Site:default
########################################
Port=80
root=//usr/apollo/webapps/default
hosts=* #OR your.com,*.your.com
... 



Open firewall Port 80:


 

  
[root@localhost siteconf]# vi /etc/sysconfig/iptables  
 
# firewall configuration written by  system-config-firewall 
# manual customization of this file is  not recommended. 
*FILTER&NBSP;
: input accept [0:0]  
:FORWARD&NBSP;ACCEPT&NBSP;[0:0]&NBSP;
:output accept [0:0] 
-a input -m state --state established,related -j  accept 
-a input -p icmp -j accept 
-A  input -i lo -j accept 
-a input -m state --state new  -m tcp -p tcp --dport 22 -j accept 
-A  input -m state --state new -m tcp -p tcp --dport 80 -j  accept 
-a input -j reject --reject-with icmp-host-prohibited  
-a forward -j reject --reject-with icmp-host-prohibited  
commit 


Restart the iptables for the firewall settings to take effect:



[Root@localhost siteconf]#/etc/init.d/iptables Restart



Restart the Jexus service:



[Root@localhost siteconf]#/usr/apollo/hostd/jexus/jws Restart



Terminal Browser input URL http://ip:port/index.html visit the website:






Figure 3 asp.net Web site successfully deployed to Linux platform



Seven, Database



Linux platform Free relational database preferred MySQL, of course, first download the MySQL source installation package:



[Root@localhost siteconf]# CD/USR
[Root@localhost usr]# wget http://downloads.mysql.com/archives/get/file/mysql-5.1.72-linux-x86_64-glibc23.tar.gz



Unzip the MySQL source installation package to the specified directory and modify the directory name:


 
  
[Root@localhost usr]# tar zxvf mysql-5.1.72-linux-x86_64-glibc23.tar.gz-c/usr/apollo
[Root@localhost usr]# CD Apollo
[Root@localhost apollo]# MV mysql-5.1.72-linux-x86_64-glibc23 data


Copy the MySQL configuration file to the specified directory:


 
  
[Root@localhost apollo]# CD data
[Root@localhost data]# CP support-files/my-medium.cnf/etc/my.cnf



Edit the MySQL configuration file, adding the following Gaoliang in the [Client] section and the [Mysqld] section::


[ROOT@LOCALHOST&NBSP;DATA]#&NBSP;VI&NBSP;/ETC/MY.CNF&NBSP;
 
[CLIENT]&NBSP;
#password        =  your_password 
port             = 3306 
socket          = /tmp/ mysql.sock 
DEFAULT-CHARACTER-SET&NBSP;=&NBSP;UTF8&NBSP;
 
#&NBSP;HERE&NBSP;FOLLOWS&NBSP;ENTRIES&NBSP;FOR&NBSP;SOME&NBSP;SPECIFIC&NBSP;PROGRAMS&NBSP;
 
# the mysql server 
[mysqld] 
Socket           = /tmp/mysql.sock 
SKIP-LOCKING&NBSP;
key_buffer_size = 16m 
max_ allowed_packet = 1m 
table_open_cache = 64 
sort_ buffer_size = 512k 
net_buffer_length = 8k 
read_ buffer_size = 256k 
read_rnd_buffer_size = 512k 
basedir = /usr/apollo/data 
datadir = /usr/apollo/data/data 
character-set-server = utf8 
Li class= "alt" >collation-server = utf8_general_ci 
...  



Create MySQL group and user, and set Directory access permissions:


 
  
[Root@localhost data]# Groupadd MySQL
[root@localhost data]# useradd-g MySQL MySQL
[Root@localhost data]# chown-r MySQL.
[Root@localhost data]# chgrp-r MySQL.
[Root@localhost data]# chown-r root.
[root@localhost data]# chown-r MySQL Data


Initialize database:



[Root@localhost data]# scripts/mysql_install_db--user=mysql



To run the MySQL service:



[Root@localhost data]# bin/mysqld_safe--user=mysql &



This way to start MySQL is blocked, you need to open a separate session to the Linux system, continue to follow up.



Set Root user password:



[root@localhost data]# bin/mysqladmin-uroot Password 11111111



Set up the MySQL service to boot automatically:



[Root@localhost data]# CP Support-files/mysql.server/etc/rc.d/init.d/mysqld
[Root@localhost data]# chmod 700/etc/init.d/mysqld
[Root@localhost data]# chkconfig--add mysqld
[Root@localhost data]# chkconfig--level 345 mysqld on



You can now stop the MySQL service (shortcut key CTRL + C) that started the session before and start the MySQL service in the background using the service command.



[root@localhost data]# Service mysqld start



Add the MySQL command to the system environment variable and add the following two lines of code at the end of the file:



[Root@localhost data]# Vi/etc/profile



...
Path= $PATH:/usr/apollo/data/bin
Export



Perform the configuration and restart the system for the environment variable to take effect:



[Root@localhost data]# Source/etc/profile



After the Linux system restarts, the MySQL service can start automatically, and the MySQL command is included in the environment variable. At this point, the MySQL service can be accessed locally, through the MySQL command line to create databases, execute SQL files, and so on. In addition, the MySQL command-line configuration allows remote access to the MySQL database (this configuration is not possible when the Web service is together with the MySQL database service):



[Root@localhost ~]# mysql-uroot-p11111111
Mysql> grant all privileges in *.* to ' root ' @ '% ' identified by ' 11111111 ' with GRANT OPTION;
mysql> FLUSH privileges;
Mysql> exit;



You also need the firewall to open 3306 ports, edit the firewall rules file, and add the following Gaoliang to the firewall rules file:


 
  
[ROOT@LOCALHOST&NBSP;~]#&NBSP;VI&NBSP;/ETC/SYSCONFIG/IPTABLES&NBSP;
 
#&NBSP;FIREWALL&NBSP;CONFIGURATION&NBSP;WRITTEN&NBSP;BY&NBSP;SYSTEM-CONFIG-FIREWALL&NBSP;
# manual customization of this file is not recommended. 
*FILTER&NBSP;
: input accept [0:0] 
:FORWARD&NBSP;ACCEPT&NBSP;[0:0]&NBSP;
: output accept [0:0] 
-a input -m state --state established,related -j accept  
-a input -p icmp -j accept 
-a input  -i lo -j accept 
-a input -m state --state new  -m tcp -p tcp --dport 22 -j accept 
-A  input -m state --state new -m tcp -p tcp --dport 80 -j  accept 
-a input -m state --state new -m tcp -p tcp  --dport 3306 -j accept 
-a input -j reject - -reject-with icmp-host-prohibited 
-a forward -j reject --reject-with  icmp-host-prohibited 
COMMIT&NBSP;


To reboot the Firewall service:



[Root@localhost ~]#/etc/init.d/iptables Restart



The MySQL database can be accessed remotely at this time.



Eight, Deploy asp.net Application



This example provides an asp.net MVC application that is placed in the WebApps directory of the TAR packets produced in the following article, which is used to automate the creation of the database and the deletion of data from the Entity Framework. Upload the directory to the/usr/apollo/webapps directory of the Linux system and configure the Jexus Web site configuration:


 

  
[Root@localhost ~]# cd/usr/apollo/hostd/jexus/siteconf/
[Root@localhost siteconf]# CP Default Demo
[Root@localhost siteconf]# VI Demo
...
port=8080
root=//usr/apollo/webapps/demo
hosts=* #OR your.com,*.your.com
...


Jexus supports hosting multiple Web sites, so the new demo site here can coexist with the previously created default site, and only need to configure a different port number. The same need to open firewall port 8080:


 


  
[root@localhost siteconf]# vi /etc/sysconfig/iptables  
 
# firewall configuration written by  system-config-firewall 
# manual customization of this file is  not recommended. 
*FILTER&NBSP;
: input accept [0:0]  
:FORWARD&NBSP;ACCEPT&NBSP;[0:0]&NBSP;
:output accept [0:0] 
-a input -m state --state established,related -j  accept 
-a input -p icmp -j accept 
-A  input -i lo -j accept 
-a input -m state --state new  -m tcp -p tcp --dport 22 -j accept 
-A  input -m state --state new -m tcp -p tcp --dport 80 -j  accept 
-a input -m state --state new -m tcp -p  tcp --dport 8080 -j accept 
-a input -j  reject --reject-with icmp-host-prohibited 
-a forward -j reject -- reject-with icmp-host-prohibited 
COMMIT&NBSP;


Restart the iptables for the firewall settings to take effect:



[Root@localhost siteconf]#/etc/init.d/iptables Restart



Restart the Jexus service:



[Root@localhost siteconf]#/usr/apollo/hostd/jexus/jws Restart



You can now access the ASP.net MVC site through a browser in the terminal.






Figure 4 Linux environment Deployment asp.net MVC application



of product



Through the above series of steps, we realize the ASP.net MVC application in the Linux platform deployment, but this is only a technical research, from the production of a certain distance. This series of steps is too technical and needs to be networked and difficult to operate, which is difficult to achieve for both production and user environments. We need to be. NET Cross-platform technology makes ASP.net applications easy to deploy.



One, Initializing Scripts



As discussed earlier, ASP. NET application in Linux platform deployment needs to set firewall policy, MySQL users and groups, Jexus and MySQL service boot and so on, quite cumbersome. We can centralize these settings in an initialization script.


#!/BIN/SH&NBSP;
 
#echo off
Dir= $ ( cd $ ( dirname "${bash_source[0]}" ) " && pwd "
 
#创造软链接 to support deployment of an application to any directory
 
#Copy the previously configured MySQL configuration file to the specified directory.
CP -f $DIR/conf/my.cnf /etc/my.cnf
&NBSP;
#Create MySQL required users and user groups
GROUPADD&NBSP;MYSQL&NBSP;
Useradd -g mysql mysql
Li class= "alt" >chown -r root .
Chown -r mysql data
Chgrp -r mysql data
 
#Set MySQL service boot from boot
Cp $DIR/data/support-files/mysql.server /etc/rc.d/init.d/mysqld
CHMOD&NBSP;700&NBSP;/ETC/INIT.D/MYSQLD&NBSP;
Chkconfig --add mysqld
CHKCONFIG&nbsp;--LEVEL&NBSP;345&NBSP;MYSQLD&NBSP;ON&NBSP;
 
#Add MySQL commands to system environment variables
Sudo cat $DIR/conf/profile >> /etc/profile
SOURCE&NBSP;/ETC/PROFILE&NBSP;
 
#设置 Firewall Policy
Cp -f $DIR/conf/iptables /etc/sysconfig/iptables
&NBSP;
#Set the Jexus service to boot from the boot
Sudo cat $DIR/conf/rc.local >> /etc/rc.d/rc.local
Chmod +x /etc/rc.d/rc.local
Chmod a+x $DIR/hostd/jexus/jws
Chmod a+x -r $DIR/hostd/mono/bin
&NBSP;
#Restart the system
REBOOT&NBSP;


Second, Make installation package



The technology that deploys the Linux platform asp.net application needs includes mono, Jexus, MySQL, etc. integration and package distribution.



[Root@localhost ~]# tar jcvf Crossplatform-1.0.0.0-centos6.4-x86_32_64.tar.bz2/usr/apollo



Third, Deploying by installing packages asp.net Application



At this point, the Linux Platform Web application deployment package is ready to download to the terminal. Restore the virtual machine to the initial installation snapshot state, and then upload the deployment package on the terminal to the Linux system and unzip:



[Root@localhost usr]# tar jxvf crossplatform-1.0.0.0-centos6.4-x86_32_64.tar.bz2



Execute initialization script Complete installation:



[Root@localhost usr]# CD Apollo
[Root@localhost apollo]# Sh./install



In the terminal through the browser to access the ASP.net MVC site, confirm deployment success.






Figure 5 Linux asp.net MVC application of platform product deployment



Summarize



This article explains in detail the best practices for the Linux platform deployment asp.net application, through which you understand all aspects of the ASP.net application Cross-platform deployment, which also applies to other Linux distributions and native operating systems. In addition, the Linux platform deployment asp.net application of the product packaging, the production of the installation package can be directly applied to product packaging, you simply put the application into the specified directory. I hope the techniques described in this article are helpful to you.



Attachment Download: Sample source + PDF version of this article



Attachment Download: Product Deployment Tar Pack





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.