Using top Command

Enter top -S to display resource use information using “cumulative” mode (the -S flag will report CPU time used by processes and their children since top -S was started). Your top output will resemble the following:

=======

top - 03:31:05 up  7:40,  1 user,  load average: 0.00, 0.00, 0.00
Tasks:  15 total,   1 running,  14 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.1% us,  0.0% sy,  0.0% ni, 99.9% id,  0.0% wa,  0.0% hi,  0.0% si
Mem:    512000k total,   115552k used,   396448k free,        0k buffers
Swap:        0k total,        0k used,        0k free,        0k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
    1 root      15   0  1516  520  452 S  0.0  0.1   0:05.28 init
 1559 root      15   0  1564  536  440 S  0.0  0.1   0:00.03 syslogd
 1579 root      18   0  2512 1228 1016 S  0.0  0.2   0:00.01 mysqld_safe
 1598 root      18   0  2512  560  344 S  0.0  0.1   0:00.00 mysqld_safe
 1599 mysql     18   0 97960  16m 3100 S  0.0  3.2   0:02.06 mysqld
 1600 root      21   0  1496  504  440 S  0.0  0.1   0:00.00 logger
 1673 daemon    18   0  1696  368  280 S  0.0  0.1   0:00.00 atd
 1676 root      15   0  1776  748  604 S  0.0  0.1   0:02.16 cron
 1683 root      18   0 10516 4216 3184 S  0.0  0.8   0:00.21 apache
 1696 www-data  15   0 10520 1988  896 S  0.0  0.4   0:00.00 apache
 1697 www-data  15   0 10520 1960  868 S  0.0  0.4   0:00.00 apache
11830 root      18   0  3472  896  660 S  0.0  0.2   0:03.91 sshd
28133 root      15   0 14456 1980 1456 S  0.0  0.4   0:00.19 sshd
28166 root      15   0  2568 1408 1128 S  0.0  0.3   0:00.68 bash
21716 root      15   0  2072 1028  836 R  0.0  0.2   0:00.04 top
=======

Column Names

Commonly-used columns:

  1. PID – Process ID – The unique ID of the process (commonly used with the kill command)
  2. USER – Username which the process is running under
  3. PR – Priority for the process (ranges from -20 for very important processes to 19 for unimportant processes)
  4. NI – Nice value modifies the priority of the process (a negative value will increase the priority of the process and a positive value will decrease the priority of the process)
  5. VIRT – Total amount of virtual memory used by the process
  6. RES – Resident size (kb) – Non-swapped physical memory which the process has used
  7. SHR – Shared memory size (kb) – Amount of shared memory which the process has used (shared memory is memory which could be allocated to other processes)
  8. S – Process status – Possible values:
    • R – Running
    • D – Sleeping (may not be interrupted)
    • S – Sleeping (may be interrupted)
    • T – Traced or stopped
    • Z – Zombie or “hung” process
  9. %CPU – Percentage of CPU time the process was using at the time top last updated
  10. %MEM – Percentage of memory (RAM) the process was using at the time top last updated
  11. TIME+ – Cumulative CPU time which the process and children of the process have used
  12. COMMAND – Name of the process or the path to the command used to start the process (press c to toggle between the name of the process and the path to the command used to start the process)

Apache Optimization : Parameters

 

Apache Optimization

The default Apache settings that cPanel sets upon install are definitely something that can be improved on. With a few small tweaks, the efficiency with which Apache runs with can be greatly improved.

Please noted: This article assumes that you are using a Linux server running Apache and cPanel or Plesk, and that you are familiar with editing files from the command line.

Please note, the settings that we will review in this article are by no means a complete list of tweakable options in the Apache configuration file. The settings we will be focusing on are the ones that control how Apache handles webpage requests.

Timeout

This parameter tells Apache how many seconds to wait while receiving an incoming request,processing it, and sending back a response.

Timeout 300

Usually this value doesn’t require editing and a default of 300 is sufficient. Lowering the ‘Timeout’ value will cause a long running script to terminate earlier than expected.

