Wednesday, October 21, 2009

Deleting Files Based on Date Time

Today I needed to delete files based on date and time:
Here is the command I used for deleting file last modified on 21 Oct 2009 16:30:
ls -lh | sed -n '/Oct\ 21\ 16:30/p' | awk '{print $9}' >> 1.txt
rm -f `cat 1.txt`
Explanations: 
ls -lh (lists all files)
sed -n  '/Oct\ 21\ 16:30/p' (find files at given date from list)
awk '{print $9}' (print only file names on list) and >> 1.txt put list of files to 1.txt
rm -f (force to delete)

Wednesday, October 14, 2009

Recursively Download FTP

Sometimes, you may need to move one server to another. At that time, you may use wget command to move your files without any pain.
Here is the command for recursive download on ftp via wget:

wget -r ftp://username:password@ftp.example.com/
Yes, thats all folks!

Thursday, October 8, 2009

List of users on Linux

To list the users in linux you may use following command:

cat /etc/passwd | cut -d ":" -f1

Wednesday, September 16, 2009

NGINX, PHP-CGI, SPAWN-FCI, FTP, VHOSTS, MYSQL | centos-fedora-redhat

Setup is done under a clean Centos 5.2 32bit server

yum -y update
yum -y upgrade

PART 0
#####################################################
# start with php-5 setup and mysql setup #
#####################################################

yum -y install mysql-server mysql-client
yum -y install php-cgi php-mysql php-curl php-gd php-idn php-pear php-imagick php-imap php-mcrypt php-memcache php-mhash php-ming php-pspell php-recode php-snmp php-sqlite php-tidy php-xmlrpc php-xsl

Note: add following line to end of php ini file
cgi.fix_pathinfo = 1

PART 1
#####################################################
# NGINX SETUP START #
#####################################################

# first creating users for nginx
adduser www-data --home-dir /home/www-data
passwd www-data
--------------->enter a password

# Creating a client user for some virtual hosting
adduser mustafat --home-dir /home/www-data/mustafat -G www-data
passwd mustafat
--------------->enter a password
chown mustafat:www-data /home/www-data/mustafat
#after adding users run chmod command
chmod 0755 /home/www-data/ -R
# adding some directories for user www-data actually for nginx setup :)
mkdir /var/lib/nginx/
mkdir /var/lib/nginx/body
mkdir /var/lib/nginx/proxy
mkdir /var/lib/nginx/fastcgi
chown www-data:root /var/lib/nginx/ -R
chmod 0700 /var/lib/nginx -R


yum -y install wget which
wget http://sysoev.ru/nginx/nginx-0.8.15.tar.gz
tar zxvf nginx-0.8.15.tar.gz
cd nginx-0.8.15

yum -y install gcc make
yum -y install pcre pcre-devel
yum -y install openssl openssl-devel

./configure --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-

path=/var/lock/nginx.lock --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/body --http-proxy-temp-

path=/var/lib/nginx/proxy --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --with-debug --with-http_stub_status_module --with-http_flv_module --with-

http_ssl_module --with-http_dav_module

make
make install


#nginx setup finished
#####################################################
#####################################################

PART 2
#####################################################
# start spawn-fcgi setup #
#####################################################
wget http://www.lighttpd.net/download/spawn-fcgi-1.6.2.tar.gz
tar zxvf spawn-fcgi-1.6.2.tar.gz
cd spawn-fcgi-1.6.2
./configure
make
make install

which spawn-fcgi
#returned: /usr/local/bin/spawn-fcgi
which php-cgi
#returned: /usr/bin/php-cgi

#for help run for spawning
/usr/local/bin/spawn-fcgi --help

#so lets SPAWN it:
/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid -C 10

#add it to the start up
vi /etc/rc.local
#add the following line to the end of file
/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid -C 10


# end spawn-fcgi setup
#####################################################
#####################################################

PART 3
#####################################################
#editing default nginx conf
vi /etc/nginx/nginx.conf
#change file content with following one
user www-data;
worker_processes 5;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

access_log /var/log/nginx/access.log;
server_names_hash_bucket_size 128;
sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 2;
tcp_nodelay on;

gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain application/xml;
gzip_disable "MSIE [1-6]\.";


limit_req_zone $binary_remote_addr zone=antiddos:20m rate=3r/s;

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}


