Osclass with Nginx, PHP-FPM for better performance

Osclass is a great open source classified script based on PHP and very fast performer compared to other scripts in this league. Most Osclass installations runs on Apache web-server, which is a hassle free solutions to some. But it comes with its own problems, as Apache load php module in every process even if browser is requesting image or js script, making it easy to choke your server with sudden spikes of traffic.

Nginx on the other hand is very light-weight server with very low memory footprint and serve request insanely fast. Adding Php-fpm to Nginx makes it super efficient web-server for PHP based dynamic website. In this guide we will help you to set up your classified website with Nginx (Basically on LEMP Stack). We are using Ubuntu OS for this guide.

1. Installing Nginx, Php-fpm and MySQL

With Ubuntu it is super easy to install LEMP stack.

$ sudo apt-get update
$ sudo apt-get install nginx
$ sudo apt-get install php-fpm
$ sudo apt-get install mysql-server-5.6

Above command will take care of installing Nginx, Php-fpm and MySQL on your server. You’ll be prompted to create a root password during MySQL installation. Choose a secure one and make sure you remember it, because you’ll need it later.

2. Setting up Osclass

Osclass requires a database, so login to the MySQL server with the root user and create a new user and database that Osclass use to store information.

$ mysql -u root -p

CREATE DATABASE osclass;
CREATE USER 'osclassuser'@'localhost' IDENTIFIED BY 'PASSWORD';
GRANT ALL PRIVILEGES ON 'osclass'.* TO 'osclassuser'@'localhost';
FLUSH PRIVILEGES;
exit

Don’t forget to replace ‘PASSWORD’ with an actual strong password of your choice. Above Command will create an empty database, named ‘osclass’ with user ‘osclassuser’.

Go to the official  website and download the latest release of their application or run command given below (Currently 3.6.1 is a latest version).

$ wget https://static.osclass.org/download/osclass.3.6.1.zip

Create a new directory for Osclass in the document root directory on your server and unpack the downloaded ZIP archive to the that directory

$ mkdir /var/www/example.com
$ unzip osclass.3.6.1.zip -d /var/www/example.com

Now make www-data as owner and user for www directory.

$ chown -R www-data:www-data /var/www

3.Adding Osclass virtual host to Nginx

Nginx provides us with a layout for virtual-host file in the sites-available directory (/etc/nginx/sites-available), and we simply need to copy the text into a new custom file:

$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com

Now delete all content from it, and paste our properly tested configuration.

server {
        listen 80; 
        listen [::]:80;
        root /var/www/example.com; #your document root
        server_name www.example.com example.com ;  # Your server name
        index index.php index.html index.htm ;

# Webfonts,fonts
  location ~* \.(?:ttf|ttc|otf|eot|woff|woff2)$ {
           add_header "Access-Control-Allow-Origin" "*";
           expires 1y;
           access_log off;
           add_header Cache-Control "public";
   }

# Media: images, icons, video, audio, HTC
   location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
           expires 1y;
           access_log off;
           add_header Cache-Control "public";
   }

# Js, Css
  location ~* \.(?:css|js)$ {
           expires 1y;
           access_log off;
           add_header Cache-Control "public";
  }

# Serve static files without touching php
  location / {
           try_files $uri $uri/ /index.php?$args;
  } 
# pass requests for dynamic content to site
  location ~ [^/]\.php(/|$) {
           try_files $uri /index.php; 
           include fastcgi_params;
           fastcgi_pass unix:/var/run/php-fpm.sock;
  }
}

Save file and enable it for Nginx

$ ln -s /usr/local/nginx/sites-available/example.com /usr/local/nginx/sites-enabled/example.com

Now restart your nginx server.

$ service nginx restart

Now goto http://www.example.com in your browser and fill required information in Installer (Use Database Name and User we created earlier). Follow instructions on that page. Configuration we posted above is compatible with permalink, so you can enable permalink in osclass through admin dashboard.

Whats more?

