Enable Keep Alive for faster web speed Apache and Nginx

There are 2 ways to enable Keep Alive On at Apache server, with .htaccess and httpd.conf

Enable keep-alive On at Apache with httpd.conf

Keep Alive Default configuration is Off, we must change it to On

vi /etc/httpd/conf/httpd.conf

Find KeepAlive sentence and edit Off to On

.....................................
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
# KeepAlive Off
KeepAlive On
......................................

Restart Apache to effect the changes

#vi service httpd restart

Enable keep-alive on Apache server with .htaccess

This is alternative if we do not have access to server. Add code below to .htaccess

<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>

Enable keep-alive On Nginx with nginx.conf

By default NGINX supports keep-alive, add this line below to nginx configuration

#vi etc/nginx/nginx.conf
# Keep Alive
keepalive_timeout 65;
keepalive_requests 100000;
sendfile on;
tcp_nopush on;
tcp_nodelay on;

Install MariaDB Server on CentOS 6

Most people said MariaDB better then old MySQL Server, so i decided to migrate Database Server from old MySQL to new MariaDB Server

For brief info MariaDB is a community-developed fork of the MySQL relational database management system intended to remain free under the GNU GPL. It is notable for being led by the original developers of MySQL, who forked it due to concerns over its acquisition by Oracle.

For CentOS system highly recommended to install MariaDB from a repository using yum.
First step make sure list of available CentOS packages is up-to-date before installing by input following commands.

#yum -y update

Then add the MariaDB repository

#vi /etc/yum.repos.d/MariaDB.repo

Insert this custom MariaDB 10.1 Stable YUM repository for CentOS 6 (64 Bit).

# MariaDB 10.1 CentOS repository list - created 2016-07-03 19:01 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

For complete custom MariaDB repository with linux system version please visit

https://downloads.mariadb.org/mariadb/repositories/

Install MariaDB

#yum install MariaDB-server MariaDB-client -y

Start MariaDB and set to start on every boot.

#service mysql start
#chkconfig mysql on

Secure MariaDB, Secure your MariaDB installation. The following checklist shows the steps that will be performed.
Set (Change) root password.
Remove anonymous users.
Disallow root login remotely.
Remove test database and access to it.
Reload privilege tables.
Run the secure installation command.

#mysql_secure_installation
Example output

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password: ENTER YOUR PASSWORD
Re-enter new password: REPEAT YOUR PASSWORD
Password updated successfully!
Reloading privilege tables..
... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
... Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Restart MariaDB.

#service mysql restart

Install Configure VSFTPD on CentOS 6

First please make sure to update CentOS 6 systems with the current files with command yum -y update

#yum -y update
Loaded plugins: fastestmirror
Setting up Update Process
base | 3.7 kB 00:00
base/primary_db | 4.7 MB 00:04

Then move forward to install VSFTPD with any required packages with yum -y install vsftpd

#yum -y install vsftpd
Loaded plugins: fastestmirror
Setting up Install Process
Determining fastest mirrors
* base: mirror.ventraip.net.au
* extras: mirror.ventraip.net.au
* updates: mirror.ventraip.net.au
Resolving Dependencies
--> Running transaction check
---> Package vsftpd.x86_64 0:2.2.2-21.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved

After VSFTPD installation has been complete, now edit and configure vsftpd.conf file

#vi /etc/vsftpd/vsftpd.conf
# Allow anonymous FTP (Beware - allowed by default if you comment this out).
anonymous_enable=NO
#
# Uncomment this to allow local users to log in.
local_enable=YES
#
# Uncomment this to enable any form of FTP write command.
write_enable=YES
#
# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
chroot_local_user=YES
#chroot_list_enable=YES
# (default follows)
chroot_list_file=/etc/vsftpd/chroot_list

Exit and save the file with command :wq
Then restart the vsftpd service with command service vsftpd restart

#service vsftpd restart

Set the vsftpd service to start at boot with command chkconfig vsftpd on

#chkconfig vsftpd on

Add VSFTPD user