#####################################################
mkdir /etc/nginx/conf.d/
mkdir /etc/nginx/sites-enabled
#add following lines for default config you may change it whatever you like
vi /etc/nginx/sites-enabled/default
# sample config file
server {
listen 80;
server_name _; # underdash means for all hosts

access_log off;

location / {
root /home/www-data;
index index.html index.htm;
}
location /nginx_status {
stub_status on;
access_log off;
}

}
#some test
vi /home/www-data/index.html
hello world from vpslife.blogspot.com and vpswatch.com



#adding a virtual host
vi /etc/nginx/sites-enabled/vpswatch.com.conf
#sample config for vpswatch.com (virtual host) which is running now
server {
listen 80;
server_name vpswatch.com www.vpswatch.com;

access_log off;

location / {
root /home/www-data/mustafat/vpswatch.com;
index index.php index.html index.htm;

# to forward www to non-www address
if ($host ~* www\.(.*)) {
set $host_without_www $1;
rewrite ^(.*)$ http://$host_without_www$1 permanent;
}
# I added a some write for the testing purpose
rewrite ^/vps-provider/([^/]*)/$ /myserviceproviders.php?vps_provider=$1 last;

}
location ~ \.php$ {
limit_req zone=antiddos burst=5 nodelay;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www-data/mustafat/vpswatch.com$fastcgi_script_name;
fastcgi_read_timeout 180;
include fastcgi_params;
}

}

PART 4
#####################################################
# NGINX STARTUP SCRIPT FOR CENTOS/FEDORA/REDHAT #
#####################################################
vi /etc/init.d/nginx
# copy paste following script for redhat/centos/fedora


#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
start
}

reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac


#####################################################

#make it executable
chmod +x /etc/init.d/nginx
# lets start nginx
/etc/init.d/nginx start
# now try at is it working with a standard browser
# like firefox, ie, safari, opera...
# open http://yourserveripaddress/
# or try your virtual hosting in example : http://vpswatch.com/


# add it to the start up
vi /etc/rc.local
# add following line
/etc/init.d/nginx start
PART 5
#####################################################
# FTP Server with same users #
#####################################################
http://vpslife.blogspot.com/2009/09/setting-up-secure-fast-ftp-server-for.html

Reference : http://wiki.nginx.org/NginxInstall

Setting Up a secure fast ftp server for linux | VSFTP

#################################################
# setting up a ftp server #
# http://vpslife.blogspot.com/ & http://vpswatch.com/
#################################################

wget ftp://vsftpd.beasts.org/users/cevans/vsftpd-2.2.0.tar.gz
tar zxvf vsftpd-2.2.0.tar.gz
cd vsftpd-2.2.0
make
adduser nobody
adduser: user nobody exists
mkdir /var/ftp/
useradd -d /var/ftp ftp
(the next two are useful to run even if the user "ftp" already exists).
chown root.root /var/ftp
chmod og-w /var/ftp
Install vsftpd config file, executable, man page, etc.
make install
cp vsftpd.conf /etc
# open conf file and edit
vi /etc/vsftpd.conf
# set anonym ftp to no!
anonymous_enable=NO
# set local users can login and uncomment
local_enable=YES
# set write enable to yes and uncomment
write_enable=YES
#other settings
local_umask=022
connect_from_port_20=NO

chroot_list_enable=YES
chroot_list_file=/etc/vsftpd.chroot_list

##save file

#add users that you want enable ftp
vi /etc/vsftpd.chroot_list
#I added two user
www-data
mustafat
#add following lines to pam file
vi /etc/pam.d/vsftpd

# Standard behaviour for ftpd(8).

auth required pam_listfile.so item=user sense=allow file=/etc/vsftpd.chroot_list onerr=fail


# Note: vsftpd handles anonymous logins on its own. Do not enable
# pam_ftp.so.

# Standard blurb.
@include common-account
@include common-session

@include common-auth
auth required pam_shells.so
account required pam_unix.so
session required pam_unix.so
# to add to start up
vi /etc/rc.local
# add following line
/usr/local/sbin/vsftpd &

Reference : ftp://vsftpd.beasts.org/users/cevans/untar/vsftpd-2.2.0/INSTALL
Why Vsftp: Its fast and secure!

Saturday, April 4, 2009

PHP Upload Progress installation-setup

This article covers installation of PHP Upload Progress extension.
#SETUP guide is done by Mustafa TURAN(http://vpslife.blogspot.com/)#
#SETUP Software:#
#--PHP 5.2 or more#
#--Any OS#

Download
wget http://pecl.php.net/get/uploadprogress-*.tgz
Untar
tar zxvf uploadprogress-*.tgz
Go to dir
cd uploadprogress-*
Setup
phpize
./configure
make
make install
Learn php ini path
php --ini
;to ini file add these two lines:
[uploadprogress]
extension=uploadprogress.so

