2

I have a web application that interacts with API. In html file is an element with onclick property for this function:

function executePHP() {
   $.ajax({
       type: "POST",
       url: 'index2.php',
   });

}

Then index2.php has written only this code:

<?php exec("/usr/local/bin/node script.js"); ?>

The script.js is just a file which is executed. Everything works fine on my maschine when using MAMP. However when I upload it on real server and open the website the script.js isn't executed. And I didn't forget to change the url for the real one once I was testing it. Any clues where the problem is?

Basically, I just need to execute node script.js when the button is clicked

Baljeet
  • 66
  • 1
  • 9
  • 2
    Your code is ok, however basic shared servers don't allow this. You need a real real server vps. – NVRM Nov 21 '20 at 19:36
  • 1
    Oh I see, unfortunately these services are quite expensive – Baljeet Nov 21 '20 at 20:26
  • Cheapest starts at $5 the month, for scripting and small/medium load it's nice. – NVRM Nov 21 '20 at 20:30
  • 1
    Yeah, I think this will be good invention. Once I have it, can I use my old code and it will work? – Baljeet Nov 21 '20 at 20:35
  • Yep, just as your machine. For example if you use Ubuntu on your pc, and the server is under Debian, this will works straight away (same Debian family, same packages etc). – NVRM Nov 21 '20 at 20:40
  • 1
    I am quite newbie in this type of backend and executing command, so I am sorry for asking so much. The fact is, I am using Webstorm as my IDE and when I open it from there it doesn't work, but I have also Mamp on my computer and with that everything works. I was just checking the servers now and there is huge variety of them. Which one would you advise for a beginner, who was used to only to Mamp? – Baljeet Nov 21 '20 at 20:50
  • You mean editing remote files? Yes it's different, don't use your editor to connect it might crash if the internet disconnect. Instead use SSH, you need your ssh login and password. Then open the file explorer of your computer, click `File`,`Connect to server`, choose `SSH`, logins. Thefirst connection will create the SSH keys automaticly. From there you will be able to browse and edit your files like as if they were on your pc. Of course, this is also doable from the command line. You will be abble to use any editor as notmal. I personally use Kdevellop, but it does not matter really. – NVRM Nov 21 '20 at 20:50
  • This sounds really difficult, but thank you for explaining it to me. I will need to study more these VPS. Now I am considering buying Ubuntu server and hope it will work. – Baljeet Nov 21 '20 at 20:59
  • You won't regret it, be patient, good luck^ – NVRM Nov 21 '20 at 21:04
  • 1
    Hey, you there? – Baljeet Nov 29 '20 at 16:56
  • Yes. Hi there!!!^ – NVRM Nov 29 '20 at 17:06
  • 1
    Hi, I finally managed to start my vps server and my website is successfully running there, but I have an issue with executing node script.js. This is my script `` I know I need new path for the node, because it is no more in /usr/local... Can you help me please where to find new path for node? Or should I rather open a new question for this? – Baljeet Nov 29 '20 at 17:12
  • This should work straight away using `system("node script.js > output.txt");` , to retrieve the output from php, simply `file_get_contents("output.txt");` Of course use json instead of text, to pass objects between the 2 langs. Alternatively, if you don't need the output use backticks from php: `node script.js` should simply run the script silently. If you don't need to wait, use `passthru()` Also you can set bash environment variables directly from php. Yes further details need a question. Good luck my friend. – NVRM Nov 29 '20 at 17:34
  • See https://stackoverflow.com/questions/732832/php-exec-vs-system-vs-passthru there is fine differences, both of those commands are shell call functions. If node is in usr/bin, this should work directly, because php is calling the shell of the machine, not his own. – NVRM Nov 29 '20 at 17:46
  • So I am back after a longer time and many tryings. Even though I used `system("node script.js > output.txt");` my code isn't executed. The website works perfectly, till I want it to run node script.js. Do you know where the problem could be? – Baljeet Dec 03 '20 at 17:42
  • When you open a ssh session, if you type `node script.js` does it works?. Type in `whoami` are you root? See ownerships , from what user is running the server. You might have to give permissions. Depends. – NVRM Dec 03 '20 at 17:47
  • Yeah, I am in root. I tried to go to var/www to my website but can't – Baljeet Dec 03 '20 at 19:18
  • When you open a ssh session, if you type node script.js does it works? – NVRM Dec 03 '20 at 19:33
  • It throws an error. Can't find the module, also when I try `node ../var/html/test/script.js` But I know, that node is there, because I can type `node --version` . My website is located outside of root directory in var, but node is installed globally – Baljeet Dec 03 '20 at 19:54
  • This is about linux, you have to digg around how it works. Users have rights. You have to set rights for the server owner. Apache by default has an user www something. Read about around linux. No worries it takes time, this is admin tasks – NVRM Dec 03 '20 at 20:08
  • 1
    Okay, I will examine the code and try to find solution #iwontgiveup – Baljeet Dec 03 '20 at 20:11
  • 1
    Hurray, it works. I was able to get to my website directory, tried `node script.js` and found out that there was one module missing, so I just installed it locally as well and it solved the problem. You were right, with node was everything fine. My website works now perfectly. Thank you for all your help #wedidit – Baljeet Dec 03 '20 at 20:46
  • Sounds good man – NVRM Dec 03 '20 at 20:53

1 Answers1

2

After the discussion with an expert in this field I can answer my question. This code can't be executed on basic shared server, therefore VPS (virtual private server) is needed.

Baljeet
  • 66
  • 1
  • 9