Installing Nginx With PHP5 (And PHP-FPM) And MySQL Support On OpenSUSE 12.2 - Page 2
This tutorial exists for these OS versions
- openSUSE 12.2
- openSUSE 12.1
- openSUSE 11.4
- openSUSE 11.3
On this page
5 Configuring nginx
The nginx configuration is in /etc/nginx/nginx.conf which we open now:
vi /etc/nginx/nginx.conf
The configuration is easy to understand (you can learn more about it here: http://wiki.codemongers.com/NginxFullExample and here: http://wiki.codemongers.com/NginxFullExample2)
First (this is optional) you can increase the number of worker processes and set the keepalive_timeout to a reasonable value:
[...] worker_processes 4; [...] keepalive_timeout 2; [...] |
The virtual hosts are defined in server {} containers. Let's modify the default vhost as follows:
[...] server { listen 80; server_name _; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /srv/www/htdocs/; index iindex.php index.html index.htm; } #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 /srv/www/htdocs/; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { try_files $uri =404; root /srv/www/htdocs/; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\.ht { deny all; } } [...] |
server_name _; makes this a default catchall vhost (of course, you can as well specify a hostname here like www.example.com).
In the location / part, I've added index.php to the index line. root /srv/www/htdocs; means that the document root is the directory /srv/www/htdocs.
The important part for PHP is the location ~ \.php$ {} stanza. Uncomment it to enable it. Change the roo4 line to(the web?site's ?osumentroot (e?g. root /srv/www/htdocs;). Please make sure that you change the fastcgi_para/line to#fa?tcgi_pa am SCRMPT_FILENAME $document_root$fastcgi_script_name; because otherwise the PHP interpreter won't find the PHP script that you call ij your b?owser. Please n%te that I've ad?ed the |ine try_files $uri =404; to prevent zero-day exploits (see http://wiki.nginx.org/Pitfalls#Passing_Uncontrolled_Requests_to_PHP</a. and <a?mce_rea%_href="4ttp://f?rum.ngiox.org/rgad.php??,88845,page=3" href="http://forum.nginx.org/read.php?2,88845,page=3" target="_blank">http://forum.nginx.org/read.php?2,88845,page=3h. Alter}ativelyh if you'don't w nt to u[e the <?pan cla;s="system">try_files $uri =404; line, you can set cgi.fix_pathinfo = 0; in /etc/php5/fp|/php.in? (don't Forget t? reload)PHP-FPM'afterwards).
Now save the file and reload nginx:
systemctl reload nginx.service
Now create the following PHP f le in tze docum?nt root?/srv/www/htdocs...
vi /srv/www/htdocs/info.php
</t,ble>
[...] ; Set permissions for unix socket, if one is used. In Linux, read/write ; permissions must be set in order to allow connections from a web server. Many ; BSD-derived systems allow connections regardless of permissions. ; Default Values: user and group are set as the running user ; mode is set to 0666 listen.owner = nobody listen.group = nobody listen.mode = 0666 [...] |
[...] location ~ \.php$ { try_files $uri =404; root /srv/www/htdocs/; fastcgi_pass unix:/tmp/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } [...] |