Configuring Nginx Web Server On Ubuntu, CentOS And openSUSE

So, yesterday we showed you how to install Nginx web server on Ubuntu, CentOS and openSUSE. To view that post, please click this link.

Installing Nginx on these distributions is easy, especially on Ubuntu. Now that you’ve learned how to install Nginx, lets continue below and show you how to configure it.

Installing Nginx web server is your first challenge. The next challenge is configuring it so that it accepts connections.

This post will show you how to do that on Ubuntu, CentOS and openSUSE, if you don’t already know how to do it.

Again, to learn how to install Nginx, please go back to our previous post. For those who are just starting.. please refer to our post on installing Nginx before continuing below.

 

  • Configuring Nginx on Ubuntu

Now that you know how to install Nginx on Ubuntu, below is how to configure it. After installing Nginx, all its configuration files and settings files are stored in the /etc/nginx directory.

Nginx global (Main) configuration file is at /etc/nginx/nginx.conf and it’s default (server) site configuration file is at /etc/nginx/sites-available/default

The default root directory for Nginx web server is at /var/www/html .. That info can be found in the default config file.

By default, Nginx web server can only respond to requests for html files. Nginx is not PHP enabled by default.

Put all your HTML files in the root directory of Nginx to be served. To make Nginx PHP enabled, change the default (server) site config file to look like the one below.

# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 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 the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php5-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
#
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
#

That’s it! That’s the bare minimum to get Nginx working for PHP scripts. For the above to work with PHP scripts, you should install this PHP packages.

sudo apt-get install php5 php5-fpm

 

  • Configuring Nginx on CentOS

Now that you’ve installed Nginx on CentOS machine, it’s now time to configure it. Just like above for the Ubuntu setup, Nginx keeps all its configuration in this directory /etc/nginx/  .

Nginx’s global (Main) configuration file is at /etc/nginx/nginx.conf and its default (server) site configuration file is at /etc/nginx/conf.d/default.conf

The default root directory for Nginx web server on Ubuntu is at /usr/share/nginx/html ..

By default only HTML files are retrieved and no support for PHP scripts. So, put all your html files in the default root directory to be served.

To make PHP scripts enabled on CentOS, you must edit the default site configure file to look like the one below.

server {
listen 80;
server_name localhost;
#
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
#
location / {
root /usr/share/nginx/html;
index index.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 /usr/share/nginx/html;
}
#
# 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$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
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;
#}
}

Save the file. That’s the basic configuration to get Nginx service PHP scripts.

 

  • Configuring Nginx on openSUSE

Just like both Ubuntu and CentOS, openSUSE keeps all its Nginx configuration files in the /etc/nginx directory.

Nginx’s global (Main) configuration file is located at /etc/nginx/nginx.conf .. Both the global and default site (server) configurations are stored in the same file called nginx.conf in the /etc/nginx directory.

Nginx root directory on openSUSE is at /srv/www/htdocs/

By default only HTML files are served.. to enable PHP scripts, modify the configuration file to look like the one below.

 

#user nginx;
worker_processes 1;
#
#error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;
#
#pid /var/run/nginx.pid;
events {
worker_connections 1024;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
#
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#
#access_log /var/log/nginx/access.log main;
#
sendfile on;
#tcp_nopush on;
#
#keepalive_timeout 0;
keepalive_timeout 65;
#
#gzip on;
#
include conf.d/*.conf;
#
server {
listen 80;
server_name localhost;
#
#charset koi8-r;
#
#access_log /var/log/nginx/host.access.log main;
#
location / {
root /srv/www/htdocs/;
index index.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$ {
 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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
#
# location / {
# root /srv/www/htdocs/;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
#
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
#
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
#
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# location / {
# root /srv/www/htdocs/;
# index index.html index.htm;
# }
#}
#
include vhosts.d/*.conf;
}

Save the file and you’re done. To get PHP scripts to work, you must install PHP-FPM.. To do that, run the commands below.

zypper install php php-fpm

That’s it!

There might be more configurations needed in these three environments, but these settings will get you started.

Enjoy!

 

Leave a Reply

Your email address will not be published. Required fields are marked *