Welcome! Log In Create A New Profile

Advanced

How to setup Nginx as a Service?

Posted by knightpraetor 
How to setup Nginx as a Service?
April 03, 2015 10:37PM
I was trying to setup Nginx, so I wanted to make a small service so that I could run it without logging on. I have a bat file that calls both nginx and a php server, shown below


start C:\wiki\nginx-1.7.11\nginx.exe
IF errorlevel 1 goto EXIT_FAILURE


start c:\wiki\php-5.4.39-Win32-VC9-x86\php-cgi.exe -b 127.0.0.1:9000 -c c:\wiki\php-5.6.7-Win32-VC11-x86\php.ini
IF errorlevel 1 goto EXIT_FAILURE
REM exit command can take /B to only exit script instead of command, and then a number specifying the error level
EXIT

:EXIT_FAILURE
EXIT 1


this batch file works when I just double click on it, but when I attempt to run it from within visual studio, Nginx doesn't start.


I run it in visual studio with the command below, where CMD is the string "/c pathtothebatfile". It doesn't seem to work regardless of whether I am Administrator or a local account.
if (!CreateProcess(
//userToken,
"C:\\Windows\\System32\\cmd.exe", // No module name (use command line)
CMD, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure
))
{
printf("CreateProcess failed (%d).\n", GetLastError());
}

I have also tried registering the exe as a service. From within the service I can set the property LOGON32_LOGON_SERVICE, and try running it as the user LocalService as well. However I can run a batch file like this, but neither the nginx server, nor the phpserver show up in Windows Task Manager's System Processes, so I assume they cannot start.

My guess is that I can't run nginx or the phpserver without certain privileges or using a certain user account.

I am sure I can either use someone else's service and get nginx to start up without requiring me to log into windows.

However, I would really appreciate it if someone could explain to me what i was doing wrong for my own development as a coder.
Re: How to setup Nginx as a Service?
April 04, 2015 07:38AM
Go to http://nginx-win.ecsds.eu/
and download Install_nginx_php_services.zip, read the scripts how this works.

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: How to setup Nginx as a Service?
April 06, 2015 03:14PM
works. Though i'm still really confused as to why I couldn't host the bat file like svrany. I guess what i really want is to see the source from svrany.

I tried adding a line to the bat file to just write a line of text to a file, and I then ran that bat file from the service wrapper I created. This resulted in the new text file being made successfully. So i know the bat file was run. if that's the case, then why can't nginx and the php server start from the bat file, even though they start successfully if I just double click the batch file in windows.
Re: How to setup Nginx as a Service?
April 06, 2015 03:27PM
A bat file is not a service, srvany does this all including the communication Windows wants.

https://www.google.nl/#q=srvany+source+code

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: How to setup Nginx as a Service?
April 06, 2015 07:22PM
I guess my original post didn't make it clear, but I was coding a C++ service wrapper using the example give by msdn. Anyway I got it working. however, I assume that just using that srvany is probably coded better than mine, so I just used that instead.

Thanks for the help.

On a side note, the reason that I couldn't get my service to work originally was because my bat file would call nginx.exe without moving to the folder where the nginx.exe was stored. Then nginx would use the calling folder rather than the folder with the exe to determine the path to files nginx needed.

For example,

when my bat file contained "c:\nginx\nginx.exe" and I ran the bat file from within the folder where nginx was stored, it ran fine.

However, if I were for example in c:\system32 and called c:\nginx\nginx.exe it would fail because nginx would attempt to find files inside c:\system32\logs when it really wanted to be pointing to c:\nginx\logs.

Basically in my C++ code when i made a new process that called "cmd /c BatchFile.bat," the new cmd window would start in the folder system32. So when i changed my bat file so that I moved to the nginx folder before calling nginx, it ran fine.

I still think it's a little strange that nginx used the calling folder to determine its path though. Was that intended?
Re: How to setup Nginx as a Service?
April 07, 2015 04:14AM
knightpraetor Wrote:
-------------------------------------------------------
> I still think it's a little strange that nginx used the calling folder
> to determine its path though. Was that intended?

Yes because it should not be hardcoded for any path, well the nginx one is sort of hardcoded, mine isn't.

---
nginx for Windows http://nginx-win.ecsds.eu/
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 342
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready