Error in Apache start with XData Server

I'm not able to start the apache server when I use a connection to the MySQL database. 
The code is the same as the example of the link: https://www.youtube.com/watch?v=09Wa5wX9NtU

Up to Phase 4, with the Sparkle server, everything works normally. But when the XData server is inserted into the adapter, using a FireDac connection to MySql, the error occurs when apache is started.

Error in Apache start (libmod_ts.so is a module):

Dec 04 00:30:37 ubuntu kernel: apache2[2670]: segfault at c0 ip 00007f18a3338bde sp 00007fff49045110 error 4 in libmod_ts.so[7f18a2730000+f31000]
Dec 04 00:30:37 ubuntu kernel: Code: 55 f8 48 8b 7d f0 e8 21 1d a3 ff 90 55 48 89 e5 53 48 83 ec 18 48 89 fb 48 89 75 f0 48 89 5d e8 48 8d 05 35 28 7d 00 48 8b 00 <48>
Dec 04 00:30:37 ubuntu apachectl[2657]: Segmentation fault (core dumped)
Dec 04 00:30:37 ubuntu apachectl[2657]: Action 'start' failed.
Dec 04 00:30:37 ubuntu apachectl[2657]: The Apache error log may have more information.
Dec 04 00:30:37 ubuntu systemd[1]: apache2.service: Control process exited, code=exited status=139
Dec 04 00:30:37 ubuntu systemd[1]: apache2.service: Failed with result 'exit-code'.
Dec 04 00:30:37 ubuntu systemd[1]: Failed to start The Apache HTTP Server.

Code in WebModuleUnit1:


initialization

  Server := TWebBrokerServer.create;
  
  // A conexão funciona normalmente em design time.

  TDatabaseManager.Update(TFireDacMySqlConnection.CreateConnection);
  Server.Dispatcher.AddModule(TXDataServerModule.Create('http://ubuntu/tron',
          TFireDacMySqlConnection.CreateConnection));


  { Até essa etapa abaixo , funciona normalmente, ou seja, o uso do Sparke Module funciona.

  Server.Dispatcher.AddModule(TAnonymousServerModule.Create(
    'http://ubuntu/tron',
    procedure(const C: THttpServerContext)
    begin
      C.Response.StatusCode := 200;
      C.Response.ContentType := 'text/plain';
      C.Response.Close(TEncoding.UTF8.GetBytes('Hello Word'));
    end
    ));
   }

finalization
  Server.Free;


The MySql user is already configured for remote access (% in the host field of the user table). 
I have already tried to access another PC with another MySql server. 
I have already done several connection tests with the two MySql servers and everything works normally. 
I have already used connection to localhost, 127.0.0.1 and the IP of the PC with the other database.
By the way, the MySql user has all access rights and uses the mysql_native_password validation engine

mysql> grant all privileges on *.* to user@'%';


mysql> USE mysql;

mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='user';

mysql> FLUSH PRIVILEGES;

Hi Antonio,

The error mentions that Apache log might have more information. Did you check it?

Your code mentions that "until this point, it works normally", but before that point you have a TDatabaseManager.Update call. Does that work or not? Because that requires the MySQL connection.

Have you tried to create a simple console Linux application that just connects to the database with FireDac without involving Aurelius/XData? Does it work?

Have you tried to create the Apache module without involving Aurelius/XData and see if it can connect to the MySQL database? I suspect that's a connection issue (not XData/Aurelius related) and this would confirm that.

The program below works perfectly. The connection to the database occurs normally:


program acessobanco;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  FireDAC.Comp.Client,
  FireDAC.Stan.Def,
  FireDAC.Phys.MySQL,
  FireDAC.Phys.MySQLDef;

var
   conexao : TFDConnection;

begin
  try

    conexao := TFDConnection.Create(Nil);
    conexao.drivername:= 'MySQL';
    conexao.loginPrompt:= false;
    conexao.params.DriverID:='MySql';
    conexao.Params.UserName:='antonio';
    conexao.Params.Database:='teste';
    conexao.Params.Password:='12345';
    Conexao.Params.Add('Server=ubuntu');
    conexao.Connected := True;
    writeln('Teste Simples - Conexão Efetuada');
    conexao.Connected := false;
    conexao.Free;

  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.


