How to deploy Symfony3 Application on CentOS - php

I still cannot understand what happening in my server
when i push my code throught the server in /public_html and wanna get the homepage of my application like this:
http:/www.example.com/web/
Server shows me the content of /web (the files and directory that exist inside /web)
and that's happen after i follow step's Command Line to deploy symfony 3 in that link:
https://symfony.com/doc/3.1/deployment.html
I Still cannot understand what's happening there!!
any help would be extremely appreciated!

1-)
yum install nginx
2-)service nginx start
3-) Add Nginx configuration into /etc/nginx/conf.d/project_name.conf
server{
set $web_host "domain_name";
set $web_root "web_directory";
server_name $web_host;
root $web_root;
location / {
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 127.0.0.1:9000;
# 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 127.0.0.1:9000;
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;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
4-) `chmod -R nginx:nginx /var/www/html/project``
5-) service nginx restartand service php-fpm restart

Related

PHP don't process through Nginx in windows WSL

I'm trying to make a local webserver with nginx php 7.1 and mariadb on windows WSL.
I tried a lot of things, use Kali, use Debian, use Ubuntu, change nginx port instead of 80, I found a few nginx config that I tried but none worked.
My problem is that php don't process throught nginx.
When I try to load a simple php page <?php echo 'ok'; ?> it load infinitly.
But when I restart php7.1-fpm, the page work ! (One time and only if I was trying to load it)
This is my actual default nginx config file :
server {
listen 32000 default_server;
listen [::]:32000 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
# # With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
# I tried this too :
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
#fastcgi_index index.php;
#include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
When I run php index.php this is working too. But on firefox nothing happen when I load this file. Everything is fine with full html page. I got the nginx welcome page too with no problems.
I don't know where to look anymore. Thanks for reading and if you have any answer, thanks a lot !
Here is mine:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9123;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME D:/web/html$fastcgi_script_name;
include fastcgi_params;
try_files = $uri #missing;
}
Also make sure PHP-CGI is running (and on the right port).

Do I really have to move my Symfony project to /var/www directory?

I am following this article in creating my symfony project :
Install Symfony3 with nginx in ubuntu 14.04
It says in here that after creating my symfony project :
symfony new project_name
I have to move my project to directory /var/www
mv /project_name /var/www/your-domain.com
But my project is inside a repository in github which cant be moved. How do I proceed? I'm using nginx by the way
Thanks
If you are installing it for development only (say, on your laptop), you don't need a webserver like nginx. You can use the built-in webserver. In your project root execute:
php bin/console server:start
By default the server listens on http://localhost:8000
If you are installing it on a dedicated server install nginx and create this file in /etc/nginx/sites-available name it yoursite. You only have to serve the web directory of your project.
server {
server_name yoursite.com;
root /home/your_user/path/to/your/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.0-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.0-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;
}
Symlink the file to the sites-enabled directory.
sudo ln -s /etc/nginx/sites-available/yoursite /etc/nginx/sites-enabled/yoursite.
Remove the default configuration from the sites-enabled directory. Restart nginx.
See super speed symfony
You may have to set permissions for cache, log and upload directories. See the Symfony Documentation

Symfony - Nginx Configuration error : FastCGI sent in stderr "Primary script unknown"

This is the first time I use nginx with Symfony, and I used the configuration that is on the Symfony document. I have the basic configuration of the latest version of nginx.
However, on my url: 127.0.0.1:83/app_dev.php/fr/admin/organisations
I get the error in my nginx project_error.log file :
2017/06/01 09:16:14 [error] 20204#20204:
*1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream,
client: 127.0.0.1, server: localhost,
request: "GET /app_dev.php/fr/admin/organisations/ HTTP/1.1",
upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "127.0.0.1:83",
referrer: "http://127.0.0.1:83/app_dev.php/fr/admin/organisations/1/edit"
My nginx server config (From Symfony documentation) :
server {
listen 83 default_server;
listen [::]:83 default_server;
server_name localhost;
root /var/www/SymfonySkeleton/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.0-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.0-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;
}
Any ideas ? I strongly assume that my configuration is not good
Thanks !
Remove the lines:
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
Though this may impact on the use of symlinks in your project. It will remove that error message though.
Please check the root path and the files under it .

Symfony2 Nginx auth_basic config don't open web assests

I'm developing a project using Symfony2, Nginx.
Project is located in my subdomain like developing_site.mysite.com.
I'd like to restrict access to this subdomain without authentication. Not only to dev and config files, but also to production.
So i added auth_basic component to nginx config file in location/ sector in nginx config that is recommended by symfony official web site.
As a result, before page loading server asks authentication and loads everything except for any files stores in /web directory like images, js, css and so on. As a result, there is all content processed by .php but without any style and dynamic functionality.
So how can i resolve this issue? What i'm doing wrong?
Nginx config looks like this:
server {
listen {MyServerIp};
server_name developing_site.mysite.com;
root /var/www/developing_site/web;
index index.php index.html index.htm;
location / {
try_files $uri /app.php$is_args$args;
auth_basic "Restricted Content";
auth_basic_user_file var/www/developing_site/.lock/.htpasswd;
}
# 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/php5-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/php5-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;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
I resolved the issue by myself..
Two mistakes:
syntactic mistake
incorrect place ofauth_basic block
The syntactic mistake is in var/www/developing_site/.lock/.htpasswd;. I used relative link instead of absolute. Correct form is /var/www/developing_site/.lock/.htpasswd; (sorry for that...)
When I've placed auth_basic block in location/ I've set authentication only to / location that in fact processes all /web requests... (/web requests wasn't processed because of 1-st mistake...)
Main symfony requests are processed by location ~ ^/(app_dev|config)\.php(/|$) block in nginx config file.
Solution: To restrict any requests to any files of developing_site.mysite.com without authentication, auth_basic block should be place before any location blocks.
So the correct nginx config should looks like this:
server {
listen MyServerIp;
server_name developing_site.mysite.com;
auth_basic "Unauthorized";
auth_basic_user_file /var/www/.lock/.htpasswd;
root /var/www/developing_site/web;
index index.php index.html index.htm;
location / {
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/php5-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/php5-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;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}

Nginx Symfony2 Staging Config

i'm trying to configure nginx to serve several releases of one symfony project.
Releasefolder structure:
/var/www/my-project/releases/24/
/var/www/my-project/releases/46/
/var/www/my-project/releases/47/
I'd like to call a URL like "http://my-server/my-project/release/47" which should access /var/www/my-project/releases/47/web/app.php on the server.
I tried for a while now but couldn't find the final solution, any help would be great!
My last try:
server {
server_name my-server;
location / {
# try to serve file directly, fallback to app.php
try_files $uri $uri$2app.php$is_args$args;
}
location ~ /my-project/release/([-a-zA-Z0-9]+)/app\.php(/|$) {
root /var/www/my-project/releases/$1/web;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/app.php;
fastcgi_param HTTPS off;
error_log /var/log/nginx/my-project_$1_error.log;
access_log /var/log/nginx/my-project_$1_access.log;
# 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;
}
}
This config produces following error:
[error] 2272#0: *169 rewrite or internal redirection cycle while internally redirecting to "/my-project/release/47app.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.php"
You are missing a root directive in your server container -- is /var/www the default on your architecture? Otherwise, add a root directive:
root /var/www;
try_files is failing for find a match and will rewrite the URL by appending app.php to it. The resulting URL does not match your other location block, so it gets rewritten again and again.
The final term on your try_files directive is wrong. You have $2 where /web/ should be (probably inherited from a previous experiment).
Using try_files to make a general rewrite is a bad idea, as any mistyped URL could lead to a rewrite loop. Try:
location / {
rewrite ^(/my-project/release/[-a-zA-Z0-9]+)$ $1/app.php;
}
I changed the config to this:
server {
server_name my-server;
root /var/www;
location / {
rewrite ^(/my-project/release/[-a-zA-Z0-9]+)$ $1/app.php;
}
location ~ /my-project/release/([-a-zA-Z0-9]+)/app\.php(/|$) {
alias /var/www/my-project/releases/$1/web;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/app.php;
fastcgi_param HTTPS off;
error_log /var/www/my-project/releases/$1/app/logs/nginx_error.log;
access_log /var/www/my-project/releases/$1/app/logs/nginx_access.log;
# 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;
}
}
Now a call like "http://my-server/my-project/release/47" points correctly to "/var/www/my-project/releases/47/web/app.php".
The problem is now that the url "my-project/release/47" arrives at symfony router which can't find a route to this url.
What is now the right way to keep the url showing up like "//my-server/my-project/release/47" at browser but arrives at symfony as "//my-server/"?
Thank you all for your help, unfortunately i couln't get it working. I think i have to learn more about nginx configuration generally to be able to handle such problems, before i can use it in production.
My workaround for my problem is to create an own nginx conf per release. The file will be created at build process, is then transfered to the webserver, copied to /etc/nginx/conf.d/[releasename].conf and activated by sudo service nginx reload.
A deinstallation script within the release dir removes sources and config, when release no longer needed.

Categories