Monday, 23 January 2012

Configure LAMP server (Linux + Apache + MySQL + PHP/Perl) on Linux

Configure LAMP server (Linux + Apache + MySQL + PHP/Perl) on Linux

1) Install Apache
# yum install httpd httpd­devel

start the apache service
# /etc/init.d/httpd start


2) Install MySQL Database Server

# yum install mysql mysql­server mysql­devel
# /etc/init.d/mysqld start
# mysql

Changing MySQL Root Password

# mysql> USE mysql;
# mysql> UPDATE user SET Password=PASSWORD('newpassword') WHERE user='root';
# mysql> FLUSH PRIVILEGES;

Once done, check by logging in:

# mysql ­u root ­p
Enter Password:

To Create A New MySQL User

# mysql > create database demo
# mysql >GRANT ALL PRIVILEGES ON demo.* TO 'guest'@'localhost' IDENTIFIED BY 'guest'
WITH GRANT OPTION;
# mysql> UPDATE user SET Password=PASSWORD('guest') WHERE user='guest';

3) Install PHP5
# yum install php php­mysql php­common php­gd php­mbstring php­mcrypt php­devel php­xml

Test If PHP Is Working Or Not:
Create a file named /var/www/html/test.php with the following phpinfo() function inside php
quotes.
// test.php
phpinfo();
?>
Then point your browser to http://ip.address/test.php.


4) Install phpMyAdmin
phpMyAdmin is a free web based MySQL database Administration Tool

# yum install phpmyadmin
Point your browser to: http://ip.address/phpmyadmin.

5) Install Webmin
wget
rpm ­ i webmin­1.410­1.noarch.rpm

Point your browser to: http://ip.address:10000

You should see a webmin login. But we don't know the login and password yet! To set up the
webmin password run the script below...

# /usr/libexec/webmin/changepass.pl /etc/webmin admin

Log in with the admin username and new webmin password!
To uninstall webmin, just run:

# /etc/webmin/uninstall.sh

6) Final Steps
We want the Apache and mysql to be loaded at every boot so we switch them on using chkconfig:
# chkconfig httpd on
# chkconfig mysqld on

Recover MySQL root Password


You can recover MySQL database server password with following five easy steps.

Step # 1: Stop the MySQL server process.

Step # 2: Start the MySQL (mysqld) server/daemon process with the --skip-grant-tables option so that it will not prompt for password.

Step # 3: Connect to mysql server as the root user.

Step # 4: Setup new mysql root account password i.e. reset mysql password.

Step # 5: Exit and restart the MySQL server.

Here are commands you need to type for each step (login as the root user):

Step# 1 : Stop mysql service

# /etc/init.d/mysql stop
Output:

Stopping MySQL database server: mysqld.

Step # 2: Start to MySQL server w/o password:

# mysqld_safe --skip-grant-tables &
Output:

[1] 5988
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6025]: started

Step # 3: Connect to mysql server using mysql client:

# mysql -u root
Output:

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.1.15-Debian_1-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

Step # 4: Setup new MySQL root user password

mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit
Step # 5: Stop MySQL Server:

# /etc/init.d/mysql stop
Output:

Stopping MySQL database server: mysqld
STOPPING server from pid file /var/run/mysqld/mysqld.pid
mysqld_safe[6186]: ended

[1]+ Done mysqld_safe --skip-grant-tables

Step # 6: Start MySQL server and test it

# /etc/init.d/mysql start
# mysql -u root -p

How To Flush Linux / UNIX DNS Cache

Flush nscd dns cache

Nscd caches libc-issued requests to the Name Service. If retrieving NSS data is fairly expensive, nscd is able to speed up consecutive access to the same data dramatically and increase overall system performance. Just restart nscd:

$ sudo /etc/init.d/nscd restart
OR
# service nscd restart

Flush dnsmasq dns cache

dnsmasq is a lightweight DNS, TFTP and DHCP server. It is intended to provide coupled DNS and DHCP service to a LAN. Dnsmasq accepts DNS queries and either answers them from a small, local, cache or forwards them to a real, recursive, DNS server. This software is also installed many cheap routers to cache dns queries. Just restart to flush out dns cache:

$ sudo /etc/init.d/dnsmasq restart

Flush caching BIND server dns cache

A caching BIND server obtains information from another server (a Zone Master) in response to a host query and then saves (caches) the data locally. All you have to do is restart bind to clear its cache:


# /etc/init.d/named restart

How to check a perticular process uptime in Linuz

We can check a uptime for any process using linux command. But you should have to know process ID for this.

We can find process ID using below commands.
#ps -ef | grep process_name

Below is the command which provides you the uptime for this process.

 #ps -p 12119 -o "%t"





Alter Multiple tables using bash script

Below is the shell script to alter multiple tables.


#!/bin/bash

DATABASENAME="DBNAME"
for t in `echo "show tables" | mysql --batch --skip-column-names $DATABASENAME`;
do mysql $DATABASENAME -e "ALTER TABLE $t ENGINE=NDBCLUSTER";
done



How to repaire all MySQL tables in one command