How to uninstall Webmin?

Just run the command
/etc/webmin/uninstall.sh

If you have installed the RPM version of Webmin, you can also use
rpm -e webmin
Or if you have installed the Solaris package you can use
pkgrm WSwebmin
Webmin FAQ: http://www.webmin.com/faq.html

How to uninstall LAMP?

Sometimes, we need to uninstall current Apache Mysql PHP collections and re install them. In this article I will show how to remove a LAMP system(for RHEL,Fedora, CENTOS):
  1. Check the existence of LAMP system:
    rpm -qa | grep -i mysql
    rpm -qa | grep -i apache
    rpm -qa | grep -i httpd
    rpm -qa | grep -i php
  2. If you get list then remove via rpm
    rpm -e "file name"
  3. or with "yum -y uninstall"
    yum remove "file name"
Thats all :))

Tuesday, March 17, 2009

Mysql Nested Query Tweak

Do not use mysql nested queries for performance. Here I will share a little database table and two queries and their response times:
#Experiment is done by Mustafa TURAN(http://vpslife.blogspot.com/)#
#Experiment purpose to see how a nested query affects MYSQL Performance #
#Experiment Hardware:#
#--RAM: 1.5GB#
#--CPU: AMD Athlon(tm) 64 X2 Dual Core Processor 4000#
#Experiment Software:#
#--MYSQL 5(MYISAM)#
#--Centos5.2#
#Notes: 1k=1000#
Experiment tables
Table #1: links

database table : links(l_id, l_source, l_status, l_kb)

total rows: 220k

Table #2: filelinks

database table : filelinks(f_id, l_id)

total rows: 1500k

Nested query

Query: SELECT l_id, l_source, l_status, l_kb FROM links WHERE links.l_id IN (SELECT l_id FROM filelinks WHERE filelinks.f_id = 1);

Response: Showing rows 0 - 0 (1 total, Query took 2.9740 sec)


Inner Join(same query with inner join method)

Query: SELECT distinct(l_id), l_source, l_status, l_kb FROM links INNER JOIN (SELECT l_id as xL_id FROM filelinks WHERE filelinks.f_id = 1) xLinks ON links.l_id =
xLinks.xL_id;

Response: Showing rows 0 - 0 (1 total, Query took 0.0003 sec)

This example make you to see how dangerous using nested queries. I hope this experiment will save your CPU usage by MYSQL!

My detailed /etc/my.cnf file:

#Mustafa Turan (http://vpslife.blogspot.com/)#

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
port = 3306
skip-locking
key_buffer = 256M
max_allowed_packet = 1M
table_cache = 2M
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 128
query_cache_size= 64M
thread_concurrency = 4
thread_stack = 1M
max_connections = 250
skip-bdb
skip-innodb


[mysqldump]
quick
max_allowed_packet = 32M


[mysql]
no-auto-rehash
#safe-updates


[isamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 16M
write_buffer = 16M


[myisamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 16M
write_buffer = 16M


[mysqlhotcopy]
interactive-timeout


[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

Monday, March 16, 2009

How to learn linux system(hardware) properties?

To learn system(hardware,sofware) properties linux has such commands:
Operating System Version:
uname -r
Memory Information:
cat /proc/meminfo
CPU Information:
cat /proc/cpuinfo
Easy Way(smart info):
uname -rs;less /proc/meminfo grep MemTotal;less /proc/cpuinfo grep
"model name"

Aktive System Resource Usages:
(all users and all activity)
top -s

(specified user)
top -u username

Sunday, March 15, 2009

Change Hostname on Centos 5x

On terminal screen apply the commands:
vi /etc/sysconfig/network

Then change HOSTNAME=xxxx.xxx line to HOSTNAME=your_desired_hostname
You have to save the data and quit(thats all!):

wq!

Tuesday, March 10, 2009

Copy Remote Database to Local Database

Remote mysqldump

Run this one line command as a user:


mysqldump --opt --compress --user=REMOTE_DB_USERNAME --password=REMOTE_DB_PASS --host=REMOTE_DB_HOSTNAME REMOTE_DB_NAME mysql --user=LOCAL_DB_USERNAME --password=LOCAL_DB_PASSWORD --host=LOCAL_DB_HOSTNAME -D LOCAL_DB_NAME -C LOCAL_DB_NAME

Notes: You have to enable remote mysql on remote computer! (To do this you have to add ip address of local computer to remote computer's db remote access ip list)