Monthly Archives: January 2014

PHP Script to download multiple files concurrently

This script will show you an example of how to download multiple files concurrently
using the curl_multi commands in PHP 5.While php itself doesn’t support multi threading and concurrency,
libcurl does and php allows us to download multiple files at the same time from php.

 Script Author – JeenaJoy
<?php//This script will show you an example of how to download multiple files concurrently
//using the curl_multi commands in PHP 5.While php itself doesn’t support multi threading and concurrency,
//libcurl does and php allows us to download multiple files at the same time from php.// Files to download
$urls = array(‘http://abc.com/docs/cdbwpohq0ayey.pdf’,
‘http://abc.com/docs/8wyxlxfufftas.pdf’,
‘http://abc.com/docs/9q29bbglnc2gk.pdf’,);

$save_to=’/tmp/';  // Path to save files in

$mh = curl_multi_init();
// Add curl multi handles
foreach ($urls as $i => $url) {

// Path to save files in
$g=$save_to.basename($url);

$conn[$i]=curl_init($url);
$fp[$i]=fopen ($g, “wb”);

curl_setopt ($conn[$i], CURLOPT_HEADER ,0);///it should come first
curl_setopt ($conn[$i], CURLOPT_FILE, $fp[$i]);
curl_multi_add_handle ($mh,$conn[$i]);

}
// Download the files
do {
$n=curl_multi_exec($mh,$active);
}
while ($active);
foreach ($urls as $i => $url)
{
curl_multi_remove_handle($mh,$conn[$i]);

curl_close($conn[$i]);
fclose ($fp[$i]);

}
curl_multi_close($mh);

?>

Adding SSL to a site in nginx

1) First create a folder where SSL certs are to be placed

#mkdir /etc/ssl/certs

#cd /etc/ssl/certs

Create key

# openssl genrsa -out abc.com.key 2048

# chmod 600 abc.com.key

Generate CSR

# openssl req -new -key abc.com.key -out abc.com.csr

If all that you need is a self signed certificated, do as follows.

# openssl x509 -req -days 365 -in abc.com.csr -signkey abc.com.key -out abc.com.crt

Otherwise get the cert signed and place it in /etc/ssl/certs/abc.com.crt

Now, open up your nginx config file for this domain.

# vi /etc/nginx/sites-available/abc.com

If you are planning to use both http and https, you need two separate server modules in your config. Hence, copy the config for 80 and put it to the bottom of the page and make modifications. What you need to add to the https server module is given below. Paste it before server_name line.

server {

listen   1.2.1.3:443 ssl;

ssl    on;
ssl_certificate    /etc/ssl/certs/abc.com.crt;
ssl_certificate_key    /etc/ssl/certs/abc.com.key;
server_name abc.com www.abc.com

….

}

Now restart nginx.

#/etc/rc.d/init.d/nginx restart

Now try accessing site with https.

Password protecting a site in nginx

This post will explain how to password protect a site in nginx. It is very simple and it uses the normal htpasswd protection

Suppose I need to password protect the site abc.com whose document root is /var/www/abc/public_html

Simple follow the steps given below

[root@abc ~]#  cd /var/www/abc/public_html

[root@abc ~]#  htpasswd -c .htpasswd admin        where admin is the username chosen. It will prompt for password, give a strong password.

Now, your login details are stored to the file /var/www/abc/public_html/.htpasswd and we need to call this file from abc.com’s conf.

Simply add the following two lines before the locations

[root@abc ~]# cd /etc/nginx/sites-available/

[root@abc ~]# vi abc.com

auth_basic “Restricted”;
auth_basic_user_file /var/www/abc/public_html/.htpasswd;

 

So your final conf will now look as follows.

server {
server_name www.abc.com abc.com;
access_log /var/www/abc/logs/access.log;
error_log /var/www/abc/logs/error.log;
root /var/www/abc/public_html/;

auth_basic “Restricted”;
auth_basic_user_file /var/www/abc/public_html/.htpasswd;

location / {

index index.html index.htm index.php;

}

location ~ \.php$ {
if (!-e $request_filename) { rewrite / /index.php last; }
include /etc/nginx/fastcgi_params;
fastcgi_pass  127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME  /var/www/abc/public_html$fastcgi_script_name;
fastcgi_param MAGE_RUN_CODE base;
fastcgi_param MAGE_RUN_TYPE website;

}
}

All done, save the configuration file and exit. Now restart nginx and see if it starts without errors.

[root@abc ~]# /etc/rc.d/init.d/nginx restart
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]

Now clear your browser cache and try accessing the site. Bingo!! isnt’t it protected? :-)