# useradd user
# passwd user
Changing password for user user.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

You will need to create a vsftp.chroot_list file and enter users who do not use chroot. Ever user chroots by default. Therefore, create a chroot_list file, even if the file is going to remain empty:

#touch /etc/vsftpd/vsftpd.chroot_list

Once the vsftpd.chroot_list file has been created, restart vsftpd again

#service vsftpd restart

Test VSFTPD user log in to FTP Server with ftp client application, this example using FileZilla

Install WordPress on Nginx CentOS Server

Before to install Wordpress online on Nginx CentOS Server, we should verify that NGINX MySQL PHP (LEMP) has been installed and all of the LEMP services are started and running.

The most important thing is Nginx WordPress configuration for domain.tld. Assume domain.ltd has been configured as virtual host on our server, edit virtual.conf and insert configuration below:

#vi /etc/nginx/conf.d/virtual.conf
# Upstream to abstract backend connection(s) for php
upstream php {
server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9000;
}

server {
listen 80;
## Your website name goes here (servername example.com www.example.com).
server_name domain.tld;
## Your only path reference.
root /var/www/example.com/public_html;
## This should be in your http block and if it is, it's not needed here.
index index.php;

location = /favicon.ico {
log_not_found off;
access_log off;
}

location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}

location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
#Fix Yoast SEO Sitemaps
rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
}

Save and Exit then Restart nginx and php-fpm service again so that all of the changes take effect:

#service nginx restart
#service php-fpm restart

Step one: Download WordPress directly from their website.

Make sure current directory is /var/www/example.com/public_html

#cd /var/www/example.com/public_html
#wget http://wordpress.org/latest.tar.gz

Unzip it the the next line:

#tar -xzvf latest.tar.gz

After we unzip the wordpress files, they will be in a directory called wordpress in the current directory.
Copy all files and folder on that wordpress directory to /var/www/example.com/public_html

#cp -R wordpress/* /var/www/example.com/public_html

We will need to create an /upload directory on /var/www/wp-content/ folder so we can make contents and upload files into it.

#mkdir -p /var/www/wp-content/uploads

Grant Ownership Permissions of directory to user

#chown -R user:nginx /var/www/example.com/public_html

Step Two: Create the WordPress Database and User.

#mysql -u root -p
mysql>CREATE DATABASE wpdb;
Query OK, 1 row affected (0.00 sec)

Then we need to create the new user. You can replace the database, name, and password

mysql>CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)

Finish up by granting all privileges to the new user.

mysql>GRANT ALL PRIVILEGES ON wpdb.* TO 'wpuser'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

Then refresh MySQL:

mysql>FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

Exit out of the MySQL shell:

exit

Step Three Setup the WordPress Configuration
The first step to is to copy the sample wordpress configuration file (wp-config-sample.php) into a new file wp-config.php which we will edit,  assume current working directory is /var/www/example.com/public_html;

#cd /var/www/example.com/public_html
#cp wp-config-sample.php wp-config.php

Then open the wordpress config:

#vi wp-config.php

Find the section that contains the field below and substitute in the correct name for your database, username, and password:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wpdb');

/** MySQL database username */
define('DB_USER', 'wpuser');

/** MySQL database password */
define('DB_PASSWORD', 'password');
Save and Exit.

 

From here, WordPress has its own easy to follow installation form online. However, the form does require a specific php module to run. If it is not yet installed on your server, download php-gd:

#yum install php-gd

Restart nginx and php-fpm service again so that all of the changes take effect:

#service httpd restart

Step Five RESULTS Access the WordPress Installation
Access the page by adding /wp-admin/install.php to your site’s domain or IP address (eg. example.com/wp-admin/install.php) and fill out the short online form

Add new User Group on CentOS 6 terminal

To add a New User and Assign a Group on CentOS 6 terminal

#useradd -g <groupname> username

For instance, lets say you wanted to add a new user named user to the apache group:

#useradd -G apache user

And then you’ll want to assign a password for that user, of course:

passwd user

