PDA

Bekijk Volledige Versie : Nginx: Php bestanden worden gedownload ipv geopend



BaseBoyNL
24/08/12, 09:25
Hey all,

Na wat dingen te hebben gelezen over nginx wilde ik dit wel eens gaan proberen.
De installatie van nginx ging vrij gemakkelijk met het opzetten van virtualhosts echter is het php deel een heel ander verhaal.
Ik krijg het maar niet voor elkaar om de php bestanden te laten uitvoeren, wanneer je naar een php bestand gaat wordt deze gedownload ipv uitgevoerd.

Wanneer ik op de commandline php /bestandlocatie/iets.php doet wordt deze wel netjes uitgevoerd.
Ik ben aan de slag gegaan met verschillende tutorials. Waar het bij hun schijnbaar werkt krijg ik het niet voor elkaar ik denk dus dat ik iets over het hoofd zie.

Ik heb php geïnstalleerd doormiddel van:

sudo apt-get install php-fpm

Mijn /etc/nginx/sites-available/default:


server {
listen 80; ## listen for ipv4; this line is default and implied
listen [::]:80 default ipv6only=on; ## listen for ipv6

root /usr/share/nginx/www;
index index.html index.htm;

# Make site accessible from http://localhost/
server_name localhost;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}

location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}

# Only for nginx-naxsi : process denied requests
#location /RequestDenied {
# For example, return an error code
#return 418;
#}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/www;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

# try_files $uri =404;
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;

# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
# fastcgi_pass unix:/var/run/php5-fpm.sock;
# fastcgi_index index.php;
# include fastcgi_params;
#}

location ~ \.php$ {
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;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# root html;
# index index.html index.htm;
#
# location / {
# try_files $uri $uri/ /index.html;
# }
#}

server {
listen 80;
server_name nginx.domein.nl;

index index.html index.php index.html;
root /home/ruben/www;
}

# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
#
# root html;
# index index.html index.htm;
#
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
#
# ssl_session_timeout 5m;
#
# ssl_protocols SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSL v3:+EXP;
# ssl_prefer_server_ciphers on;
#
# location / {
# try_files $uri $uri/ /index.html;
# }


Alvast bedankt voor jullie tijd en moeite.

PimEffting
24/08/12, 10:49
Hier heb ik ook ooit mee zitten stoeien.
Probleem was toen dat in php-fpm standaard de shortopentags uit stonden (dus dat je alleen <?php ipv <? kan gebruiken)
Misschien is het ook zo simpel bij jou

magentohosting
24/08/12, 10:54
Ook ik heb hier mee zitten stoeien, maar ook zo weer opgelost.
Bij mij was het geval dat het stuk PHP in het, in dit geval bij jou, default configuratiebestand niet goed stond.

Ik include in default een php.conf.
In deze php.conf staat het volgende:



fastcgi_intercept_errors on;

# this will allow Nginx to intercept 4xx/5xx error codes
# Nginx will only intercept if there are error page rules defined
# -- This is better placed in the http {} block as a default
# -- in that virtual host's server block

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# A handy function that became available in 0.7.31 that breaks down
# The path information based on the provided regex expression
# This is handy for requests such as file.php/some/paths/here/

include fastcgi_params;

fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
}


De fastcgi_params die geïnclude wordt in bovenstaande php.conf:


fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;

fastcgi_connect_timeout 90;
fastcgi_send_timeout 180;
fastcgi_read_timeout 360;
fastcgi_buffer_size 1024k;
fastcgi_buffers 8 512k;
fastcgi_busy_buffers_size 1024k;
fastcgi_temp_file_write_size 1024k;
fastcgi_intercept_errors on;
fastcgi_pass_header *;


Hiermee draait het bij ons succesvol op een aantal servers :)

BaseBoyNL
24/08/12, 11:32
Hier heb ik ook ooit mee zitten stoeien.
Probleem was toen dat in php-fpm standaard de shortopentags uit stonden (dus dat je alleen <?php ipv <? kan gebruiken)
Misschien is het ook zo simpel bij jou

Dit is helaas niet het probleem het geen wat ik probeer te openen is:

<?php phpinfo(); ?>

@magentohosting
Hoe bedoel je precies php.conf, staat deze in de map bij default ?
Kun je eens het stukje van de include in de default hier posten ?

Nogmaals enorm bedankt voor de hulp !

