VPS 2GB RAM memory and Symfony 3 - Out of memory - nginx

I'm trying to install a symfony 3.4 project on a Debian 8 server. I'm using Nginx as web server.
My config server :
RAM = 2 GB
Memory 20 GB
The composer install works fine (the composer update too).
I had also an angular front in the same server. Everything had works fine (npm install, ng build, ..).
But when I request any route of my API, I get an error with code 500. I can't even get the profiler.
Inside the log file of nginx, I can see this error message log message but I configured the php memory_limit = -1.
My nginx conf file :
server {
listen 80;
server_name my_server_name;
root location/of/my/front;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
server_name my_server_name_api;
root location/of/my/api/web;
location / {
try_files $uri /app.php$is_args$args;
}
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
Do you have any idea ?
Thanks

This error could be due to your PHP.ini settings file having a low limit of memory, PHP can have a set limit of memory that it's allowed to use, and typically it's not set dynamically meaning that it requires you to change it to the value that you want to use.
To do this, find the php.ini file, and find the line with memory_limit on it and then change it to:
memory_limit = -1
This will allow PHP to use as much ram as it can, this is not ideal in some situations, and often times you'll find most websites only need around 1gb of memory to run, so try:
memory_limit = 1024
Instead, you could look at other methods such as disabling profiler in Symfony if you are not intending on using it as it's the most eager component within Symfony. To do this add this snippet to your code and it will where you would like to disable profiler:
if ($this->container->has('profiler')){
$this->container->get('profiler')->disable();
}
or if you want to disable to globally you can set the global parameter within the config.
framework:
profiler:
collect: false
You can also set the memory_limit vairable via PHP by putting ini_set('memory_limit', '-1'); into your main .php file.

Related

Why is my site downloaded instead of running?

I'm trying to install Wordpress on a Ubuntu 18.04 on a subdomain. I set the Nginx files on sites-available, but I get a 502 error on browser because Wordpress is using a .php file type for the index, so I added "index.php" on the list in sites-available. Well after adding "index.php" on the list when I try to access the URL in browser it downloads a file named with the subdomain address.
Here's my code in sites-available
server {
listen 80;
listen [::]:80;
root /var/www/apt;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;
server_name apt.forrum.ro;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
Please let me know how to fix it.
This is simplified, basically Nginx uses the try_files directive to serve the file to user in the folder. This is why your php file is being sent to the user, it's then downloaded rather than shown as browsers don't really know how to show PHP to the user.
What you need to do is tell Nginx to run the file. In the case of PHP you can use FastCGI. There are many guides to doing this on ubuntu such as This One.
Once you have it installed, all the directives for FastCGI are described by Nginx themselves Here.
Their example is posted here:
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# include the fastcgi_param setting
include fastcgi_params;
# SCRIPT_FILENAME parameter is used for PHP FPM determining
# the script name. If it is not set in fastcgi_params file,
# i.e. /etc/nginx/fastcgi_params or in the parent contexts,
# please comment off following line:
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Nginx load subpath as wordpress root

I'm trying to set up a Wordpress in a system that has another php application installed, using nginx as web server.
I've simplified my config file to the maximun. The following confi is serving one post of my blog:
server {
listen 80;
server_name blog.ct.com;
root /home/ff/www/blog/;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$uri&$args =405;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_buffer_size 128k;
fastcgi_buffers 64 32k;
fastcgi_busy_buffers_size 128k;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param APPLICATION_ENV development;
fastcgi_param HTTP_X_FORWARDED_PROTO https;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
}
But, due my system's requirements I need to serve the blog from within a sub path (In my final system http://blog.ct.com/ should be serving my custom php app and http://blog.ct.com/vendor should be serving the wordpress blog).
The local root directory from wordpress must be /home/ff/www/blog/ (this cannot be changed, while my custom app's directory is /home/ff/www/myapp/). So I think I need to reserve location / for my custom app, I have to create a location /vendor
If I add /vendor and I return 403 in / (just to debug easier), the browser says 405 (notice the =405 in /vendor, also to debug easier):
location /vendor {
try_files $uri $uri/ /index.php?$uri&$args =405;
}
location / {
return 403;
}
So I think nginx is going into location /vendor but is not finding my php script in /home/ff/www/blog/index.php so its returning the fallback 405.
Any idea why this could happen?
How can I achieve to load http://blog.ct.com/vendor as the root from wordpress but keeping http://blog.ct.com/ using another php script?
I've found out the following hints that gave me the clue to fix the problem (in case someone has the same problem than me, this may help)
Using location /path is not the same as using location ~(/path) (regex have different priority, so maybe they are not being checked in the order you think)
Adding error_log /your/path/log/error.log debug; to any location block may help you to see how is nginx serving every request (e.g. to location fastcgi, location \vendor, or the server{ block).
alias /var/www/path/vendor works different than root /var/www/path/vendor (check Nginx -- static file serving confusion with root & alias);
In case of the root directive, full path is appended to the root including the location part, whereas in case of the alias directive, only the portion of the path NOT including the location part is appended to the alias.
using rewrite with alias can help you parse the php file you want independent of the path
if (!-f $request_filename) {
rewrite ^ $document_root/index-wp.php last;
}
Take care of the SCRIPT_FILENAME you are using (check it with error_log, see above), maybe you need fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; but you are loading fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; so depending on your previous config you may be attaching the document root twice.
Two different configurations for fastcgi can be used if you change your index.php file names. E.g. location ~ wp\.php$ { will work with wp.php while location ~ \.php$ { will work with all other php files like index.php.

Ubuntu Nginx - changing the server root directory

I really depends on changing the server root directory from /var/www to the /srv/http.
Because if I sue:
server {
server_name www.domain.tld;
root /srv/http/project/web;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /app.php$is_args$args;
}
# DEV
# This rule should only be placed on your development environment
# In production, don't include this and don't deploy app_dev.php or config.php
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/app.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
does not work.
The system is Ubuntu 16.04 and server Nginx.
Thanks in advance for your help.
First of all your root directory is /srv/http/project/web but not /var/www, and in order to change it to /srv/http, you got to find root /srv/http/project/web; in above configuration(which is third line of the same) and change it to root /srv/http;. -Thank You :)

Nginx, fastcgi PHP Windows, No input file specified

Running php-cgi on port 9000
Netstat gives me
TCP 127.0.0.1:9000 DESKTOP-xxxxxxx:0 LISTENING
[php-cgi.exe]
nginx.conf
http://pastebin.com/wkfz8wxw
Every php file gives me this No input file specified. error...
Changed SCRIPT_FILENAME to SCRIPT_NAME and no succes..
I am on Windows 10 Home x64
You need to set a document root with the root directive, either within your location ~ \.php$ block or inherited from the outer server block.
The solution may be to move the root c:/Users/Youri/PhpstormProjects; line out of your location / block into a position above it.
Usually fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; is the correct method to specify the full path to the script, whereas SCRIPT_NAME is usually just the last element.
Like this:
server {
...
root c:/Users/Youri/PhpstormProjects;
location / {
index index.html index.htm index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Set root directive with absolute path in windows PC.
Check for the case or any typing mistake in the path.
Reload the nginx process.
May be the root path problem. I used the backslash \ on Windows and found this issue.
server {
location ~ \.php$ {
- root C:\Users\name\ProjectOfPHP; # 404
+ root C:/Users/name/ProjectOfPHP; # Use this pattern
}
}

FPM/Nginx: unable to open primary script

I'm struggling to get NGINX and PHP-FPM to talk effectively. My NGINX configuration file includes the following definition for the api offset:
location /api {
try_files $uri /api/index.php$is_args$args;
gzip off;
fastcgi_pass PHP:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_keep_conn off;
include fastcgi_params;
}
and on my PHP machine I have the following Pool configuration:
[api]
listen = 9000
user = www-data
group = www-data
pm = dynamic
pm.max_children = 10
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 5
pm.status_path = /status
I then tail the NGINX server's log file and when hitting the webserver with:
http://localhost/api
# or
http://localhost/api/index.php
# or ...
http://localhost/api/resources.json
I can see from the NGINX server log that NGINX is correctly matching the /api pattern but i get the following errors:
[error] 14#0: *1 FastCGI sent in stderr: "Unable to open primary script: /app/html/websites/couchbase/api/index.php (No such file or directory)"
Where the root directory on the PHP machine is /app/html/websites/couchbase. I'm at a complete loss on what this error really means or more importantly how to debug it from here. Any help would be greatly appreciated.
p.s. I have tried replacing $document_root reference in the fast_cgi_param to hard coded values to see if it makes a difference. It does in the sense that if I point it at an incorrect directory it gives me
a No input file specified. error. The only place where I get the error is when it seemingly is pointed to the right place.
Added this in case it helps clarify ... this is the file system at /app/html/websites/couchbase/api on the PHP/FPM machine.
I created the test.php file which basically just echo's "Ok" back but that works no better than the primary goal of running index.php.
note: I wasn't sure if execution permissions were important -- as you can see test.php does not have them set in the picture -- but I've tried them both ways and it appears to make no difference.
The error is complaining about the index.php file not found in the root directory. Is it under ...couchbase/api or is in under .../couchbase? In the second case you can fix this problem with a redirection.
I think you can move try_files outside the location block. I never used $is_args and $args in this kind of redirection, and I've not tested it but IMHO I think you can remove it.
I think you can fix this problem with this configuration:
root /app/html/websites/couchbase;
try_files $uri /api/index.php$is_args$args;
location /api {
rewrite /api/(.*) $1 break;
gzip off;
fastcgi_pass PHP:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_keep_conn off;
include fastcgi_params;
}
You can have a cleaner config file if you'll include the following directives:
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_keep_conn off;
inside the fastcgi_params file.

Resources