Add or update User to an an Group

#usermod -a -G <groupname> username

Change a User Primary Group

Sometimes a user has many group, this is switch out the primary group that a user is assigned to with this command:

#usermod -g <groupname> username

View a User Group Assignments by use the id command to see what groups the user is assigned to:

id <username>

This will display output something like this:

uid=500(howtogeek) gid=500(howtogeek) groups=500(howtogeek), 1093(admins)

 

Import Export MySQL Dumpfile MySQL Server on Terminal

This is an alternative way to restore or import MySQL Dumpfile data with mysql command on Terminal.
Best tool to restore or import MySQL Dumpfile is phpMyAdmin, here is the tutorial to Set up Apache phpMyadmin and Nginx phpMyAdmin

This is restore or import dbuser.sql dumpfile into dbuser MySQL database using user as username:

First we upload MySQL dumpfile to terminal or home folder of user
Easy simple way by log in via SFTP (SSH File Transfer Protocol), this is same way a  user log via ssh. We can use Bitvise, Filezilla, etc.

Assume database ‘dbuser’ has been created and MySQL dumpfile (dbuser.sql) has been uploaded to user home folder, user log in to terminal and in home directory position.

#ls -la
dbuser.sql

Import: To MySQL dump data file to MySQL Server type the following command:

#pwd
/home/user
#mysql -u user -p -h localhost dbuser < dbuser.sql

Export: To Export a database and create dump file from MySQL Server type the following command:

#mysqldump -u [username] -p [database name] > [database name].sql

Important to avoid Mysql::Error: Incorrect string value: ‘\xE2\x80\xA8\x09 while inserting data to the table

#mysql -u root
MariaDB [(none)]>use dbname;
MariaDB [(none)]>ALTER TABLE dbname.tablename CONVERT TO CHARACTER SET utf8;

Install phpMyadmin Nginx CentOS 6

Prior to installing phpMyAdmin, we should verify that NGINX MySQL PHP (LEMP) has been installed and all of the LEMP services are started and running.  We can grep look for their processes – “nginx”, “mysqld” and “php-fpm”:

#ps auxf | egrep "nginx|mysqld|php-fpm"

If you see processes similar to the ones below, you may proceed with the phpMyAdmin installation, otherwise you need to get back to the LEMP setup tutorial:

[root@vpshelpdesk phpMyAdmin]# ps auxf | egrep "nginx|mysqld|php-fpm"
root 1171 0.0 0.2 2340 588 pts/0 S+ 15:01 0:00 | \_ gr ep -E nginx|mysqld|php-fpm
root 574 0.0 0.5 3044 1324 ? S 14:41 0:00 /bin/sh /usr/bin/mysqld_sa fe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/m ysqld.pid --basedir=/usr --user=mysql

Install phpMyadmin

#wget https://files.phpmyadmin.net/phpMyAdmin/4.1.7/phpMyAdmin-4.1.7-all-languages.zip
#unzip phpMyAdmin-4.1.7-all-languages.zip
#mv phpMyAdmin-4.1.7-all-languages.zip phpMyAdmin
#mv phpMyAdmin /usr/share/nginx/html
#cd /usr/share/nginx/html/phpMyAdmin
#service nginx restart
#service php-fpm restart

Check the result http://ipaddress/phpMyAdmin
phpMyadmin page will appear but this still not work, we should edit phpMyadmin config.ing.php

#cd /usr/share/nginx/html/phpMyAdmin/
#mv config.sample.inc.php config.inc.php
#vi /usr/share/nginx/html/phpMyAdmin/config.ing.php
Edit following lines:
$cfg['Servers'][$i]['auth_type'] = 'cookie';
With this:
$cfg['Servers'][$i]['auth_type'] = 'http';
#service nginx restart
#service php-fpm restart

Check the result again http://ipaddress/phpMyAdmin
It will work!

Setup Nginx Virtual Host on CentOS 6

Assume NGINX MySQL PHP (LEMP) has been success installed on CentOS 6, for tutorial how to install visit here

