I'm using wordpress for a specific client because of their need to edit content themselves. With this, I'm using their page password protection, per client's request. The problem is, it seems that the cookie being set never times out. So, once the client enters the password, nobody ever has to enter the password again through the same browser on the same machine. This leaves it wide open for anybody to walk up to and enter. So, I assume the best way to address this is to set a timeout on the cookie. However, I'm not sure how to do that with the php function. Here's the whole function:
Really, I'd like to have the cookie expire when the browser closes, and otherwise every few hours. Any advice on what to add to make the cookie expire after it's set?
I believe it would probably have to be added to this line:
There's a much, much easier way to do this, using thepost_password_expiresfilter. By default, the cookie expires 10 days from creation.To turn this into a session cookie, return 0.The following should be added to your theme'sfunctions.php:
function custom_password_cookie_expiry( $expires ) {
return 0; // Make it a session cookie
}
add_filter( 'post_password_expires', 'custom_password_cookie_expiry' );
Bot인 경우에 대비해서 이 차이를 정확히 명시해 주어야 합니다.(예를 들어, 웹 크롤러가 301을 반환받았다면 검색엔진의 Indexing된 주소를 수정할 것입니다.)
이 Redirection은 접속하려는 사용자에게 HTTPS를 통한 접속을 강제하려 할 때에도 유용하게 사용할 수 있습니다. 예를 들어, 다음과 같이 설정파일을 작성하면http://tuwlab.com으로 접속하려는 사용자를https://tuwlab.com으로 강제 Redirection 시킵니다.
이 경우 페이지를 이동하는 과정에 사용에게 순간적으로 노출이 되고, 자바스크립트를 꺼 놓은 경우 흰색의 빈 화면만 달랑 표시되고 아무 일도 일어나지 않게 됩니다. 더 큰 문제점은 이런 식으로 Redirection을 하면 검색엔진에서 크롤링을 하지 않거나, 스팸 사이트로 분류할수도 있다는 점입니다.
따라서 이동된 페이지에 대해서는 HTTP Status Code를 사용해서 정확히 무슨 일이 일어났는지 Client측에 알려주도록 해야 합니다.
How To Optimize WordPress Performance With MySQL Replication On Ubuntu 14.04
Introduction
In this tutorial, we will teach you how to scale up your WordPress MySQL database server setup using master-slave database replication and the HyperDB plugin for WordPress. Adding more database servers to your environment in this manner allows your WordPress application to read from multiple database servers, increasing read performance.
MySQL replication reaps the most performance benefits for a system that processes frequent reads and infrequent writes, like most WordPress installations. By using a single-master with multiple-slave setup, you can add more slaves to scale your system, until you run out of network bandwidth or your master cannot handle the update load. If you wish, you can add more than one slaves by repeating the “slave” portions of the replication sections of this tutorial.
We are assuming that your setup includes two load balanced WordPress application servers that connect to a separate MySQL database server (see the prerequisites for a tutorial on how to set that up). It is not strictly necessary to have load balanced application servers to follow this tutorial, but your MySQL database server should be separate from your application servers.
Prerequisites
Before continuing with this tutorial, you should have completed two tutorials or have a similar environment:
After following those tutorials, to set up WordPress with two load balanced web application servers and a separate database server, you should have four VPSs. Because we will be dealing with several VPSs, for reference purposes, we will call your four existing VPSs the following:
haproxy-www: Your HAProxy server for layer 4 load balancing your WordPress web application servers. This is the entry point into your website
wordpress-1: Your first WordPress web application server
wordpress-2: Your second WordPress web application server
mysql-1: Your MySQL server for WordPress
That is, your environment should look something like this:
In addition to your current environment, we will require one additional VPS during this tutorial. We will call it:
mysql-2: Your slave MySQL database server
Our Goal
When we are finished with this tutorial, you will have two database servers will be replicating in a master-slave configuration. Your WordPress servers will selectively write to your master and read from both your master and slave databases, by use of the HyperDB WordPress plugin. Your final environment should look something like this:
Keep in mind that you do not need to have load balanced application servers (wordpress-1/wordpress-2) to follow this tutorial, and that you can add more slave databases if you want.
Set Up MySQL Master-Slave Replication
Before we can configure our WordPress application to read from multiple database servers, we need to set up our MySQL replication.
Create MySQL Slave VPS, mysql-2
You will want to create a new VPS that will act as the MySQL slave server–for reference purposes, we will call this servermysql-2. The slave will be configured to replicate all of the databases of your master MySQL server, including your WordPress database.
bind-address: the IP address that MySQL will listen on. This should already be set tomysql-1’s private IP address from your original setup
server-id: the unique server ID. Since this is the master server, we will want to leave the value as “1” and uncomment this line
log_bin: the location of the binary log file. The binary log is used to send data changes from the master to its slave for replication. Uncomment this line
The three lines should look like this (be sure to substitute the highlighted with database server’s private IP address):
Optionally, if you want to restrict the replication to thewordpressdatabase, specifically, add the following line to your configuration (substituting the highlighted with your desired database name):
Save and quit. To put these changes into effect, restart mysql with the following command:
sudo service mysql restart
Connect to to the MySQL console with the following command, then enter the password when prompted:
mysql -u root -p
Create a user that will be used by our slave MySQL servers for replication purposes. We will call this userrepl. Be sure to replace therepl_passwordwith your own, strong password. The%specifies that the source IP for this user can be anything, but you may substitute the%with the private IP address of your slave MySQL server,mysql-2, to restrict connections with this user to that particular server:
<pre> CREATE USER ‘repl’@’<span class=“highlight”>%</span>’ IDENTIFIED BY ’<span class=“highlight”>repl_password</span>’; GRANT REPLICATION SLAVE ON.TO 'repl’@’%’; </pre>
Do not exit the MySQL console yet!
Export a Backup Of MySQL Master
Next, we will want to export a backup of the MySQL master database, to import into our slave database so it will be identical before we start replication. We need to lock the database so we can do a data dump. In your MySQL console onmysql-1, run this:
FLUSH TABLES WITH READ LOCK; SET GLOBAL read_only = ON; EXIT
Now, from your command shell, run the following command to export a backup of the databases on your master MySQL server to a file calledmasterdump.sql:
Save and quit. Restart MySQL to put the changes into effect:
sudo service mysql restart
Enter the MySQL console:
mysql -u root -p
Next, we will connect the slave to the master. The five following values are required:
MASTER_HOST: set to mysql-1’s private IP
MASTER_USER: set to the replication user that we created on the master,repl
MASTER_PASSWORD: set torepl’s password, which should be substituted with your own password
MASTERLOGFILE: set to the “File” listed when you ranSHOW MASTER STATUS;on your master MySQL server
MASTERLOGPOS: set to the “Position” listed when you ranSHOW MASTER STATUS;on your master MySQL server
The following statement connects your slave to your master server, and it requires that you substitute all of the highlighted fields with the appropriate values:
If that statement ran properly, run this command to initiate the slave connection:
START SLAVE;
Yourmysql-2server should be connected as a slave now! Run the following command to check that the replication is working:
SHOW SLAVE STATUS\G
Revoke Write Privileges From Slave Users
This is optional because the HyperDB plugin can be configured to only read from your slave database server, but you may want revoke the write privileges from yourwordpressuserdatabase users on your slave database (because updates to your slave will not be replicated to your master, if you accidentally update your slave somehow).
On mysql-2, from your MySQL console run the following statement to list your database users:
You should see output similar to the above code block. You may view privileges for each user with the following command:
<pre> SHOW GRANTS FOR <span class=“highlight”>wordpressuser</span>@<span class=“highlight”>wordpress1IP</span>; </pre>
In this example, we have onewordpressuserfor each WordPress server, so we will revoke theinsert,update, anddeleteprivileges from each of them (“wordpress” is the name of our database in this example):
<pre> REVOKE INSERT, UPDATE, DELETE ON <span class=“highlight”>wordpress</span>.* FROM ’<span class=“highlight”>wordpressuser</span>’@’<span class=“highlight”>wordpress1privateIP</span>’; REVOKE INSERT, UPDATE, DELETE ON <span class=“highlight”>wordpress</span>.* FROM ’<span class=“highlight”>wordpressuser</span>’@’<span class=“highlight”>wordpress2privateIP</span>’; FLUSH PRIVILEGES; </pre>
Now your MySQL replication setup is complete. Let’s move on to setting up WordPress to use both database servers properly.
Install and Configure HyperDB
We will use HyperDB to determine where to send updates (your master database) and read requests (your master and slave). Let’s download it to your home directory from the WordPress Plugin Directory (also install zip/unzip to unarchive it):
It should be unarchived to a directory called “hyperdb”, in your home directory. Copy the sample configuration file to your WordPress installation (substitute the highlighted with your WordPress installation path), and open it for editing:
<pre> cp ~/hyperdb/db-config.php <span class=“highlight”>/var/www/example.com</span>/ vi <span class=“highlight”>/var/www/example.com</span>/db-config.php</pre>
Look for thesecond occurrenceofDB_HOST, which should be directly after some comments that describe setting up a slave and it should look exactly like the following:
<pre> $wpdb->adddatabase(array( 'host’ => <span class=“highlight”>DBHOST</span>, // If port is other than 3306, use host:port. 'user’ => DBUSER, 'password’ => DBPASSWORD, 'name’ => DB_NAME, 'write’ => 0,'read’ => 1,'dataset’ => 'global’,'timeout’ => 0.2,));</pre>
The first occurrence of DBHOST defines the master database server, and the second occurrence defines the slave database server (denoted by the'write' => 0,). Replace the second occurrence of `DBHOSTwithDBSLAVE1`:
<pre> 'host’ => <span class=“highlight”>DBSLAVE1</span>, // If port is other than 3306, use host:port. </pre>
Save and exit. Next you will want to defineDB_SLAVE_1in your wp-config.php, which HyperDB will use as a slave database host. Open wp-config.php for editing:
<pre> vi <span class=“highlight”>/var/www/example.com</span>/wp-config.php </pre>
Find the line that definesDB_HOSTand add the following line under it, substituting your slave’s private IP address (mysql-2):
Then update the ownership of your wordpress files to their appropriate values (in this tutorial, we have been usingwww-datafor the user/group ownership):
Now your WordPress read requests will be served by both your master and slave databases, while updates will be sent to your master (which will then be replicated to your slave).
Conclusion
Now that you have completed your MySQL replication and HyperDB setup, your database environment will be able to handle increased read traffic i.e. more concurrent users! Remember that you can add more MySQL slaves if you want to scale your database serving capacity even more.