Subdomains with .htaccess

0
เขียน rewrite rule เพื่อแปลง subdomain จาก http://backend.example.com ให้เรียก http://www.example.com/backend/
โดยที่ชื่อโดเมนยังคงเป็น backend.example.com เหมือนเดิม

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/
backend
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^backend\.example
\.com
RewriteRule (.*) /backend/$1 [L]

Ref#
http://www.webmasterworld.com/forum92/3555.htm

Another way to do this is to rewrite <subdomain>.example.com to /sd_<subdomain>/ — That is, write each subdomain to a uniquely-named subdirectory. You could also name the subdirectories "user-<subdirectory>", or the familiar "~<subdomain>", or anything like that, but the prefix needs to be something that you ‘reserve’ for use only in naming first-level subdomain subdirectories.

In those cases, you could use:

# Rewrite subdomain requests to subdirectories except for http://www.example.com
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/sd_
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /sd_%1/$1 [L]
#
# Redirect direct user-agent requests for http://www.example.com/sd_<subdomain>/<page&gt; to http://<subdomain&gt;.example.com/<page>
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /sd_(.+)\ HTTP/
RewriteRule ^sd_([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]

The first section supports all subdomain requests, while the second section keeps users and search engines from finding the ‘real’ location of the files under http://www.example.com/sd_<subdomain>, thus preventing curiosity-seekers from finding them and duplicate-content problemns in search engine results.

Jim



How to MySQL Replication

0

1 Configure The Master

First we have to edit /etc/mysql/my.cnf.
We have to enable networking for MySQL, and MySQL should listen on all
IP addresses, therefore we comment out these lines (if existant):

#skip-networking
#bind-address = 127.0.0.1

Furthermore
we have to tell MySQL for which database it should write logs (these
logs are used by the slave to see what has changed on the master),
which log file it should use, and we have to specify that this MySQL
server is the master. We want to replicate the database exampledb, so we put the following lines into /etc/mysql/my.cnf:


log-bin = /var/log/mysql/mysql-bin.log
# what database you want to replication
binlog-do-db=exampledb
server-id=1

Then we restart MySQL:

/etc/init.d/mysql restart

Then we log into the MySQL database as root and create a user with replication privileges:

mysql -u root -p
Enter password:

Now we are on the MySQL shell.

GRANT REPLICATION SLAVE ON *.* TO ‘slave_user’@’%’ IDENTIFIED BY ‘<some_password>’; (Replace <some_password> with a real password!)
FLUSH PRIVILEGES;

Next (still on the MySQL shell) do this:

USE exampledb;
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;

The last command will show something like this:

Write down this information, we will need it later on the slave!

+---------------+----------+--------------+------------------+
| File | Position | Binlog_do_db | Binlog_ignore_db |
+---------------+----------+--------------+------------------+
| mysql-bin.006 | 183 | exampledb | |
+---------------+----------+--------------+------------------+
1 row in set (0.00 sec)

Then leave the MySQL shell:

quit;

There are two possibilities to get the existing tables and data from exampledb from the master to the slave.
The first one is to make a database dump,
the second one is to use the
LOAD DATA FROM MASTER; command on the slave.
The latter has the disadvantage the the database on the master will be locked
during this operation, so if you have a large database on a
high-traffic production system, this is not what you want, and I
recommend to follow the first method
in this case
. However, the latter
method is very fast, so I will describe both here.

If you want to follow the first method, then do this:

This will create an SQL dump of exampledb in the file exampledb.sql. Transfer this file to your slave server!
If you want to go the LOAD DATA FROM MASTER; way then there is nothing you must do right now.

mysqldump -u root -p<password> –opt exampledb > exampledb.sql (Replace <password> with the real password for the MySQL user root! Important: There is no space between -p and <password>!)

Finally we have to unlock the tables in exampledb:

mysql -u root -p
Enter password:
UNLOCK TABLES;

quit;

Now the configuration on the master is finished. On to the slave…


2 Configure The Slave

On the slave we first have to create the database exampledb:

mysql -u root -p
Enter password:
CREATE DATABASE exampledb;

quit;

————————————————————————————————————————————-
If you have made an SQL dump
of exampledb on the master and have transferred it to the slave, then it is time now to import the SQL dump into our newly created exampledb on the slave:

mysql -u root -p<password> exampledb < /path/to/exampledb.sql (Replace <password> with the real password for the MySQL user root! Important: There is no space between -p and <password>!)
————————————————————————————————————————————-

Now we have to tell MySQL on the slave that it is the slave, that the master is 192.168.0.100, and that the master database to watch is exampledb. Therefore we add the following lines to /etc/mysql/my.cnf:

server-id=2
master-host=192.168.0.100
master-user=slave_user
master-password=secret

#if connection fail retry connect in .. second
master-connect-retry=60

#what database you want to replicate
# replicate-do-db=exampledb
#or what wildcard table you want to replicate (for replicate many table)
# replicate-wild-do-table=exampledb.%
#or what table you want to replicate (if more than one table, do this multiple)
replicate-do-table=exampledb.member

Then we restart MySQL:

/etc/init.d/mysql restart

———————————————————————————————————————————–
If you want to go the
LOAD DATA FROM MASTER; way, then it is time for you now to get the data from the master exampledb:

mysql -u root -p
Enter password:
LOAD DATA FROM MASTER;

quit;

If you have phpMyAdmin installed on the slave you can now check if all tables/data from the master exampledb is also available on the slave exampledb.

IF YOU SEE THIS IN SLAVE SIDE
mysql > LOAD DATA FROM MASTER;


ERROR 1219 (HY000): Error running query on master: Access denied; you need the RELOAD privilege for this operation

TYPE THIS IN MASTER SERVER

mysql> grant RELOAD,SUPER,REPLICATION CLIENT on *.* to repl@’%’ identified by ‘your_password’;
————————————————————————————————————————————

Finally, we must do this:

mysql -u root -p
Enter password:
SLAVE STOP;

In the next command (still on the MySQL shell) you have to replace the values appropriately:

CHANGE MASTER TO MASTER_HOST=’192.168.0.100′, MASTER_USER=’slave_user’, MASTER_PASSWORD=’<some_password>‘, MASTER_LOG_FILE=’mysql-bin.006’, MASTER_LOG_POS=183;

START SLAVE;
quit;

That’s it! Now whenever exampledb is updated on the master, all changes will be replicated to exampledb on the slave. Test it!

Troubleshooting

-------- Master:
mysql> SHOW GRANTS FOR repl;
mysql> SHOW MASTER LOGS \G
mysql> SHOW BINARY LOGS;
mysql> SHOW MASTER STATUS;
-------- SLAVE:
mysql> SHOW SLAVE STATUS;
mysql> STOP SLAVE;
mysql> START SLAVE;

reference:
http://www.howtoforge.com/mysql_database_replication/
http://janux.aleux.com/blog/2007/07/25/how-to-mysql-replication/
http://dev.mysql.com/doc/refman/5.1/en/replication-options-slave.html