That’s it! Now you have one lean, mean server for your website. In the next post, I’ll be looking at adding more help guide for osclass. For now you maybe interested in our post about installing let’s encrypt ssl certificate in Ubuntu server.

12 thoughts on “Osclass with Nginx, PHP-FPM for better performance

  1. Hello, nice tutorial, i use VestaCP with nginx and PHP-FPM, my nginx.conf file is:

    server {
        listen      194.135.88.106:80;
        server_name duooglasi.com www.duooglasi.com;
        root        /home/insha/web/duooglasi.com/public_html;
        index       index.php index.html index.htm;
        access_log  /var/log/nginx/domains/duooglasi.com.log combined;
        access_log  /var/log/nginx/domains/duooglasi.com.bytes bytes;
        error_log   /var/log/nginx/domains/duooglasi.com.error.log error;
    
        location / {
    
            location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
                expires     max;
            }
    
            location ~ [^/]\.php(/|$) {
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                if (!-f $document_root$fastcgi_script_name) {
                    return  404;
                }
    
                fastcgi_pass    127.0.0.1:9003;
                fastcgi_index   index.php;
                include         /etc/nginx/fastcgi_params;
            }
        }
    
        error_page  403 /error/404.html;
        error_page  404 /error/404.html;
        error_page  500 502 503 504 /error/50x.html;
    
        location /error/ {
            alias   /home/insha/web/duooglasi.com/document_errors/;
        }
    
      location ~* "/\.(htaccess|htpasswd)$" {
            deny    all;
            return  404;
        }
    
        include     /etc/nginx/conf.d/phpmyadmin.inc*;
        include     /etc/nginx/conf.d/phppgadmin.inc*;
        include     /etc/nginx/conf.d/webmail.inc*;
    
        include     /home/insha/conf/web/nginx.duooglasi.com.conf*;
    }

    how can i enable permal links (friendly urls)
    this is .htaccess

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    

    ?

    1. Use these rules from above configuration.

      # Serve static files without touching php
        location / {
                 try_files $uri $uri/ /index.php?$args;
        } 
      # pass requests for dynamic content to site
        location ~ [^/]\.php(/|$) {
                 try_files $uri /index.php; 
                 include fastcgi_params;
                 fastcgi_pass unix:/var/run/php-fpm.sock;
        }
      
        1. Use this configuration and enable permalink in osclass dashboard after nginx reload.

          server {
              listen      194.135.88.106:80;
              server_name duooglasi.com www.duooglasi.com;
              root        /home/insha/web/duooglasi.com/public_html;
              index       index.php index.html index.htm;
              access_log  /var/log/nginx/domains/duooglasi.com.log combined;
              access_log  /var/log/nginx/domains/duooglasi.com.bytes bytes;
              error_log   /var/log/nginx/domains/duooglasi.com.error.log error;
          
          
          	location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
                      expires     max;
          	}
                  
              error_page  403 /error/404.html;
              error_page  404 /error/404.html;
              error_page  500 502 503 504 /error/50x.html;
          
              location /error/ {
                  alias   /home/insha/web/duooglasi.com/document_errors/;
              }
          
              location ~* "/\.(htaccess|htpasswd)$" {
                  deny    all;
                  return  404;
              }
          	# Serve static files without touching php
            	location / {
                     try_files $uri $uri/ /index.php?$args;
            	} 
          	# pass requests for dynamic content to site
            	location ~ [^/]\.php(/|$) {
                      try_files $uri /index.php; 
                      fastcgi_pass    127.0.0.1:9003;
                      fastcgi_index   index.php;
                      include         /etc/nginx/fastcgi_params;
            	}
          
              include     /etc/nginx/conf.d/phpmyadmin.inc*;
              include     /etc/nginx/conf.d/phppgadmin.inc*;
              include     /etc/nginx/conf.d/webmail.inc*;
          
              include     /home/insha/conf/web/nginx.duooglasi.com.conf*;
          }
          
          1. This is my error log:

            2016/07/20 12:32:13 [emerg] 14040#14040: unknown directive “—” in /home/insha/conf/web/nginx.conf:33
            2016/07/20 12:53:39 [emerg] 15062#15062: socket() [::]:80 failed (97: Address family not supported by protocol)
            2016/07/20 12:56:46 [crit] 15275#15275: *1 connect() to unix:/var/run/php-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 93.92.25$
            2016/07/20 12:56:46 [crit] 15275#15275: *1 connect() to unix:/var/run/php-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 93.92.25$
            2016/07/20 12:56:47 [crit] 15275#15275: *1 connect() to unix:/var/run/php-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 93.92.25$
            2016/07/20 12:56:47 [crit] 15275#15275: *1 connect() to unix:/var/run/php-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 93.92.25$
            2016/07/20 12:57:12 [emerg] 15292#15292: unexpected “}” in /home/insha/conf/web/nginx.conf:42
            2016/07/20 12:57:36 [crit] 15275#15275: *1 connect() to unix:/var/run/php-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 93.92.25$
            2016/07/20 12:57:36 [crit] 15275#15275: *1 connect() to unix:/var/run/php-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 93.92.25$
            2016/07/20 12:57:37 [crit] 15275#15275: *1 connect() to unix:/var/run/php-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 93.92.25$
            2016/07/20 12:57:37 [crit] 15275#15275: *1 connect() to unix:/var/run/php-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 93.92.25$
            2016/07/20 13:00:18 [emerg] 15509#15509: “location” directive is not allowed here in /home/insha/conf/web/nginx.conf:47
            2016/07/20 13:00:53 [emerg] 15524#15524: unexpected end of file, expecting “}” in /home/insha/conf/web/nginx.conf:47
            2016/07/20 13:05:56 [emerg] 15768#15768: unexpected end of file, expecting “}” in /home/insha/conf/web/nginx.conf:46
            2016/07/20 13:06:34 [emerg] 15783#15783: “location” directive is not allowed here in /home/insha/conf/web/nginx.conf:47
            2016/07/20 13:07:35 [emerg] 15798#15798: duplicate location “/” in /home/insha/conf/web/nginx.conf:36
            2016/07/20 13:08:39 [emerg] 15813#15813: invalid number of arguments in “location” directive in /home/insha/conf/web/nginx.conf:36
            2016/07/20 13:10:57 [emerg] 16058#16058: “location” directive is not allowed here in /home/insha/conf/web/nginx.conf:1

          2. Just like yours:
            server {
            listen 194.135.88.106:80;
            server_name duooglasi.com http://www.duooglasi.com;
            root /home/insha/web/duooglasi.com/public_html;
            index index.php index.html index.htm;
            access_log /var/log/nginx/domains/duooglasi.com.log combined;
            access_log /var/log/nginx/domains/duooglasi.com.bytes bytes;
            error_log /var/log/nginx/domains/duooglasi.com.error.log error;

            location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
            expires max;
            }

            error_page 403 /error/404.html;
            error_page 404 /error/404.html;
            error_page 500 502 503 504 /error/50x.html;

            location /error/ {
            alias /home/insha/web/duooglasi.com/document_errors/;
            }

            location ~* “/\.(htaccess|htpasswd)$” {
            deny all;
            return 404;
            }
            # Serve static files without touching php
            location / {
            try_files $uri $uri/ /index.php?$args;
            }
            # pass requests for dynamic content to site
            location ~ [^/]\.php(/|$) {
            try_files $uri /index.php;
            fastcgi_pass 127.0.0.1:9003;
            fastcgi_index index.php;
            include /etc/nginx/fastcgi_params;
            }

            include /etc/nginx/conf.d/phpmyadmin.inc*;
            include /etc/nginx/conf.d/phppgadmin.inc*;
            include /etc/nginx/conf.d/webmail.inc*;

            include /home/insha/conf/web/nginx.duooglasi.com.conf*;
            }

            And i hope you wanted this:

            # Server globals
            user nginx;
            worker_processes 2;
            error_log /var/log/nginx/error.log;
            pid /var/run/nginx.pid;

            # Worker config
            events {
            worker_connections 1024;
            use epoll;
            }

            http {
            # Main settings
            sendfile on;
            tcp_nopush on;
            tcp_nodelay on;
            client_header_timeout 1m;
            client_body_timeout 1m;
            client_header_buffer_size 2k;
            client_body_buffer_size 256k;
            client_max_body_size 256m;
            large_client_header_buffers 4 8k;
            send_timeout 30;
            keepalive_timeout 60 60;
            reset_timedout_connection on;
            server_tokens off;
            server_name_in_redirect off;
            server_names_hash_max_size 512;
            server_names_hash_bucket_size 512;

            # Log format
            log_format main ‘$remote_addr – $remote_user [$time_local] $request ‘
            ‘”$status” $body_bytes_sent “$http_referer” ‘
            ‘”$http_user_agent” “$http_x_forwarded_for”‘;
            log_format bytes ‘$body_bytes_sent’;
            #access_log /var/log/nginx/access.log main;
            access_log off;

            # Mime settings
            include /etc/nginx/mime.types;
            default_type application/octet-stream;

            # Compression
            gzip on;
            gzip_comp_level 9;
            gzip_min_length 512;
            gzip_buffers 8 64k;
            gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml application/x-font-ttf font/opentype;
            gzip_proxied any;

            # Proxy settings
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass_header Set-Cookie;
            proxy_connect_timeout 90;
            proxy_send_timeout 90;
            proxy_read_timeout 90;
            proxy_buffers 32 4k;

            # SSL PCI Compliance
            ssl_session_cache shared:SSL:10m;
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
            ssl_prefer_server_ciphers on;
            ssl_ciphers “ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4”;

            # Error pages
            error_page 403 /error/403.html;
            error_page 404 /error/404.html;
            error_page 502 503 504 /error/50x.html;

            # Cache
            proxy_cache_path /var/cache/nginx levels=2 keys_zone=cache:10m inactive=60m max_size=512m;
            proxy_temp_path /var/cache/nginx/temp;
            proxy_cache_key “$host$request_uri $cookie_user”;
            proxy_ignore_headers Expires Cache-Control;
            proxy_cache_use_stale error timeout invalid_header http_502;
            proxy_cache_valid any 3d;

            map $http_cookie $no_cache {
            default 0;
            ~SESS 1;
            ~wordpress_logged_in 1;
            }

            # Wildcard include
            include /etc/nginx/conf.d/*.conf;
            }

          3. server {
                listen      194.135.88.106:80;
                server_name duooglasi.com www.duooglasi.com;
                root        /home/insha/web/duooglasi.com/public_html;
                index       index.php index.html index.htm;
                access_log  /var/log/nginx/domains/duooglasi.com.log combined;
                access_log  /var/log/nginx/domains/duooglasi.com.bytes bytes;
                error_log   /var/log/nginx/domains/duooglasi.com.error.log error;
            
            
            	location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
                        expires     max;
            	}
                    
                error_page  403 /error/404.html;
                error_page  404 /error/404.html;
                error_page  500 502 503 504 /error/50x.html;
            
                location /error/ {
                    alias   /home/insha/web/duooglasi.com/document_errors/;
                }
            
                location ~* "/\.(htaccess|htpasswd)$" {
                    deny    all;
                    return  404;
                }
            	# Serve static files without touching php
              	location / {
                       try_files $uri $uri/ /index.php?$args;
              	} 
            	# pass requests for dynamic content to site
              	location ~ [^/]\.php(/|$) {
                        try_files $uri /index.php; 
                        fastcgi_pass    127.0.0.1:9003;
                        fastcgi_index   index.php;
                        include         /etc/nginx/fastcgi_params;
              	}
            }

            Use above code and save your file.
            Then run this command

            nginx -t

            If this give any error please post here.

          4. hello, i figured out, i used wordpress confu+iguration, it is little understandanle now for mw. Thank you for this help. If you know a little web design or programming please contact me

Leave a Reply