magentohosting
24/08/12, 12:08
Er bestaat geen php.conf standaard. Deze moet je eerst zelf aanmaken (nano php.conf). Wel vanuit de nginx directory doen.
Vervolgens plak je hier het stukje code wat ik eerder gepost had en sla je die op.

In de config van elke vhost voeg je onderaan:


include /etc/nginx/conf.d/php.conf


Uiteraard wel even kijken wat het juiste pad voor jou is ;).
Het is dan niet meer nodig om in elke vhost config de 'location ~.php' te plaatsen met alles wat erbij hoort. Dit staat immers in je php.conf die je include.
Een vhost config zal er dan voor jou zo uit komen de te zien:




server {
listen 80; ## listen for ipv4; this line is default and implied
listen [::]:80 default ipv6only=on; ## listen for ipv6

root /usr/share/nginx/www;
index index.html index.htm;

# Make site accessible from http://localhost/
server_name localhost;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}

location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}

## Include van php.conf --> even juiste pad zoeken!
include /etc/nginx/conf.d/php.conf;
}

# another virtual host using mix of IP-, name-, and port-based configuration
server {
listen 80;
server_name nginx.domein.nl;

index index.html index.php index.html;
root /home/ruben/www;

include /etc/nginx/conf.d/php.conf;
}


Je hebt nu meerdere vhosts in 1 configuratiebestand.
Als deze straks werken is dat heel erg mooi. Ik wil je dan nog wel als tip toevoegen om een mapje ergens in de nginx map te maken waar je alle vhosts in plaatst in losse configuratiebestanden. Bijvoorbeeld domeinnaam.conf en domeinnaam2.conf. Vervolgens doe je een include van deze bestanden in je configuratie van nginx (het master config bestand zegmaar). Hier voeg je dan toe onderaan: "include /etc/nginx/vhosts/*.conf".

BaseBoyNL
24/08/12, 16:48
Helaas werkt het nog niet :( snap er echt werkelijk niks van.
Heb precies gedaan wat je heb gezegt maar nog download die de php bestanden.

Zijn er nog anderen oplossingen ?

magentohosting
24/08/12, 17:56
Wilde gok.. staat PHP-FPM wel te draaien? Lijkt me wel, maar even om het uit te sluiten.

Welk OS gebruik je trouwens? Ubuntu?

xaban
24/08/12, 18:01
Helaas werkt het nog niet :( snap er echt werkelijk niks van.
Heb precies gedaan wat je heb gezegt maar nog download die de php bestanden.

Zijn er nog anderen oplossingen ?
De oplossing is er vast en zeker, maar zo op afstand is het altijd lastig debuggen. Wat zeggen de logs?

BaseBoyNL
25/08/12, 00:45
Jep PHP-FPM staat te draaien en luistert naar port 9000.
Inderdaad draait alles op Ubuntu 12.04.

@xaban
De logs zeggen helaas helemaal niks, omdat er ook geen fout is.
Het enigste wat nginx doet is jou het php bestand leveren inplaats van uitvoeren.

magentohosting
25/08/12, 10:30
Ik weet zeker dat het een configuratie fout is in één (of meerdere) config bestanden.
Waarom? Ik heb hetzelfde gehad tijd terug en toen heb ik config aangepast en toen deed hij het wel weer.

Nu weet ik alleen niet meer hoe ik dat gedaan had. Zal er nog is goed over nadenken! ;)

xaban
25/08/12, 13:32
Jep PHP-FPM staat te draaien en luistert naar port 9000.
Inderdaad draait alles op Ubuntu 12.04.

@xaban
De logs zeggen helaas helemaal niks, omdat er ook geen fout is.
Het enigste wat nginx doet is jou het php bestand leveren inplaats van uitvoeren.

Logs zeggen meer dan alleen foutmeldingen. Misschien wordt je configuratie inderdaad niet goed gelaten oid.

Stuur anders een PM als je er niet uitkomt.

Optimaal online
27/08/12, 09:28
Kunnen meerdere zaken zijn, bijvoorbeeld:
- je script maakt gebruik van short open tags oftewel <? inplaats van <?php bovenaan je script
- je try_files verwijst naar index.html, dit moet waarschijnlijk index.php zijn
- je maakt gebruik van eaccelerator of een dergelijke module welke niet goed functioneert waardoor het php script niet volledig uitgevoerd kan worden.