Sunday, March 22, 2020

Change outbound connections IP for Mail

Hello admins, the server will start to send the Spam mails due to malicious code or compromised mailbox. In that case, our outgoing IP address will get blocked by several spam filters including Gmail and other mail services.

In this case, we have to wait for a long time to get whitelisted or have to pay to be whitelisted. But if you have secondary IP addresses for your server, we can do this trick to send emails and let the main IP to whitelist automatically.

This means we will change to outbound connections from the main IP to the secondary IP address.

Important: Before proceeding with this, make sure that you have fixed the compromised code or application or mailbox. Else, your secondary IP address will also get blocked on Spam filters.

Default IP for outgoing connections can be set using ‘ip’ utility.
First of all, check how routing is configured on the server:

You can check this by the following command: -
# /sbin/ip route
159.8.30.64/28 dev bond1 proto kernel scope link src 159.8.30.66
10.105.124.128/26 dev bond0 proto kernel scope link src 10.105.124.130
169.254.0.0/16 dev bond0 scope link metric 1006
169.254.0.0/16 dev bond1 scope link metric 1007
10.0.0.0/8 via 10.105.124.129 dev bond0
default via 159.8.30.65 dev bond1
If no ‘src’ listed in ‘ip’ output for the default route, then the main IP on the interface is used for outgoing connections. You can change it using:
# /sbin/ip route change default via 159.8.30.65 dev bond1 src 5.153.43.25
Now:
# /sbin/ip route
192.168.50.0/24 dev eth1 proto kernel scope link src 192.168.50.53
169.254.0.0/16 dev eth1 scope link
default via 192.168.50.254 dev eth1 src 192.168.50.100

Note: be careful, if you set the wrong IP as the source, you lose the connection to the server.

Thursday, October 31, 2019

Find and clear log files in Linux

Objective:

Find and clear the files with extension of *.log, usually log files.

Commands:

find . -type f -iname *.log -exec tee {} \; </dev/null

This command will find the files with extension of 'log' and empty that file. Which means file will be there and data of that file will be cleared.

If you are using cPanel, there will be a directory called "virtfs", in that case you can use following command.

find /home -type f -iname *.log -not -path "./virtfs/*" -exec tee {} \; </dev/null

Please note, in WHM / cPanel server, this will clear logs including created by server.

So, If you need to do this manually for specific file, you can simply use any of following commands,

# > file.name

# :> file.name

# true > file.name

# echo "" > file.name

# echo  > file.name

# dd if=/dev/null of=logfile

# dd if=/dev/null > logfile

Still there are several methods to clear it.

Saturday, March 16, 2019

Connect MS-SQL Database on PHP under Ubuntu-18.04

I can see that several tutorials available over Internet for "Connecting Ms SQL by PHP code on Ubuntu 18.04", But as much as I saw, non of the mention about a small thing that plays important role in this. Here it is,

Let's see how to configure,

Requirements:
1. MS SQL 2017 STD (I've tested with 2014 Express too, It's working fine.)
2. Apache2 with PHP 7.2 (I'm not sure about previous versions, but as per my search, it'll work well on other versions too.)

1. Steps to install Microsoft ODBC Driver for SQL on Ubuntu:

sudo su -l

curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - 

#Change OS version based on yours, 16.04 or 18.04 as per your need.

curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list

apt update
apt -y install msodbcsql17
apt -y install unixodbc-dev mssql-tools

Detailed steps for other operating systems and versions are located at,
https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017

2. Installing and configuring sqlsrv and pdo_sqlsrv for PHP

apt -y install php-pear php-dev
pecl install sqlsrv
pecl install pdo_sqlsrv

echo "extension=sqlsrv.so" > /etc/php/7.2/mods-available/sqlsrv.ini
echo "extension=pdo_sqlsrv.so" > /etc/php/7.2/mods-available/pdo_sqlsrv.ini

#This may vary based on php version and loaded php conf.
ln -s /etc/php/7.2/mods-available/sqlsrv.ini /etc/php/7.2/apache2/conf.d/20-sqlsrv.ini
ln -s /etc/php/7.2/mods-available/pdo_sqlsrv.ini /etc/php/7.2/apache2/conf.d/20-pdo_sqlsrv.ini

3. Checking connection to the SQL Server with sqlcmd:

#Configuring sqlcmd on profile,

echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc

# Structure for sqlcmd connection test,

sqlcmd -S {ServerNmae\InstanceName} -U {UserName} -P {Password}

example:
sqlcmd -S MYSQLSERVER\SQL2017STD -U kumar_test_usr -P MySeurePassword

But, It doesn't work for me and throws error like,

Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2AF9.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..

After some investigations, I found that the problem with port of the SQL server and Named Instance.

The default port of the SQL server (1433) doesn't work here. You should have to mention port of the SQL

So, First you have to find the port, which is located at your SQL Server Configuration Manager

