From Rails to Erlyweb - Part II Manage Project - Reloaded

The migrating from Rails to Erlyweb of our project is going to be finished. I got more experience on how to deal with Erlyweb. First, the project management can be more straightforward. Here is it:

2. Manage project - Reloaded

Erlyweb provides erlyweb:compile(App, ..) to compile the source files under app directory. To start an app, you usually should erlydb:start(mysql, ....) and compile app files first. To make life easy, you can put some scripting like code under myproject\script directory. Here's my project source tree:

myproject
  + apps
  |   + myapp
  |       + ebin   
  |       + include
  |       + nbproject
  |       + src
  |           + components
  |           + lib
  |           + services
  |       + test
  |       + www
  + config
  |   * yaws.conf
  |   * erlyweb.conf
  + script
      + ebin
      + src
           * erlyweb_app.erl

Where, config/yaws.conf contains the yaws' configuration. Here's mine:

ebin_dir = D:/myapp/trunk/script/ebin

<server localhost>
  port = 8000
  listen = 0.0.0.0
  docroot = D:/myapp/trunk/apps/myapp/www
  appmods = </myapp, erlyweb>
        start_mod = erlyweb_app
  <opaque>
    appname = myapp
                environment = development
        </opaque>
</server>

You may have noticed, all beams under D:/myapp/trunk/script/ebin will be auto-loaded when yaws starts up. And you can prepare another yaws.conf for test or production environment by change the environment var in opaque

Now the config/erlyweb.conf:

{pa, ["script/ebin", 
      "apps/myapp/ebin", 
      "vendor/erlyweb/ebin", 
      "vendor/eunit/ebin"]}.

{i, ["vendor", 
     "apps/myapp/include",
     "/usr/local/lib/yaws"]}.

{production,  [{dbdriver, mysql},
               {database, "mydb_production"},
               {hostname, "localhost"}, 
               {username, "mememe"},
               {password, "pwpwpw"}]}.

{development, [{dbdriver, mysql},
               {database, "mydb_development"},
               {hostname, "localhost"}, 
               {username, "mememe"},
               {password, "pwpwpw"}]}.

{test,        [{dbdriver, mysql},
               {database, "mydb_test"},
               {hostname, "localhost"}, 
               {username, "mememe"},
               {password, "pwpwpw"}]}.

erlyweb_app.erl is the boot scripting code, which will be used to start db connection and compile the code. Currently I run these scripts manually. I'll talk later.

Notice: erlyweb 0.6.2 needed, which contains Haoboy's logfun patch.

%% @doc Main entrance to the entire erlyweb application.
-module(erlyweb_app).

-export([start/1]).

-export([get_conf/1,
         build/1,
         build_test/1, 
   build_product/1,
   environment/1,
   decompile/2,
   db_log/4,
   db_dummy_log/4
        ]).

-include("yaws/include/yaws.hrl").
-include("yaws/include/yaws_api.hrl").

db_log(Module, Line, Level, FormatFun) ->
    mysql:log(Module, Line, Level, FormatFun).
db_dummy_log(_Mod, _Line, _Level, _FormatFun) ->
    empty.

%% @doc call back function when yaws start an app  
%% @see man yaws.conf
%%      start_mod = Module
%%          Defines  a  user  provided  callback  module.  At startup of the
%%          server, Module:start/1 will  be  called.   The  #sconf{}  record
%%          (defined  in  yaws.hrl) will be used as the input argument. This
%%          makes it possible for  a  user  application  to  syncronize  the
%%          startup  with  the  yaws  server as well as getting hold of user
%%          specific  configuration  data,  see  the  explanation  for   the
%%          

To build it,

> erlc -I /opt/local/lib/yaws/include erlyweb_app.erl -o ebin

The erlyweb_app.erl is almost escript ready, but I use it as module functions currently. It's pre-compiled and erlyweb_app.beam is placed under script/ebin

So, I start myapp by steps:

cd \myproject
yaws -sname myapp -i --conf config/yaws.conf --erlang "-smp auto"
1> erlyweb_app:build(myapp).

The erlyweb_app.erl is almost escript ready, but I use it as module functions currently. It's pre-compiled and erlyweb_app.beam is placed under script/ebin

After I made changes to myapp, I run above erlyweb_app:build(myapp). again, then everything is up to date.

And if you'd like to build it from another erl shell, try this:

erl -sname erlybird
(erlybird@myhost)1> rpc:call(myapp@myhost, erlyweb_app, build, [myapp])

Yes, next version of ErlyBird will support building erlyweb apps remotely in ErlyBird's Erlang shell.

Phusion Passenger是一种基于Nginx或Apache的应用服务器,它可以帮助我们快速、稳定地部署Rails应用。下面以Redmine为例,介绍在CentOS下如何使用Phusion Passenger方式部署Rails应用。 1. 安装必要的软件 首先需要安装Ruby、Rails、Nginx、Passenger等软件,可以使用以下命令: ``` sudo yum install epel-release sudo yum install ruby ruby-devel rubygems gcc gcc-c++ make sqlite-devel sudo gem install rails sudo gem install passenger ``` 2. 安装Nginx和Passenger 使用以下命令安装Nginx和Passenger: ``` sudo yum install nginx sudo passenger-install-nginx-module ``` 在安装过程中,需要选择“1. Yes: download, compile and install Nginx for me”选项,让Passenger自动编译和安装Nginx。 3. 配置Nginx和Passenger 默认情况下,Passenger会自动添加Nginx的配置文件并启动Nginx服务,但是我们需要手动修改Nginx配置文件。 找到Nginx的配置文件,一般是/etc/nginx/nginx.conf,添加以下内容: ``` http { # ... server { listen 80; server_name yourserver.com; root /path/to/your/redmine/public; passenger_enabled on; passenger_ruby /usr/bin/ruby; passenger_app_env production; } } ``` 其中,yourserver.com是你的服务器域名或IP地址,/path/to/your/redmine是你的Redmine应用所在路径。 4. 启动Nginx服务 启动Nginx服务: ``` sudo systemctl start nginx ``` 5. 配置数据库 修改/config/database.yml文件,配置Redmine连接数据库的相关信息。 6. 初始化Redmine 在Redmine目录下运行以下命令初始化数据库: ``` RAILS_ENV=production bundle exec rake db:create RAILS_ENV=production bundle exec rake db:migrate RAILS_ENV=production bundle exec rake redmine:load_default_data ``` 7. 启动Redmine 在Redmine目录下运行以下命令启动Redmine: ``` RAILS_ENV=production bundle exec rails server -e production ``` 现在你就可以在浏览器中输入yourserver.com访问Redmine了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值