In a WebModule, when a code is the code below, its work ok:

initialization

  
  Server := TWebBrokerServer.create;
  Server.Dispatcher.AddModule(TAnonymousServerModule.Create(
    procedure(const C: THttpServerContext)
    begin
      C.Response.StatusCode := 200;
      C.Response.ContentType := 'text/plain';
      C.Response.Close(TEncoding.UTF8.GetBytes('Tron Lives'));
    end
    ));
   

finalization
  Server.Free;


But, with XDataServer and FiredacMySql connection , the apache error occurs.

initialization

  Server := TWebBrokerServer.create;
  Server.Dispatcher.AddModule(TXDataServerModule.Create('http://ubuntu/tron',
          TFireDacMySqlConnection.CreateConnection));

finalization
  Server.Free;

When I use de command "sudo invoke-rc.d apache2 start" :

Dec 05 04:43:58 ubuntu systemd[1]: Starting The Apache HTTP Server...
-- Subject: Unit apache2.service has begun start-up
-- Defined-By: systemd
--
-- Unit apache2.service has begun starting up.
Dec 05 04:43:58 ubuntu kernel: apache2[9534]: segfault at c0 ip 00007f2481a20bde sp 00007ffda6b00040 error 4 in libmod_ts.so[7f2480e18000+f31000]
Dec 05 04:43:58 ubuntu kernel: Code: 55 f8 48 8b 7d f0 e8 21 1d a3 ff 90 55 48 89 e5 53 48 83 ec 18 48 89 fb 48 89 75 f0 48 89 5d e8 48 8d 05 35 28 7d 00 48 8b 00 <48> 8b b0 c0 00 0
Dec 05 04:43:58 ubuntu apachectl[9521]: Segmentation fault (core dumped)
Dec 05 04:43:58 ubuntu apachectl[9521]: Action 'start' failed.
Dec 05 04:43:58 ubuntu apachectl[9521]: The Apache error log may have more information.
Dec 05 04:43:58 ubuntu systemd[1]: apache2.service: Control process exited, code=exited status=139
Dec 05 04:43:58 ubuntu systemd[1]: apache2.service: Failed with result 'exit-code'.
Dec 05 04:43:58 ubuntu systemd[1]: Failed to start The Apache HTTP Server.



What happen when you try to use the MySQL connection in an Apache Module, but without XData? Maybe it's something related to Apache module permissions and MySQL connection?

What does the Apache error log say?

"What happen when you try to use the MySQL connection in an Apache Module, but without XData? "


Don't exist error in this situation. See the code below:

implementation

uses
  Sparkle.WebBroker.Server,
  Sparkle.WebBroker.Adapter;

{%CLASSGROUP 'System.Classes.TPersistent'}

{$R *.dfm}

var
  Server : TWebBrokerServer;

procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  Adapter : TWebBrokerAdapter;
begin

  Adapter := TWebBrokerAdapter.Create(Request, Response);
  Server.DispatchRequest(Adapter);

end;

initialization

  Server := TWebBrokerServer.create;
  Server.Dispatcher.AddModule(TAnonymousServerModule.Create(
    procedure(const C: THttpServerContext)
    var
      lResposta : String;
    begin
      C.Response.StatusCode := 200;
      C.Response.ContentType := 'text/plain';
      lResposta := 'Sparkle Server Active!';
      C.Response.Close(TEncoding.UTF8.GetBytes(' '));
      try
        lResposta:=lResposta +'  Starting MySql Connection.... ';

        conexao := TFDConnection.Create(Nil);
        conexao.drivername:= 'MySQL';
        conexao.loginPrompt:= false;
        conexao.params.DriverID:='MySql';
        conexao.Params.UserName:='antonio';
        conexao.Params.Database:='teste';
        conexao.Params.Password:='12345';
        Conexao.Params.Add('Server=ubuntu');
        conexao.Connected := True;
        lResposta:=lResposta +'  Connected to MySql (OK) ';
        conexao.Connected := false;
        conexao.Free;
      except
        on E: Exception do
          lResposta:=lResposta + (E.ClassName +  ': ' + E.Message);
      end;
      C.Response.Close(TEncoding.UTF8.GetBytes(lResposta));
    end
    ));