SQL Server Network Configuration -> Protocol for SQL2017STD {This will be your Instance Name}

TCP/IP - > Right click and Enable it, If it's disabled.
TCP/IP - > Right click and go to Properties, Under the IP Address tab, Go to the end, Check out the Port mentioned in TCP Dynamic Ports or you can specify your port on TCP Port.

We going to use this port for connection.

As we are using Named Instance, we should mention "\\" instead of "\" in connection string, the final command will be like as mentioned below,

sqlcmd -S MYSQLSERVER\\SQL2017STD, 55042 -U kumar_test_usr -P MySeurePassword

If you are not have access to find the port, you can use nmap to find it,

4. Checking connection to the SQL Server with php:

PHP code for check the connection,

<?php
$serverName = "MYSQLSERVER\\SQL2017STD, 55042";
$connectionInfo = array( "Database"=>"kumar_test_delete", "UID"=>"kumar_test_usr", "PWD"=>"MySeurePassword");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}
?>

Friday, October 12, 2018

disable_functions with Easy Apache 4 and PHP-FPM

Recently I've faced the issue about disable_functions !!!

I need to use 'shell_exec' for one of application with PHP.

By default php will disable some functions with php.ini file with "disable_functions" line.

It will look like,

disable_functions = "exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source"

If you wanna use these functions, you have to remove that function name from disable_functions line on php.ini.

Let's take a look on configurations,

I logged on my WHM and went to "Home »Software »MultiPHP INI Editor", By choosing my php version, I can see the file. Shockingly, disable_functions is empty. If it's empty, then how the function is getting disabled. And verified my php.ini file path. It's too respond same that "disable_functions" with empty value.

So I used phpinfo() in side website and checked. It's showing disable_function is active with 'shell_exec'.

So who is that enabling "disable_functions" field with 'shell_exec'.

Everything fine before upgrading to EA4 with PHP-FPM. So he must be doing these changes.

Yes, once I turn off PHP-FPM for the website. It's working. It's simple solution for single website. But, How to do this globally ? Will update shortly !!!

Edit crontab with VIM

How to change crontab editor from NANO to VIM ?

Usually google searches give following method, But it didn't work for me,

export EDITOR=/usr/bin/vim
crontab -e

or

EDITOR=vim crontab -e

At same time, following is working fine for me.

export VISUAL=/usr/bin/vim
crontab -e

VISUAL=vim crontab -e

Tuesday, September 18, 2018

cPanel MultiPHP Manager with PHP-FPM

We all know, cPanel is one of best server admin panel. It's best thing is Easy Apache. It is user friendly GUI platform for managing Apache and PHP in WHM panel. cPanel is going to withdraw support for EasyApache 3 (EA3) and asking every user to upgrade EA4.

Recently I've upgraded it, It's too simple process, you can do this with their official tutorial.

But, after upgraded and if you are enabled and using PHP-FPM (FastCGI Process Manager), you may face some slowdown on websites or you may face frequent "This site can't be reached" error message on browser.

This is happens due to max_children size may exceed. That means, the default value of max_children is set to 5 on PHP-FPM. You can trace this issue with below command,

root@server [~]#egrep 'example.*max_children' /opt/cpanel/ea-php56/root/usr/var/log/php-fpm/error.log

the above commend will help to find specific website, where example is domain name without extension and ea-php56 is php version, where it may ea-php70 or ea-php72 or any other based on your php version.

This will give result as mentioned bellow,
[18-Sep-2018 05:34:58] WARNING: [pool example_com] server reached max_children setting (5), consider raising it
[18-Sep-2018 08:28:00] WARNING: [pool example_com] server reached max_children setting (5), consider raising it
[18-Sep-2018 08:44:26] WARNING: [pool example_com] server reached max_children setting (5), consider raising it
[18-Sep-2018 08:44:55] WARNING: [pool example_com] server reached max_children setting (5), consider raising it
[18-Sep-2018 09:31:40] WARNING: [pool example_com] server reached max_children setting (5), consider raising it
[18-Sep-2018 09:57:16] WARNING: [pool example_com] server reached max_children setting (5), consider raising it


If you get result as mentioned above, you may need to adjust max_children value on bellow steps.

WHM Home > Software > MultiPHP Manager

If you are not sure about website name, you can use below command to get all results,

root@server [~]#egrep 'consider raising it' /opt/cpanel/ea-php56/root/usr/var/log/php-fpm/error.log

Saturday, July 21, 2018

Finding multiple folders in Linux with single command

Finding multiple folders with single commend on Linux is not a big deal.

I wanna know all temp / tmp and cache folders in websites, that are configured with WHM to exclude from the backup. Here is the command for that,


find /home/*/public_html \( -type d -iname "cache" -or -iname "tmp" -or -iname "temp" \)

To know size of these folders,

find /home/*/public_html \( -type d -iname "cache" -or -iname "tmp" -or -iname "temp" \) -exec du -sh {} \;