On virtualized servers like VPS servers, lowering this value to 100 can help improve performance.

KeepAlive

KeepAlive is an apache parameter to allow multiple requests over the same TCP connection. If KeepAlive is turned On the complete contents of the web page will be sent over one single connection. If this feature is turned Off , Apache has to make new connections for each requests.

KeepAlive On

This setting should be “On” unless the server is getting requests from hundreds of IPs at once.

High volume and/or load balanced servers should have this setting disabled (Off) to increase connection throughput.

MaxKeepAliveRequests

This parameter limits the number of requests a client can issue across a persistent connection.

higher the value allows more requests per connection and increases efficiency.

MaxKeepAliveRequests 100

This setting limits the number of requests allowed per persistent connection when KeepAlive is on. If it is set to 0, unlimited requests will be allowed.

It is recommended to keep this value at 100 for virtualized accounts like VPS accounts. On dedicated servers it is recommended that this value be modified to 150.

KeepAliveTimeout

This parameter defines how long Apache will wait before closing the connection in seconds.

KeepAliveTimeout 15

The number of seconds Apache will wait for another request before closing the connection. Setting this to a high value may cause performance problems in heavily loaded servers. The higher the timeout, the more server processes will be kept occupied waiting on connections with idle clients.

It is recommended that this value be lowered to 5 on all servers.

MinSpareServers

MinSpareServers 5

MinSpareServers sets the minimum number of idle child server processes. A process which does not handle any request is called an idle process.

This directive sets the desired minimum number of idle child server processes. An idle process is one which is not handling a request. If there are fewer spareservers idle then specified by this value, then the parent process creates new children at a maximum rate of 1 per second. Setting this parameter to a large number is almost always a bad idea.

Liquidweb recommends adjusting the value for this setting to the following:

Virtualized server, ie VPS 5

Dedicated server with 1-2GB RAM 10

Dedicated server with 2-4GB RAM 20

Dedicated server with 4+ GB RAM 25

MaxSpareServers

MaxSpareServers 10

MaxSpareServers sets the maximum number of idle child server process in the server. The parent process will kill the excess process if the number of idle process is greater than this value.

The MaxSpareServers directive sets the desired maximum number of idle child server processes. An idle process is one which is not handling a request. If there are more than MaxSpareServers idle, then the parent process will kill off the excess processes.

The MaxSpareServers value should be set as double the value that is set in MinSpareServers.

StartServers

when apache is started this parameter determines the number of apache child process.

StartServers 5

This directivesets the number of child server processes created on startup. This value should mirror what is set in MinSpareServers.

MaxClients

This parameter decides the maximum number of simultaneous requests handled by apache. If any connection attempt over the specified limit will be queued and once a process is freed the queued connection will then be processed.

MaxClients 150

This directive sets the limit on the number of simultaneous requests that will be served. Any connection attempts over the specified limit will be queued. Once a process is freed at the end of a different request, the queued connection will then be served.

For virtualized servers such as VPS accounts, it is recommended to keep this value at 150. For all dedicated servers the recommended value for this setting is 250.

MaxRequestsPerChild

This parameter will limit the number of requests that an individual child server process will handle.

MaxRequestsPerChild 0

This directive sets the limit on the number of requests that an individual child server process will handle. After the number of requests reaches the value specified, the child process will die. When this value is set at 0, then the process will never expire.

Liquidweb recommends adjusting the value for this setting to the following:

Virtualized server, ie VPS 300

Dedicated server with 1-4GB RAM 500

Dedicated server with 4+GB RAM 1000

 

MySQL basic commands

MySQL
—–
create database test;

create user test@localhost identified by “password”;

grant all on test.* to test@localhost;

flush privileages;

use test; database changed.

create table students (id INT, name VARCHAR(20), email VARCHAR (20) age INT));

insert into students values (1, “Ranjith”, “ranjith@gmail.com” 25);

Set the root password via command line
—–
set password for root@localhost=password(‘mathappally’);

MySQL commands used for wordpress
—–
To check the current theme