finalization
  Server.Free;

end.

The resposta in the browser is:

Hm, you are not using the exactly same connection component in the "pure-Apache" tests. I believe in XData tests you are using the connection in a data module? 

Probably your data module is not created yet. Try using this at the beginning of your application:

Application.CreateForm(TFireDacMySQLConnection, FireDacMySqlConnection);
This code already exists in the application. I will copy below all the units of the module that is working (OK). 

Please try to show me other possibilities of code changes, configurations, sample codes etc. I'm almost giving up using XData in linux ..

mod_ts.dpr


library mod_ts;

uses
  {$IFDEF MSWINDOWS}
  Winapi.ActiveX,
  {$ENDIF }
  Web.WebBroker,
  Web.ApacheApp,
  Web.HTTPD24Impl,
  WebModuleUnit1 in 'WebModuleUnit1.pas' {e: TWebModule},
  ConnectionModule in 'ConnectionModule.pas' {SQLiteConnection: TDataModule};

{$R *.res}

var
  GModuleData: TApacheModuleData;
exports
  GModuleData name 'ts_module';

begin
{$IFDEF MSWINDOWS}
  CoInitFlags := COINIT_MULTITHREADED;
{$ENDIF}
  Web.ApacheApp.InitApplication(@GModuleData);
  Application.Initialize;
  Application.WebModuleClass := WebModuleClass;
  Application.CreateForm(TSQLiteConnection, SQLiteConnection);
  Application.Run;
end.

ConnectionModulo.pas

unit ConnectionModule;

interface

uses
  Aurelius.Drivers.Interfaces,
  Aurelius.Drivers.SQLite, 
  System.SysUtils, System.Classes, Aurelius.Comp.Connection;

type
  TSQLiteConnection = class(TDataModule)
    AureliusConnection1: TAureliusConnection;
  private
  public
    class function CreateConnection: IDBConnection;
    class function CreateFactory: IDBConnectionFactory;
    
  end;

var
  SQLiteConnection: TSQLiteConnection;

implementation

{%CLASSGROUP 'System.Classes.TPersistent'}

uses 
  Aurelius.Drivers.Base;

{$R *.dfm}

{ TMyConnectionModule }

class function TSQLiteConnection.CreateConnection: IDBConnection;
begin 
  Result := SQLiteConnection.AureliusConnection1.CreateConnection; 
end;

class function TSQLiteConnection.CreateFactory: IDBConnectionFactory;
begin
  Result := TDBConnectionFactory.Create(
    function: IDBConnection
    begin
      Result := CreateConnection;
    end
  );
end;

end.


WebModuleUnit.pas


unit WebModuleUnit1;

interface

uses System.SysUtils, System.Classes, Web.HTTPApp, Sparkle.Comp.Server,
  Sparkle.Comp.StaticServer, Sparkle.httpserver.module, Sparkle.httpserver.context,
  XData.Server.Module, ConnectionModule, Aurelius.Engine.DatabaseManager,
  FireDAC.Comp.Client,
  FireDAC.Stan.Def,
  FireDAC.Phys.MySQL,
  FireDAC.Phys.MySQLDef;

