hi i got a problem running nodejs in my php project, im planning run my apps in nginx, nodejs, php-fpm, and pm2
but i tryna access the test file its doesn't work and the nginx error log is like this
2022/03/10 21:04:04 [error] 14875#14875: *12 connect() failed (111: Connection refused) while connecting to upstream, client: ips, server: domainname, request: "GET /test.js HTTP/1.1", upstream: "http://[::1]:7000/test.js", host: "domainname
and this is my nginx configuration file
#Names a server and declares the listening port
server {
listen 80;
server_name domainname;
#Configures the publicly served root directory
#Configures the index file to be served
root /var/www/domainname;
index index.html index.htm;
#These lines create a bypass for certain pathnames
#www.example.com/test.js is now routed to port 3000
#instead of port 80
location ~* \.(js)$ {
proxy_pass http://localhost:7000;
proxy_set_header Host $host;
}
}
i already install nodejs but the .js file is not work, after the js work ill try to add some conf for nginx php
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
but the problem right now is the .js file is not work how to solve that problem ? i already allow firewall at 7000/tcp but its not working
Related
I was running Ubuntu server 20.04 quite successfully with Ired mail and 2 websites, one of them with WordPress.
I wanted to install Nextcloud, to do that I had to reinstall php-fpm to generate php7.4-fpm.sock. After this Nextcloud worked, but my other websites stopped working with error '502 Bad Gateway'.
So to say the least, I'm very confused!
I followed this article to install Nextcloud and set up the sites-enabled .conf file as per instructions: https://www.linuxbabe.com/ubuntu/install-nextcloud-ubuntu-20-04-nginx-lemp-stack/amp
I think I understand that the .conf file used to listen on 127.0.0.1:XXXX and now listens on php7.4-fpm.sock?
Here is the .conf file that I have put together for my website after re-installing php-fpm:
#
# Note: This file must be loaded before other virtual host config files,
#
# HTTPS
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name SOMEWEBSITE www.SOMEWEBSITE;
error_log /var/log/nginx/localhost.error_log info;
root /var/www/SOMEWEBSITE/html;
index index.php index.html;
include /etc/nginx/templates/misc.tmpl;
include /etc/nginx/templates/ssl.tmpl;
include /etc/nginx/templates/iredadmin.tmpl;
include /etc/nginx/templates/roundcube.tmpl;
include /etc/nginx/templates/sogo.tmpl;
include /etc/nginx/templates/netdata.tmpl;
include /etc/nginx/templates/php-catchall.tmpl;
include /etc/nginx/templates/stub_status.tmpl;
location / {
try_files $uri $uri/ /index.php?q=$uri$args;
}
# PHP handling
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
ssl_certificate /etc/letsencrypt/live/SOMEWEBSITE/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/SOMEWEBSITE/privkey.pem; # managed by Certbot
}
# Redirect http to https
server {
listen 80;
listen [::]:80;
server_name SOMEWEBSITE www.SOMEWEBSITE;
return 301 https://$host$request_uri;
}
I have checked the file permissions for php7.4-fpm.sock
ll /var/run/php/ | grep php
-rw-r--r-- 1 root root 3 May 22 21:13 php7.4-fpm.pid
srw-rw---- 1 www-data www-data 0 May 22 21:13 php7.4-fpm.sock=
lrwxrwxrwx 1 root root 30 May 22 21:13 php-fpm.sock -> /etc/alternatives/php-fpm.sock=
and I think it looks ok.
here is the log file:
2021/05/23 20:32:52 [error] 43596#43596: *305 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xx.xxx.xxx, server: SOMEWEBSITE, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9999", host: "SOMEWEBSITE"
2021/05/23 20:32:53 [info] 43596#43596: *305 client xx.xx.xxx.xxx closed keepalive connection
Any Ideas? Need any more information? Thank you in advance for looking.
PHP-FPM can listen using two method for accepting fastcgi request. using TCP Socket or with Unix Socket.
You can sepecify it in php-fpm configuration, In Ubuntu the configuration is in /etc/php/7.4/fpm/pool.d/www.conf and check listen configuration.
If you want to use unix socket, use configuration below.
listen = /run/php/php7.4-fpm.sock
For TCP Socket.
listen = 127.0.0.1:9000
Next in nginx you can specify fastcgi_pass based on fpm configuration. If you using Unix socket, all your website, include Nextcloud must be using Unix Socket.
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
If you using TCP Socket, you must change the nginx configuration for Nextcloud to pass from TCP Socket.
fastcgi_pass 127.0.0.1:9000;
I have faced this issue as well. the problem is php-fpm didnt listen port 9999 (In my case i use port 9999). To make /mail/ work. u need to change below config file. change ip:port change to socket
/etc/php/fpm/pool.d/www.conf
listen = /var/run/php/php7.2-fpm.sock
/etc/nginx/templates/fastcgi_php.tmpl
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
So I made an alternative domain to a site of mine, using nginx but that error keeps coming up.
The original site works just fine, but the alternate domain shows this error.
2021/01/04 15:14:27 [error] 2007#2007: *269 connect() failed (111:
Connection refused) while connecting to upstream, client:
35.209.169.6, server: cocknutsandball.com, request: "GET /sh4 HTTP/1.1", upstream: "http://127.0.0.1:9000/sh4", host:
"104.244.79.32"
Here is what I tried to do:
reload nginx and php8.0-fpm
disable any firewall
for the alternate domain
'''
server {
listen 80;
server_name cocknutsandball.com www.cocknutsandball.com;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:9000;
}
}
'''
For the main domain that works
'''
server {
listen 127.0.0.1:80 default_server;
root /var/www/;
index chat.php lang_en.php emoji.css;
server_name ylwebs62zfllr4hhailwwysq2erzoxqcwqhfokkzimi73c2n4fp3mspad.o$
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
include fastcgi.conf;
}
error_log
/root/error_log;
access_log
/root/access_log;
}
'''
OK quick update: I changed the config to be completely identical to the one for the working domain but change the domain so now it is:
server {
listen 127.0.0.1:80;
root /var/www/;
index chat.php lang_en.php emoji.css;
server_name cocknutsandball.com;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
include fastcgi.conf;
}
error_log
/root/error_log;
access_log
/root/access_log;
}
but now im getting the same exact error
just to clarify some things:
I have restarted nginx
i have made sure that the configuration syntax is working(which it was)
i have changed the index to another file to see if thats a problem
i made sure all of the ports are being listened to
i have but the domain into the original domain config, still the same error on the alternate domain
I have referred to the manual, and a lot of other guides on the internet, i just have a really difficult time understanding it
I am setting up a development environment that uses nginx and forwards requests to a command line php server using fastcgi. When I send requests to the php server directly, it handles them correctly. The nginx server can properly handle requests for non-php files. However, when I send a php request through nginx, the php server is hit, but returns with "Invalid request (Malformed HTTP request)".
Apologies for attaching all of my configuration files, but I'm not sure where to start. All files are located in $PROJECT_PATH/dev-config/.
Here are the relevant parts of my nginx config.
http {
...
server {
root .;
listen 8123;
server_name localhost;
location / {
index index.php;
}
location ~ \.php$ {
try_files $fastcgi_script_name =404;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass 127.0.0.1:9123;
fastcgi_index index.php;
include fastcgi_params; # this file is the default
}
}
}
Here is the script that gets run to (re-)start nginx and php:
#!/bin/bash
PROJECT_PATH="$(dirname $0)/../"
nginx -s stop || true
nginx -c dev-config/nginx.conf -p $PROJECT_PATH
php -c php-dev.ini -t $PROJECT_PATH -S 127.0.0.1:9123
The file I am sending is test.php:
<? echo 'YAY!' ?>
Inside of my nginx error logs, I get messages like this:
2017/10/27 08:12:56 [error] 68934#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9123", host: "localhost:8123"
Simultaneously, php is sending messages like this:
[Fri Oct 27 08:12:56 2017] 127.0.0.1:52470 Invalid request (Malformed HTTP request)
This shows that the request is handled by nginx and sent onward to the php server. But the request is somehow incorrect.
What can I do to send properly formed requests?
Or, if that's impossible to tell right now, what can I do to get more info from php as to what request, exactly, it is getting?
To be clear, this is meant to be run in a dev environment on a laptop with no configuration changes for each user.
The problem that I was having was that the commandline php server does not have fastcgi running with it. Since the commandline server is already a server, I don't need fastcgi at all. Instead, I can proxy_pass to it.
I changed the php location block to this and it worked:
location ~ \.php$ {
proxy_pass http://127.0.0.1:9123;
}
On my vagrant box , centtos 7, i had installed php7.0.3 and nginx 1.9.12. the php config www.conf. this file in the /usr/local/php/etc/php-fpm.d/ directory, Its configuration like this
user = www
group = www
;listen = /tmp/php-fpm.sock
listen = 127.0.0.1:9000
listen.owner = www
listen.group = www
and the nginx config. the file vm.demo.com.conf in the /usr/local/nginx/conf/vhost/ directory. Its configuration like this
server {
listen 80;
server_name vm.demo.com;
index index.html index.htm index.php;
root /data/wwwroot/demo;
location / {
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
location ~ .*\.(php|php5)?$ {
#fastcgi_pass unix:/tmp/php-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
access_log logs/demo.log main;
}
in the nginx conf, if i use fastcgi_pass 127.0.0.1:9000 and the php-fpm.conf listen 127.0.0.1:9000, They are working properly
bug if i use the socket configuration:
php www.conf
user = www
group = www
listen = /tmp/php-fpm.sock
listen.owner = www
listen.group = www
nginx vm.demo.com.conf
location ~ .*\.(php|php5)?$ {
fastcgi_pass unix:/tmp/php-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
the nginx show 502 Bad gateway. in the nginx error.log show this.
2016/03/14 21:17:04 [crit] 4208#0: *5 connect() to unix:/tmp/php-fpm.sock failed (2: No such file or directory) while
connecting to upstream, client: 192.168.1.101, server: vm.demo.com, request: "GET /test.php HTTP/1.1",
upstream: "fastcgi://unix:/tmp/php-fpm.sock:", host: "vm.demo.com"
2016/03/14 21:17:04 [crit] 4208#0: *5 connect() to unix:/tmp/php-fpm.sock failed (2: No such file or directory) while
connecting to upstream, client: 192.168.1.101, server: vm.demo.com, request: "GET /test.php HTTP/1.1",
upstream: "fastcgi://unix:/tmp/php-fpm.sock:", host: "vm.demo.com"
2016/03/14 21:18:01 [crit] 4208#0: *5 connect() to unix:/tmp/php-fpm.sock failed (2: No such file or directory) while
connecting to upstream, client: 192.168.1.101, server: vm.demo.com, request: "GET /test.php HTTP/1.1",
upstream: "fastcgi://unix:/tmp/php-fpm.sock:", host: "vm.demo.com"
when i check the /tmp/php-fpm.sock file, it's existing
[root#vbox1 vhost]# ll /tmp/php-fpm.sock
srw-rw---- 1 www www 0 Mar 14 21:06 /tmp/php-fpm.sock
this are php and nginx running status:
[root#vbox1 vhost]# systemctl status php-fpm.service
● php-fpm.service - The PHP FastCGI Process Manager
Loaded: loaded (/etc/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2016-03-14 21:06:27 CST; 42min ago
Process: 4189 ExecStop=/bin/kill -SIGINT $MAINPID (code=exited, status=0/SUCCESS)
Main PID: 4198 (php-fpm)
CGroup: /system.slice/php-fpm.service
├─4198 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
├─4199 php-fpm: pool www
└─4200 php-fpm: pool www
Mar 14 21:06:27 vbox1 systemd[1]: Started The PHP FastCGI Process Manager.
Mar 14 21:06:27 vbox1 systemd[1]: Starting The PHP FastCGI Process Manager...
[root#vbox1 vhost]# systemctl status nginx.service
● nginx.service - nginx
Loaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2016-03-14 21:06:35 CST; 42min ago
Process: 4180 ExecStop=/usr/local/nginx/sbin/nginx -s quit (code=exited, status=0/SUCCESS)
Process: 4206 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 4207 (nginx)
CGroup: /system.slice/nginx.service
├─4207 nginx: master process /usr/local/nginx/sbin/nginx
├─4208 nginx: worker process
└─4209 nginx: worker process
Mar 14 21:06:35 vbox1 systemd[1]: Starting nginx...
Mar 14 21:06:35 vbox1 systemd[1]: Started nginx.
Can someone help me? If anyone encountered the same problem
I have met the same problem.
And I just change the socket path to another directory but '/tmp',
Then everything goes OK.
Basically, the problem arises with ONE process (php-fpm) setting up the sock in the /tmp path, but it would not be visible by ANOTHER process (nginx).
There is a setting in the service config, which would block files created by different process, even though using the same user.
Find your service config file (ex: /etc/systemd/system/myapp.service) and then on add this setting: PrivateTmp=No to both your services (php and nginx).
[Service]
...
PrivateTmp=No
more info about systemd units here:
https://www.freedesktop.org/software/systemd/man/systemd.unit.html
I have found a config (php5) that continues to use the unix socket and does not involve editing any other config files (except the nginx default site file).
location ~ \.php$ {
fastcgi_pass unix:/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
# \Add following to use sqlite as db.
fastcgi_param DB_CONNECTION sqlite;
}
Note:- the unix socket is not at /run/php/ (though it is for php7).
The include fastcgi_params; is required to prevent the php file downloading instead of being interpreted.
For completeness the following is my entire default file.
index index.php index.html index.htm;
server {
listen 80;
server_name sponk.co.uk;
rewrite_log on;
root /vagrant/public_html;
try_files $uri $uri/ /index.php$is_args$args;
location ~ \.php$ {
fastcgi_pass unix:/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
You need to change the listen.owner and the listen.group
listen.owner = nginx
listen.group = nginx
In my case in this file: /etc/php-fpm.d/www.conf
The socket must be the same in you nginx block (like vhost in apache). I also moved the socket under /var/sockets
You could try changing user in /usr/local/php/etc/php-fpm.d/www.conf to nginx and restart service.
user = nginx
group = nginx
listen = /tmp/php-fpm.sock
For all those who use Homestead inside Vagrant:
I solved simply reloading vagrant with provision flag:
from project root:
cd Homestead
and then:
vagrant reload --provision
in this manner recompile all configurations
I had this problem, you can do this:
chmod 777 yourpath/php-fpm.sock
This worked for me.
I am trying to setup my environment using Nginx and a fresh project from Symfony 3, both work fine on their own (either starting Nginx by launching php-cgi.exe -b 127.0.0.1:9000 or starting the Symfony 3 project with server:run), however, I can't get Symfony working through Nginx.
The error message I get in my log goes like this:
2016/02/21 00:36:33 [error] 6260#1732: *1 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost"
And this is my code in nginx.conf for the server section so far:
server {
listen 80;
server_name localhost;
root www/projects/mysite.com/web;
rewrite ^/app\.php/?(.*)$ /$1 permanent;
location / {
index app.php;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location ~ ^/(app|app_dev|config)\.php(/|$) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I have placed the 50x.html file in my /web folder and it does show up correctly after the timeout...
I can understand you are using Unix / Linux machine. This is because you haven't enabled (granted the privilege for upstream).
setsebool -P httpd_can_network_connect true
The upstream blocked by selinux by default. Please let me know if this fixes.
Thanks.