Create a New Directory for web files
The first step in creating a virtual host is to a create a directory where we will keep the new website’s information. This location will be your Document Root in the Apache virtual configuration file later on. By adding a -p to the line of code that allows us to create a folder with a nested folder inside of it

#mkdir -p /var/www/example.com/public_html

Grant Ownership Permissions of directory to user (assumed username ‘user’ has been added)

#chown -R user:nginx /var/www/example.com/public_html

Additionally, it is important to make sure that everyone will be able to read our new files.

#chmod 755 /var/www

Create index html page to for test within our configurations directory.

#vi /var/www/example.com/public_html/index.html
<html>
<head>
<title>www.example.com</title>
</head>
<body>
<h1>Success: You Have Set Up a Virtual Host</h1>
</body>
</html>

Turn on Nginx Virtual Host
To setup nginx virtual host, we will need to open up nginx virtual file located at /etc/nginx/conf.d/virtual.conf and you will need to update it with the following configuration

#vi /etc/nginx/conf.d/virtual.conf
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/public_html;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# Logging -- access_log /var/www/example.com/public_html/access.log; error_log /var/www/example.com/public_html/error.log; # serve static files directly location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ { access_log off; expires max; } }

Restart nginx and php-fpm service

#service nginx restart

Setting Up the Local Hosts
If you have pointed your domain name to your virtual private server’s IP address you can skip this step. You do not need to set up local hosts. Your virtual hosts should work. However, if want to try out your new virtual hosts without having to connect to an actual domain name, you can set up local hosts on your computer alone.

#vi /etc/hosts
# Host Database
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
#Virtual Hosts
12.34.56.789 www.example.com

Test if nginx virtual host has been working by access this at browser http://example.com

Enable PHP
To enable PHP please add following line to /etc/nginx/conf.d/virtual.conf

#vi /etc/nginx/conf.d/virtual.conf
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /var/www/example.com/public_html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

CHECK RESULTS:
Create a php info page and add in the following line:

#vi /var/www/example.com/public_html/info.php
<?php
phpinfo();
?>

Then Save and Exit.
Restart nginx and php-fpm service again so that all of the changes take effect:

#service nginx restart
#service php-fpm restart

Test if php working by access this at browser http://example.com/info.php

Install NGINX MySQL PHP (LEMP) on CentOS 6

To avoid any conflict once NGINX service start please make sure Apache service already removed (if available). We can remove Apache service as follow
Shutdown Apache service (if running)

#service httpd stop

Remove Apache from the boot cycle, so that it does not start up again on server boot

#chkconfig httpd off

Remove Apache package

#yum remove httpd

STEP ONE: UPDATE EPEL REPOSITORY

NGINX is available as a package for CentOS 6 from epel repository but first we should update epel repository, which we can install as follows:

#yum install epel-release

STEP TWO: INSTALL MYSQL

MySQL is a powerful database management system used for organizing and retrieving data on a virtual server. To install MySQL, open terminal and type in these commands:

#yum install mysql-server
#service mysql start

During the installation, MySQL will ask you for your permission twice. After you say Yes to both, MySQL will install. Once it is done installing, you can set a root MySQL password:

#/usr/bin/mysql_secure_installation

The prompt will ask you for your current root password. Since you just installed MySQL, you most likely won’t have one, so leave it blank by pressing enter.

Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n]
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!
Remove anonymous users? [Y/n] y
 ... Success!
Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
Reload privilege tables now? [Y/n] y
 ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!

Set the processes to run automatically when server boots

#chkconfig mysql on

STEP THREE: INSTALL NGINX

Nginx install using yum

#yum install nginx

To get NGINX running type:

#/etc/init.d/nginx start

We can check that nginx has installed by open a browser and type http://ipaddressNGINX-success-installed
Configure the server to start NGINX on server boot

#chkconfig nginx on

STEP FOUR: INSTALL PHP
The php-fpm package is located within the REMI repository, which, at this point, is disabled. The first thing we need to do is enable the REMI repository and install php and php-fpm:

#wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
#rpm -Uvh remi-release-6.rpm

Enable REMI repository by edit file remi.repo.
Find the line enabled =0 and change all of it to 1 to enable REMI repository.

#vi /etc/yum.repos.d/remi.repo
[...]
enabled=1
[...]

Then we can install php and php-fpm as follows:

#yum install php-fpm php-mysql

Set the php-fpm processes to run automatically when server boots

#service php-fpm start
#chkconfig php-fpm on

STEP FIVE: CONFIGURE PHP
We need to make one small change in the php.ini configuration.
Find the line, cgi.fix_pathinfo=1, and change the 1 to 0

#vi /etc/php.ini
cgi.fix_pathinfo=0

If this number is kept as a 1, the php interpreter will do its best to process the file that is as near to the requested file as possible. This is a possible security risk. If this number is set to 0, conversely, the interpreter will only process the exact file path a much safer alternative. Save and Exit.

STEP SIX: CONFIGURE NGINX
Open up the default nginx config file and raise the number of worker processes to 4 then save and exit that file.

#vi /etc/nginx/nginx.conf
[...]
worker_processes  4;
[...]

Nginx already comes with a default configuration to use php fpm. Open the /etc/nginx/conf.d/default.conf file and look following lines.

#vi /etc/nginx/conf.d/default.conf
# The default server
server {
    listen       80;
    server_name example.com;
 
    location / {
        root   /usr/share/nginx/html;
        index index.php  index.html index.htm;
    }
 
    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

Here are the details of the changes:

  • Add index.php within the index line.
  • Change the server_name to your domain name or IP address (replace the example.com in the configuration)
  • Change the root to /usr/share/nginx/html;
  • Uncomment the section beginning with “location ~ \.php$ {“,
  • Change the root to access the actual document root, /usr/share/nginx/html;
  • Change the fastcgi_param line to help the PHP interpreter find the PHP script that we stored in the document root home.
  • Save and Exit

Open up the php-fpm configuration, replace the apache in the user and group with nginx:

#vi /etc/php-fpm.d/www.conf
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

Finish by restarting php-fpm.

#service php-fpm restart

CHECK RESULTS:
Create a php info page and add in the following line:

#vi /usr/share/nginx/html/info.php
<?php
phpinfo();
?>

Then Save and Exit.
Restart nginx so that all of the changes take effect:

#service nginx restart

We can check that NGINX MySQL PHP has installed successfully by open a browser and type http://ipaddress/info.php

Set Up nginx, mysqld, php-fpm Autostart

#chkconfig --levels 235 mysql on
#chkconfig --levels 235 nginx on
#chkconfig --levels 235 php-fpm on

How to zip unzip file folder on CentOS terminal

By default zip unzip are not installed on CentOS, so we have to install it

[root@vpshelpdesk]# yum install zip
Loaded plugins: fastestmirror
Setting up Install Process
[root@vpshelpdesk]# yum install unzip
Loaded plugins: fastestmirror
Setting up Install Process

Compress files error.log to error.zip in current directory

[root@vpshelpdesk]zip error.zip error.log

Compress entire files, folder but not including all subdirectories to data.zip in current directory

[root@vpshelpdesk]#zip data.zip *

Compress entire files, folder, all subdirectories and hidden files to data.zip

[root@vpshelpdesk]#zip -r data.zip .* -x "../*"

To uncompress or extract data.zip

[root@vpshelpdesk]#unzip data.zip

You can also test data.zip, printing only a summary message indicating whether the archive is OK or not:

[root@vpshelpdesk]#unzip -tq data.zip

To extract the file called file.txt from data.zip:

[root@vpshelpdesk]#unzip data.zip  file.txt

To extract all files into the /tmp directory:

[root@vpshelpdesk]#unzip data.zip  -d /tmp

To list all files from data.zip:

[root@vpshelpdesk]#unzip -l data.zip

To move all folder and files

#mv /sourcefolder/{,.[^.]}* /destinationfolder/