type
  TWebModule1 = class(TWebModule)
    procedure WebModule1DefaultHandlerAction(Sender: TObject;
      Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  WebModuleClass: TComponentClass = TWebModule1;
  conexao : TFDConnection;

implementation

uses
  Sparkle.WebBroker.Server,
  Sparkle.WebBroker.Adapter;

{%CLASSGROUP 'System.Classes.TPersistent'}

{$R *.dfm}

var
  Server : TWebBrokerServer;

procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  Adapter : TWebBrokerAdapter;
begin

  Adapter := TWebBrokerAdapter.Create(Request, Response);
  Server.DispatchRequest(Adapter);

end;

initialization
  {
  Server := TWebBrokerServer.create;
  TDataBaseManager.Update(TSQLiteConnection.CreateConnection);
  Server.Dispatcher.AddModule(TXDataServerModule.Create('http://ubuntu/tron',
          TSQLiteConnection.CreateConnection));
  }


  Server := TWebBrokerServer.create;
  Server.Dispatcher.AddModule(TAnonymousServerModule.Create(
    'http://ubuntu/tron',
    procedure(const C: THttpServerContext)
    var
      lResposta : String;
    begin
      C.Response.StatusCode := 200;
      C.Response.ContentType := 'text/plain';
      lResposta := 'Sparkle Server Active!';
      C.Response.Close(TEncoding.UTF8.GetBytes(' '));
      try
        lResposta:=lResposta +'  Starting MySql Connection.... ';

        conexao := TFDConnection.Create(Nil);
        conexao.drivername:= 'MySQL';
        conexao.loginPrompt:= false;
        conexao.params.DriverID:='MySql';
        conexao.Params.UserName:='antonio';
        conexao.Params.Database:='tronsolution';
        conexao.Params.Password:='biagui04';
        Conexao.Params.Add('Server=ubuntu');
        conexao.Connected := True;
        lResposta:=lResposta +'  Connected to MySql (OK) ';
        conexao.Connected := false;
        conexao.Free;
      except
        on E: Exception do
          lResposta:=lResposta + (E.ClassName +  ': ' + E.Message);
      end;
      C.Response.Close(TEncoding.UTF8.GetBytes(lResposta));
    end
    ));


finalization
  Server.Free;

end.




Below the code that doesn't work:

library mod_ts;

uses
  {$IFDEF MSWINDOWS}
  Winapi.ActiveX,
  {$ENDIF }
  Web.WebBroker,
  Web.ApacheApp,
  Web.HTTPD24Impl,
  WebModuleUnit1 in 'WebModuleUnit1.pas' {e: TWebModule},
  ConnectionModule in 'ConnectionModule.pas' {FireDacMySqlConnection: TDataModule};

{$R *.res}

var
  GModuleData: TApacheModuleData;
exports
  GModuleData name 'ts_module';

begin
{$IFDEF MSWINDOWS}
  CoInitFlags := COINIT_MULTITHREADED;
{$ENDIF}
  Web.ApacheApp.InitApplication(@GModuleData);
  Application.Initialize;
  Application.WebModuleClass := WebModuleClass;
  Application.CreateForm(TFireDacMySqlConnection, FireDacMySqlConnection);
  Application.Run;
end


unit ConnectionModule;

interface

uses
  Aurelius.Drivers.Interfaces,
  Aurelius.Drivers.FireDac,  
  FireDAC.Dapt,
  System.SysUtils, System.Classes, FireDAC.Stan.Intf, FireDAC.Stan.Option,
  FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
  FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.ConsoleUI.Wait,
  Aurelius.Sql.MySQL, Aurelius.Schema.MySQL, Aurelius.Comp.Connection, Data.DB,
  FireDAC.Comp.Client, FireDAC.Phys.MySQL, FireDAC.Phys.MySQLDef;

type
  TFireDacMySqlConnection = class(TDataModule)
    Connection: TFDConnection;
    AureliusConnection1: TAureliusConnection;
  private
  public
    class function CreateConnection: IDBConnection;
    class function CreateFactory: IDBConnectionFactory;
    
  end;

var
  FireDacMySqlConnection: TFireDacMySqlConnection;

implementation

{%CLASSGROUP 'System.Classes.TPersistent'}

uses 
  Aurelius.Drivers.Base;

{$R *.dfm}

{ TMyConnectionModule }

class function TFireDacMySqlConnection.CreateConnection: IDBConnection;
begin 
  Result := FireDacMySqlConnection.AureliusConnection1.CreateConnection; 
end;

class function TFireDacMySqlConnection.CreateFactory: IDBConnectionFactory;
begin
  Result := TDBConnectionFactory.Create(
    function: IDBConnection
    begin
      Result := CreateConnection;
    end
  );
end;


unit WebModuleUnit1;

interface

uses System.SysUtils, System.Classes, Web.HTTPApp, Sparkle.Comp.Server,
  Sparkle.Comp.StaticServer, Sparkle.httpserver.module, Sparkle.httpserver.context,
  XData.Server.Module, ConnectionModule, Aurelius.Engine.DatabaseManager;

type
  TWebModule1 = class(TWebModule)
    procedure WebModule1DefaultHandlerAction(Sender: TObject;
      Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  WebModuleClass: TComponentClass = TWebModule1;

implementation

uses
  Sparkle.WebBroker.Server,
  Sparkle.WebBroker.Adapter;

{%CLASSGROUP 'System.Classes.TPersistent'}

{$R *.dfm}

var
  Server : TWebBrokerServer;

procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  Adapter : TWebBrokerAdapter;
begin

  Adapter := TWebBrokerAdapter.Create(Request, Response);
  Server.DispatchRequest(Adapter);

end;

initialization

  Server := TWebBrokerServer.create;
    Server.Dispatcher.AddModule(TXDataServerModule.Create('http://ubuntu/tron',
          TFireDacMySqlConnection.CreateConnection));



finalization
  Server.Free;

end.

Well, your dpr code is creating FireDacMySQLConnection module, but that is being called after you use it. The initialization code in WebModuleUnit1 is being called before the module is created.

Either move the code that creates FireDacMySQLConnection to initialization part of WebModuleUnit1, before it's being used in TXDataServerModule create, or move the code in WebModuleUnit1 from initialization to a procedure and call that procedure from dpr after the module is initialized, or even move it to the data module initialization.

Option1:


initialization
  // Add here
  Application.CreateForm(TFireDacMySqlConnection, FireDacMySqlConnection);


  Server := TWebBrokerServer.create;
    Server.Dispatcher.AddModule(TXDataServerModule.Create('http://ubuntu/tron',
          TFireDacMySqlConnection.CreateConnection));


Option2:
In WebModule1:

procedure InitXData;
begin


  Server := TWebBrokerServer.create;
    Server.Dispatcher.AddModule(TXDataServerModule.Create('http://ubuntu/tron',
          TFireDacMySqlConnection.CreateConnection));
end;

In dpr:

  Application.CreateForm(TFireDacMySqlConnection, FireDacMySqlConnection);
  InitXData;
  Application.Run;
end.
 

Option3:
In ConnectionModule:

initialization

  Application.CreateForm(TFireDacMySqlConnection, FireDacMySqlConnection);
end.

Unfortunately none of the options worked, and the error is the same. 


Please, send me a complete example with Apache, Linux and MySql that works in your computing environment.

I didn't find examples with apache / linux / mysql on the TMS website.

Can you please send us your full project through e-mail, with any of the applied suggestions?

There is no different between using MySQL or any other database with XData/Aurelius, since the connection is performed by FireDac. If FireDac can connect, XData should work. 

Hi Wagner, I sent the email with the source code attached.

Thank you Antonio. We have answered you with the fixed project. Basically instead of using


Application.CreateForm(TFireDacMySqlConnection, FireDacMySqlConnection);

you should use

FireDacMySqlConnection := TFireDacMySqlConnection.Create(nil);

Hi Wagner.
Was this error fixed with in this last post?
I'm having the same error and haven't fixed it yet.
Tks

Hi Marcos, it might not be the same issue, I suggest you create a new topic with details about your error.

But often the problem the problem with Apache is that you need to create the data module with the connection before using it. So if you have code in initialization section that tries to get a connection or a connection pool, make sure the data module is already created at that point. Better to create it explicitly from code.

You can take a look here as well: Apache won't start with XData server application

296/5000

Wagner, the error was in a test module that was in error.

I created XData/Aurelius backend in the cloud using Amazon AWS. The application uses an EC2 server as an application server accessing the MySQL database in RDS and now I will create the front end using the TMS Web Core.
Thank you for your help.

1 Like

Hi Marcos, I understand the problem is solved now?

Hi Wagner.
Yes, it was resolved.
Thank you

1 Like