You can use below command to optimize & repair all tables.

#mysqlcheck -u root -p --auto-repair --check --optimize --all-databases

Count all records from your MySQL databse

If you want to count all records from your database, follow below instructions.

1) Login To your database.

2) Execute below query

> SELECT table_name, TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'DATABASE_NAME' order by TABLE_ROWS asc;

You have to change only database_name from above query.

MySQL Change root Password


How do I change MySQL root password under Linux, FreeBSD, OpenBSD and UNIX like operating system over ssh / telnet session?

Setting up MySQL password is one of the essential tasks. By default root user is MySQL admin account user. Please note that the Linux / UNIX root account for your operating system and MySQL root are different. They are separate and nothing to do with each other. Sometime your may remove mysql root account and setup admin as mysql super user for security purpose.
mysqladmin command to change root password

If you have never set a root password for MySQL server, the server does not require a password at all for connecting as root. To setup root password for first time, use mysqladmin command at shell prompt as follows:

$ mysqladmin -u root password NEWPASSWORD

However, if you want to change (or update) a root password, then you need to use the following command:

$ mysqladmin -u root -p'oldpassword' password newpass

For example, If the old password is abc, you can set the new password to 123456, enter:

$ mysqladmin -u root -p'abc' password '123456'

Change MySQL password for other users

To change a normal user password you need to type (let us assume you would like to change password for user vivek) the following command:

$ mysqladmin -u vivek -p oldpassword password newpass

Changing MySQL root user password using MySQL sql command

This is another method. MySQL stores username and passwords in user table inside MySQL database. You can directly update password using the following method to update or change password for user vivek:

1) Login to mysql server, type the following command at shell prompt:

$ mysql -u root -p

2) Use mysql database (type command at mysql> prompt):

mysql> use mysql;

3) Change password for user vivek, enter:

mysql> update user set password=PASSWORD("NEWPASSWORD") where User='vivek';

4) Finally, reload the privileges:

mysql> flush privileges;
mysql> quit

The last method can be used with PHP, Python or Perl scripting mysql API.

Know your Motherboard in linux

check motherboard information in Linux

To check motherboard information in Linux, you can use either lspci or dmidecode.

# lspci –vv
# dmidecode

Friday, 16 December 2011

Force non-www to www and http to https using redirect rules

We can redirect non-www to www is quiet easy task with the mod_redirect. You can use below Redirect Rules for the same.

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]






Wednesday, 21 September 2011

SSL certificate

First we need to create a private key. Note that this process will require a passphrase for the key - don't worry, we'll remove it later to make things easier:
openssl genrsa -des3 -out myssl.key 1024
As said, this will require you to enter a passphrase.

CSR

Now we need to create a CSR (Certificate Signing Request):
openssl req -new -key myssl.key -out myssl.csr
The process will ask for various details for the certificate. I entered the following for each question:
Country Name: GB
State or Province Name: Nottinghamshire
Locality Name: Nottingham
Organization Name: PickledOnion Ltd
Organizational Unit Name: Web Development
Common Name: admin.domain.com
Email Address: webadmin@domain.com
For the 'extra' attributes I simply pressed 'return' (i.e. I left them blank).
Note: For the Common Name I entered the domain name I want to associate with the certificate. In this case I want it for my administration area so I entered 'admin.domain.com'.
You are not restricted to using the certificate with just that domain but it will produce extra warnings if the Common Name does not match the URI.

Remove Passphrase

When we generated the myssl.key file, we had to enter a passphrase. One disadvantage of this is the need to enter the passphrase if the Slice is rebooted.
This is especially problematic if an unexpected reboot occurs as the boot sequence will simply stop until you enter the console via the SliceManager and enter it.
So unless you see a particular need to keep the passphrase, let's remove it:
cp myssl.key myssl.key.org
openssl rsa -in myssl.key.org -out myssl.key
You will be asked for the passphrase one last time to confirm it is a genuine request.
Now we have three files in the temp folder:
ls
...
myssl.csr  myssl.key  myssl.key.org

CRT

The last file we need generate is the actual ssl certificate:
openssl x509 -req -days 365 -in myssl.csr -signkey myssl.key -out myssl.crt
Good. Now we have the final piece in place as that generated our myssl.crt file.

Everything in its place

Now we need to copy the relevant files to the /etc/ssl/ directory.
First file to move is the certificate itself:
sudo cp myssl.crt /etc/ssl/certs/
and secondly, copy the key:
sudo cp myssl.key /etc/ssl/private/

Clean up

You are now free to delete the temp file and the four files we generated or, if you prefer, keep them around for a while until you know the ssl certificate works correctly.

Summary

Nginx requires more than the standard pem file that Apache is happy with. As such, we need to create a ssl key and a certificate file.
Once the files have been generated and moved to the /etc/ssl/ directory, we are now ready to configure Nginx to serve our domain from an HTTPS connection.

Could not start a new session. Response code 500. Message: Failed to read marionette port

There is bug in firefox binary installed using apt/snap, I faced same issue when I installed firefox from apt package respository. I solved ...