select * from wp_options where option_name=’template’;
or
select * from wp_options where option_name=’stylesheet’;
or
select * from wp_options where option_name=’current_theme’;

How to Change WordPress Themes Directly From the Database ?

We can simply change the theme via PhpMyadmin.

1) Check the current theme using above mentioned steps.

2) Now it’s time to change the theme. Figure out which one you’d like to switch to and change the three option_values = ”  below.

3)  update wp_options set option_value=’twentyfifteen’ where option_name=’template’;
update wp_options set option_value=’twentyfifteen’ where option_name=’stylesheet’;
update wp_options set option_value=’twentyfifteen’ where option_name=’current_theme’;

More information: https://docs.appthemes.com/tutorials/how-to-change-wordpress-themes-directly-from-the-database    /
http://designgala.com/how-to-change-wordpress-theme-directly-from-database/

Install LAMP via source code (Using latest versions)

I) Installation of Apache
1) Install the necessary modules
yum install gcc gcc-c++ autoconf automake -y
yum install pcre-devel -y
2) Login to server
cd /usr/local/src/
tar -zxvf httpd-2.4.18.tar.gz
Before installing apache, we need to install APR and APR-Util
cd httpd-2.4.18/srclib/
extract and rename to apr,apr-util
cd ..
Create a folder /usr/local/src/apache/
./configure –prefix=/usr/local/src/apache/ –with-included-apr –with-included-pcre –enable-so –enable-mods-shared=”most”
We get an error regarding pcre and need to install the same via source code or yum
—-
cd /usr/local/src/
create a folder /usr/local/src/pcre
./configure –prefix=/usr/local/src/pcre
make & make install
—-
Then continue to install apache.
./configure –prefix=/usr/local/src/apache/ –with-included-apr –with-included-pcre –enable-so –enable-mods-shared=”most”
make & make install
Reference:
II) MySQL Installation
—–
yum install cmake cmake-gui
yum install ncurses-devel -y
1) Download latest MySQL version: https://dev.mysql.com/downloads/mysql/ >> select source code >> download Generic Linux (Architecture Independent), Compressed TAR Archive
cd /usr/local/src/
tar zxvf mysql-5.7.10.tar.gz
cd mysql-5.7.10
Check vi INSTALL-SOURCE for the installation steps
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
mkdir /usr/local/src/mysql/
while running the cmake command we get the error, boost is need to be installed, so that ran the following command,
cmake  -DCMAKE_INSTALL_PREFIX=/usr/local/src/mysql -DMYSQL_DATADIR=/usr/local/src/mysql/data -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/src/boost/
make
makeinstall
=======
# Postinstallation setup
shell> cd /usr/local/mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> bin/mysql_install_db –user=mysql    # Before MySQL 5.7.6 >> no need to run this
shell> bin/mysqld –initialize –user=mysql # MySQL 5.7.6 and up >> root password will generate, note it.
(When you are getting error like data direcroty and time stamp run bin/mysqld –initialize –user=mysql –basedir=/usr/local/src/mysql/  –datadir=/usr/local/src/mysql/data/ –explicit_defaults_for_timestamp)
shell> bin/mysql_ssl_rsa_setup              # MySQL 5.7.6 and up >> no need to run this
shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe –user=mysql &
# Next command is optional
or
cp /usr/local/src/mysql/support-files/mysql.server /etc/init.d/mysqld
cp -pr bin/mysql /sbin/
=======
Referrence :
III) PHP installation
cd /usr/local/src/
tar -zxvf php-7.0.1.tar.gz
cd php-7.0.1
mkdir /usr/local/src/php/
./configure –prefix=/usr/local/src/php –with-apxs2=/usr/local/src/apache/bin/apxs –with-mysql=/usr/local/src/mysql/  –with-mysqli
or
./configure –prefix=/usr/local/src/php  –with-apxs2=/usr/local/src/apache/bin/apxs –enable-bcmath –enable-calendar –enable-ftp –with-mysqli –enable-mbstring –with-curl –with-mhash –without-pear –enable-sockets –with-zlib –with-libdir=lib64 –with-kerberos –with-openssl –with-pdo-mysql –with-pear
Please note that ./configure not support after php version 5.6 or something doesnot support mysql, support mysqli, so that we will get the error while unning make, we need to include mysqli option with ./configure.
make
make test
make install
After completing the LAMP configuration we can create a index.htm file in apache document root.
When we create a index.php, it doesent work, so that we have to add the php handler to apache configuration file.
Also add index.php in the line in httpd.conf >> DirectoryIndex index.html index.php
Add granted in
<Directory “/usr/local/src/apache//cgi-bin”>
    AllowOverride None
    Options None
    Require all granted
</Directory>
Also add
AddType text/html .php
AddType application/x-httpd-php .php
==============================================

Sending mail to admin when Internal server error occurs.

#!/bin/bash
curl -I toji.ga/info.php > ranji.txt
grep HTTP ranji.txt | awk ‘{print $2}’
if [ `grep HTTP ranji.txt | awk ‘{print $2}’` = 500 ]; then
echo “Internal server error found on toji.ga/info.php”|mail -s “Internal server Error” -r “toji@tojikdominic.com” “toji@tojikdominic.com”;
else exit;
fi

Installation of SSL on the Linux server with Nginix & Postfix

Installation of SSL on the Linux server with Nginix & Postfix
—————————————————————-
For simply, we just create a private key and csr on the server and purchase the ssl, tell the ssl path to configuration file.

Detailed steps:

1) Login to the server. I have installed ssl in Ubuntu 14.04.3 LTS \n \l

2) sudo -i login as root.

3) Change the directory. cd /usr/local/src/

4) Generate both private key and csr using the following commands.

For Private key generation :
openssl genrsa -out http://www.triplepeople.com.key 2048

For csr generation using the above private key.
openssl req -new -key http://www.triplepeople.com.key -out http://www.triplepeople.com.csr

5) Using the csr we need to purchase an SSL certificate.

6) For purchasing the SSL we need an admin email address.
Since it is a postfix server.

Create a user named admin.
useradd admin (if it is not workig use  useradd -md /home/admin/ admin)
Need to make some changes in the postfix configuration file, so that mail send to admin@triplepeople.com will reach the /home/admin/Maildir folder.
—-
vi /etc.postfix/main.cf
myhostname = servertriplepeoplecom.
mydestination = triplepeople.com, localhost.localadmin, , localhost
home_mailbox = Maildir/
—-

Restart postfix—> /etc/init.d/postfix restart

Test this by sending an email address from serverhelp247@gmail.com to admin@triplepeople.com, can see the mail on /home/admin/Maildir/new/

7) Go to enom purchase the SSL certificate using the email address admin@triplepeople.com
Security –> SSL Certificates  –> GeoTrust SSL certificate –> Rapissl.

8) We will get the verfication mail and SSL certificate to the above specified mail.

9) Create a file in /usr/local/src/
vi http://www.triplepeople.com.crt, copy the certificate and save.

10) The domain ‘www.triplepeople.com’ is hosted in this server. The nginix configuration file of the domain is, the root path ssl configurations to be added this file.
vi /etc/nginx/sites-available/www.triplepeople.com. (Document root specified here)

Private kaey and SSL certificates are moving to the following path.
/etc/ssl/private/www.triplepeople.com.key
/etc/ssl/certs/www.triplepeople.com.crt >> give full permission 777

And make the necessary changes in the configuration file.
see the ssl installed nginix configuration file.(/etc/nginx/sitesavailable/www.triplepeople.com)


server {
#listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
ssl on;
listen 443 default_server ssl;
# listen [::]:443 ipv6only=on default_server ssl;

ssl_certificate /etc/ssl/certs/www.triplepeople.com.crt;
ssl_certificate_key /etc/ssl/private/www.triplepeople.com.key;

root /usr/share/nginx/www.triplepeople.com/html;
index index.php index.html index.htm;

Restart the nginix –> /etc/init.d/nginx restart

SSL is installed, we check this in https://www.sslshopper.com/ssl-checker.html, if the chain showing red.
then from the mail with SSL get a link,
You can get your RapidSSL Intermediate Certificates at:
https://knowledge.rapidssl.com/support/ssl-certificate-support/index?page=content&id=AR1548

Copy paste it to crt file and check in ssl checker and if red is there try with other bundle.

—————————————————————————–

Install VARNISH

Working of Varnish Cache

Once Varnish is installed in your server, it will listen in the port 80. Make Apache to listen in any other port other than 80. When a client requests for a page in your website, Varnish will try to serve the page from its cache memory. This is cache hit. If the page is not in its cache memory, then Varnish will request to HTTP (Apache) to serve the page and caches it for future reference.

Features of Varnish

1. Reduces the server load
2. High speed (cache + stored in RAM)
3. Useful to gain more visitors

Installation
1. Make Apache listen in any other port other than 80. Here I am making Apache to listen in port 8080. It can be done in two ways. Either editing the httpd.conf file or editing the Tweak settings in WHM
   a) Editing httpd.conf file
        # vi /usr/local/apache/conf/httpd.conf
     Locate the following line in /usr/local/apache/conf/httpd.conf file and change the port from 80 to 8080
     Listen 0.0.0.0:80

OR

b) Editing WHM – Tweak settings

WHM > Tweak Settings > Apache non-SSL IP/port  Set the port to 8080 and save the settings.

2. Install Varnish repository

Epel for varnish 4:

wget http://repo.varnish-cache.org/redhat/varnish-4.0/el6/noarch/varnish-release/varnish-release-4.0-4.el6.noarch.rpm

rpm -Uvh varnish-release-4.0-4.el6.noarch.rpm
3. Install Varnish
# yum install varnish

4. Edit the configuration file for varnish /etc/sysconfig/varnish 
and change the value of VARNISH_LISTEN_PORT to 80
# grep VARNISH_LISTEN_PORT /etc/sysconfig/varnish
VARNISH_LISTEN_PORT=80

5. Edit Varnish config: /etc/varnish/default.vcl . Correct the IP address in this file to your IP address and port to 80

backend default {
  .host = "YOUR IP ADDRESS";
  .port = "80";}
6. Start Varnish service on your server
# chkconfig varnish on
# service varnish start

Ref : http://crybit.com/how-to-enable-varnish-in-cpanel-server/

Create an Automatic cPanel account Backup

Here we create a script which hepls to automatic backup of a cPanel account in user home directory and will be deleted the backup older than 3 days.

==
#!/bin/bash
tar -cvf /home/shimji/backups/wordpress_file_`date +%Y_%m_%d`.tar.gz /home/shimji/public_html
mysqldump wordpress > /home/shimji/backups/wordpress_db_`date +%Y_%m_%d`.sql
mkdir -p /home/shimji/backups/`date +%Y_%m_%d`
mv /home/shimji/backups/wordpress_file_`date +%Y_%m_%d`.tar.gz  /home/shimji/backups/`date +%Y_%m_%d`/
mv /home/shimji/backups/wordpress_db_`date +%Y_%m_%d`.sql /home/shimji/backups/`date +%Y_%m_%d`/
find /home/shimji/backups/ -type d -mtime +3 -exec rm {} \;
==

For more details : http://coolestguidesontheplanet.com/backup-website-cpanel-manual-automatically-script-cron/

 

Enabling ping in server

Ping from outside to inside:
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
Ping from inside to outside:
iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT

Reference: http://www.thegeekstuff.com/scripts/iptables-rules
http://tecadmin.net/block-ping-responses-in-linux/#

 

Enable 2 ports to FTP

Enable both ports on ftp : connect via 21 and 2121
If you are using pureftpd, open the configuration file and add the port and open in firewall.
add the port in configuration file eg :  vi /etc/pure-ftpd.conf
Then run the below command:
iptables -t nat -A PREROUTING -p tcp -m tcp –dport 21 -j REDIRECT –to-ports 2121
 
—————-