Archive for the ‘Wordpress’ Category

WordPress Permalinks Apache Setup

June 18th, 2016 No comments

This is a post copied from here. All the credits go to the original author of that post.

Thought I’d include some instructions for setting up “WordPress Permalinks” (which don’t work by default) which means you can get rid of those ugly WordPress URI’s and replace them with something more human readable.

Assuming you have inserted the runtime mod_rewrite DSO or complied Apache with —enable-rewrite then in order to get WordPress permalinks to function you need to include the following in your Apache config file (httpd.conf):

The quoted block below needs to go inside your virtual host section for your WordPress installation. Once Apache has been restarted your permalinks menu inside “Wordpress Admin” can be used and now your new URL’s should be functional.

<Directory "Directory_to_your_Wordpress">
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
Categories: Linux, Wordpress Tags:

How to Install Apache, MySQL, PHP, and phpMyAdmin on FreeBSD

June 18th, 2016 No comments

This is a post copied from here. All the credits go to the author, iceflatline.

This post will describe how to install and configure Apache, MySQL, PHP and phpMyAdmin on FreeBSD for basic local web development. Once set up, you’ll be able to use your “AMP” server to do web development, code testing, maintain local copies of your web sites, etc.

The software discussed in this post are available as free and open source under various licensing structures. The versions of software discussed in this post are as follows:

FreeBSD 10.3-RELEASE-p4
Virtual Box 5.0.20 r106931
WordPress 4.5.2

The following steps discussed in this post assume you have the FreeBSD Ports Collection installed. If not, you can install it using the following commands:

portsnap fetch
portsnap extract

If the Ports Collection is already installed, make sure to update it:

portsnap fetch update

Okay, let’s get started. All commands are issued as the root user or by simulating the root user by using the command su. While building the various ports you should accept all default configuration options unless otherwise instructed.

Install Apache

Navigate to the Apache server port and build it:

cd /usr/ports/www/apache24
make config-recursive install distclean

Once Apache has been successfully installed, add the following line to /etc/rc.conf so that the Apache server will start automatically at system boot.

sysrc apache24_enable=YES

Now let’s start Apache to make sure it works:

service apache24 start

Point your web browser to the host name or IP address of the FreeBSD host you’ve installed Apache on and you should see the venerable “It works!”

Install MySQL

Now let’s build the MySQL server:

cd /usr/ports/databases/mysql57-server
make config-recursive install distclean

Add the following line to /etc/rc.conf:

sysrc mysql_enable=YES

And start the mysql server:

service mysql-server start

Then set a password for the MySQL root user:

/usr/local/bin/mysql -u root -p

You’ll be requested to enter a password. Enter the password contained in /root/.mysql_secret. You’ll now be at the command prompt for the mysql server. Change your password using the following command:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'your-new-password';

Enter ‘quit’ to exit the server. You may now delete /root/.mysql_secret.

Install PHP

Next, we’ll build PHP:

cd /usr/ports/lang/php70
make config-recursive install distclean

Then the module required to support PHP applications on Apache:

cd /usr/ports/www/mod_php70
make config-recursive install distclean

Now let’s add the extensions to PHP to round out its capabilities. Before we build this port though we’ll want to add support for MySQLi (an improved interface to MySQL) in order to communicate with the MySQL server.

cd /usr/ports/lang/php70-extensions/
make config-recursive

In the corresponding menu you should select “MYSQLI”, then proceed with building the port:

make install distclean

Install phpMyAdmin

phpMyAdmin is a free software tool written in PHP intended to handle the administration of MySQL from your web browser. phpMyAdmin supports a wide range of operations with MySQL, including managing databases, tables, fields, relations, indexes, users, permissions, etc., from an easy-to-use web page, while you still have the ability to directly execute any SQL statement from the command line if you prefer. Installing phpMyAdmin is optional but it’s nice tool to have:

cd /usr/ports/databases/phpmyadmin/
make config-recursive install distclean


Now that we have the requisite ports built and installed it’s time to configure them. First, let’s create the file /usr/local/etc/php.ini to hold our PHP options. The simpliest way to do this is to copy the file /usr/local/etc/php.ini-development which will add the default settings for new PHP installations. This configuration is suitable for development purposes, but NOT necessarily for production purposes. If your plans include a production server, then among other things, and before going online with your site, you should consider copying /usr/local/etc/php.ini-production instead and consult the recommendations at

cp /usr/local/etc/php.ini-development /usr/local/etc/php.ini

Now let’s configure Apache. Open the file /usr/local/etc/apache24/httpd.conf and look for the following line:

DirectoryIndex index.html

And change it so it reads as follows:

DirectoryIndex index.html index.php

Then append the following lines to the end of the file in order to support PHP files as well as phpMyAdmin, which normally lives outside of the Apache document root. Note: if you elected not to install phpMyAdmin, then you need only add the FilesMatch directives.

<FilesMatch "\.php$">
    SetHandler application/x-httpd-php
<FilesMatch "\.phps$">
    SetHandler application/x-httpd-php-source
Alias /phpmyadmin "/usr/local/www/phpMyAdmin"
<Directory "/usr/local/www/phpMyAdmin">
Options None
AllowOverride None
Require all granted

Now restart Apache:

service apache24 restart

That’s it for our Apache configuration. Now let’s configure phpMyAdmin. We’ll do this by creating the file /usr/local/www/phpMyAdmin/, the basic configuration file for phpMyAdmin. Traditionally, users have manually created or modified /usr/local/www/phpMyAdmin/, but now phpMyAdmin includes a nice setup script, making it much easier to create this file with the settings you want. Start by creating the directory /usr/local/www/phpMyAdmin/config and make it writable by the phpMyAdmin setup script:

mkdir /usr/local/www/phpMyAdmin/config && chmod o+w /usr/local/www/phpMyAdmin/config

Then make /usr/local/www/phpMyAdmin/ readable by the phpMyAdmin setup script:

chmod o+r /usr/local/www/phpMyAdmin/

Now open your web browser and navigate to http://your-hostname-or-IP-address/phpmyadmin/setup where you will see the phpMyAdmin setup Overview page. Select “New server” and then select the “Authentication” tab. Under the “Authentication type” choose “http” from the drop-down list (using HTTP-Auth to sign-in into phpMyAdmin will avoid storing login/password credentials directly in and remove “root” from the “User for config auth”(See Figure 1).

phpMyAdmin setup page
Figure 1. Screenshot of the phpMyAdmin setup page.

Now select “Apply” and you will be returned you to the Overview page where you should see a new server listed. Select “Save” in the Overview page to save your configuration as /usr/local/www/phpMyAdmin/config/ Now let’s move that file up one directory to /usr/local/www/phpMyAdmin where phpMyAdmin can make use of it.

mv /usr/local/www/phpMyAdmin/config/ /usr/local/www/phpMyAdmin

Now let’s try out phpMyAdmin to make sure it works. Point your web browser to http://your-hostname-or-IP-address/phpmyadmin where you will be presented with a pop-up box requesting you to log in. Use “root” and the MySQL password you set up previously, then you should be directed to the phpMyAdmin administration page. We no longer need the /usr/local/www/phpMyAdmin/config directory so let’s remove it, as well as the read permission we added previously to /usr/local/www/phpMyAdmin/

rm -r /usr/local/www/phpMyAdmin/config
chmod o-r /usr/local/www/phpMyAdmin/

And wrap up by restarting the Apache and MySQL servers:

service apache24 restart
service mysql-server restart

Testing our installation using WordPress

WordPress is a full-featured website/blog platform that makes heavy use of Apache, MySQL and PHP. We’ll install it on our newly created implementation to ensure we have these packages installed and working correctly. Once again, all commands are issued as the root user or by simulating the root user using the command su. Let’s start by downloading the latest WordPress directly from the developers site to your home directory and untarring the package:

cd ~
tar -zxvf latest.tar.gz

You should now see the new directory wordpress in your home directory. Next we need to create the file ~/wordpress/wp-config.php and make some changes to it so WordPress can access the MySQL server. First, let’s copy the file ~/wordpress/wp-config-sample.php to use as a template:

cp ~/wordpress/wp-config-sample.php ~/wordpress/wp-config.php

Open ~/wordpress/wp-config.php in your favorite editor and enter the database name as well as your MySQL login and password in the appropriate lines. When complete, it should look like the following:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'root');
/** MySQL database password */
define('DB_PASSWORD', 'your-mysql-password');

Now move the wordpress directory to Apache’s document root:

mv ~/wordpress/ /usr/local/www/apache24/data/

Next, let’s create an MySQL database for our WordPress installation. Open phpMyAdmin in your browser and create a database by selecting the “Databases” tab at the top. Enter a name for it in the “Create database” field. For purposes of our example, I’ll use “wordpress” as the database name. You’re free to use a different database name, just make sure to use the same name in the define(‘DB_NAME’, ‘your-DB-name’); line in ~/wordpress/wp-config.php as described above. Then select “Create.”

Now let’s run the WordPress script that will populate its database with the requisite tables. Open your web browser and navigate to http://your-hostname-or-IP-address/wordpress/wp-admin/install.php. If everything is configured correctly you should see the WordPress installation wizard page (See Figure 2). Enter a title for your site, username, password, and an e-mail, then select “Install WordPress.” Then login with your newly created WordPress credentials and you should be presented with the default WordPress administration page.

WordPress installation page
Figure 2. Screenshot of WordPress installation page.

Common problems

An error that occasionally pops up when attempting to start the Apache server is the following:

httpd: apr_sockaddr_info_get() failed for <some-host-name>
httpd: Could not reliably determine the server's fully qualified domain name, using for ServerName
/usr/local/etc/rc.d/apache24: WARNING: failed to start apache24

This can be fixed by adding the following line to /usr/local/etc/apache24/httpd.conf:

ServerName localhost:80

HTTP 403 permission problems when trying to access phpMyAdmin is another area that seems to sometimes trip people up. This is usually caused by errors in the way either the Alias or Directory Apache directives for phpMyAdmin have been written in /usr/local/etc/apache24/httpd.conf. As an example, a missing “/” in the Alias statement cost me two hours of troubleshooting time!


Well, that’s it. A few hours of your time with the FreeBSD Ports Collection and you can a get a fully configured “FAMP” web development server up and running on your FreeBSD box.

Categories: Linux, Wordpress Tags:

Backup WordPress Database Using PHPMyAdmin

March 27th, 2010 No comments
Backup WordPress Database
Categories: Tips and Tricks, Wordpress Tags:

Creat an “Archives” Page in Your WordPress Blog

July 18th, 2009 No comments

[ Note: You need to have “Exec-PHP” plugin activated in order for the code in this post to work. ]

I planned to create an “Archives” page in my blog. On this “Archives” page, I wanted to list all my posts and organize them in categories. In each category, posts would be sorted by date.

Obviously I needed to write some PHP code and use a few WordPress functions to achieve this goal. The first step was to retrieve all the categories from my blog. This was done using get_categories() function:

$categories = get_categories('orderby=name');
foreach ($categories as $cat)
    echo $cat->cat_name;
    echo ' (';
    echo $cat->category_count;
    echo ')';
    echo '<br />';

For every category retrieved from my blog, I needed to get all the posts in that category and list them in a table. To do this, I needed to create my own “Loop” in my code. There is a post on talking about creating customized “Loop” in three ways. The first two methods, which involve using query_posts() and get_posts(), did not work for me. When I tried them, for some reason, the_permalink() and the_title() functions did not return the permalinks and the titles of the posts. Instead, they only returned the permalink and the title of the page -:( The third method involves using WP_Query class, which worked nicely for me. There is another good article on WP_Query.

To create a customized “Loop”, I made an instance of the WP_Query class and did it from scratch:

$my_query = new WP_Query($query_string);
if ($my_query->have_posts())
    while ($my_query->have_posts())
        echo the_permalink();
        echo the_title();

After adding some HTML formatting tags and putting all together, I got the complete source code for my “Archives” page:


Daylight Saving Time in WordPress 2.8

July 17th, 2009 No comments

Starting from version 2.8, WordPress is capable of adjusting for daylight saving time automatically. In order for WordPress to do this, you have to choose a nearest city in the WordPress timezone setting. Go to “Dashboard -> Settings -> General”. Choose the city that is closest to you in the “Timezone” drop-down menu. In my case, I chose “Los Angeles” since I’m located in California.

Figure 1. If you choose a city from Timezone drop-down menu, WordPress will automatically adjust for daylight saving time.

One thing to note is that you have to choose a city from the drop-down menu. If you choose a “Manual Offset” timezone, say, “UTC -8:00”, WordPress would not be able to adjust for daylight saving time automatically, as shown in the following figure:

Figure 2. If you choose a “Manual Offset” timezone, WordPress will NOT automatically adjust for daylight saving time.


July 11th, 2009 No comments

前两天我在博客里提到我把自己的 Blog 搬家,顺便把wordpress从1.5版本升级到了2.8.1版本。升级的过程并不是一帆风顺,其间遇到了一些数据库方面的问题。

我最初的计划是按照 Yan’s Five Easy Steps 将Blog移植到新主机上。本来Yan的方法简单易懂,按照他的步骤应该很快就能搞定。可是偏偏在第五步,也就是在 phpMyAdmin 中将旧数据库的数据导入到新数据库的时候出问题了。phpMyAdmin 报错,然后提示导入失败。我试了好几次都是同样的结果(后来我想了一下,很有可能 wordpress 在1.5版本之后修改了数据库结构,使得旧数据库的数据无法导入到新数据库)。万般无奈之下,我只好求其次,使用 wordpress 自带的导入/导出功能把旧帖子搬了过来。这个方法似乎解决了问题,因为在我粗略浏览了一下 Blog 后并没有发现什么问题。

但是第二天当我在 wordpress 的 Dashboard 里面进行一些维护工作时发现了一些问题。

  1. 首先是 Dashboard 显示的帖子总数不对,如图一所示。
  2. 从图中可以看到,在 Dashboard 中显示的我的 Blog 帖子总数为105,发表了的帖子数为35。事实上我的 Blog 一共只有35个帖子,一个不多一个不少。那么这凭空多出来的70个帖子是从哪里来的呢?


    经过仔细检查,我用 phpMyAdmin 发现了 wordpress 数据库的问题。图二显示的是 wordpress 的 wp_posts 表的一个截图。从图中可以看到 wp_posts 中包含有两个 fields:post_statuspost_type。而从表一中可以看到 wordpress 的1.5版本和2.8.1版本对post、page、和 attachment 的处理是不一样的。



      WordPress 1.5版本 WordPress 2.8.1版本
    post_status post_type post_status post_type
    post (published) publish post publish post
    page static post publish page
    attachment attachment post inherit attachment

    在1.5版本中,post、page 和 attachment 对应的 post_type 都是 post,区分它们的是 post_status。而在2.8.1版本中,post、page 和 attachment 对应的 post_type 各不相同,相应的 post_status 也有少许区别。明白了这点,再回头来看上面的帖子总数问题就容易理解了。原来当 wordpress 导入旧 Blog 的时候沿用了1.5版本的数据,导致所有的post、page 和 attachment 都被 wordpress 认为是 post。因此在 Dashboard 里面显示的帖子总数比实际帖子数多。

    为了改正这个错误,需要在 phpMyAdmin 输入 SQL 命令对 wordpress 数据库进行修改。首先我们要把 attachment 的 post_typepost 改成 attachment,把 post_statusattachment 改成 inherit。相应的 SQL 命令是:

    UPDATE wp_posts SET post_type="attachment" WHERE post_status="attachment";
    UPDATE wp_posts SET post_status="inherit" WHERE post_type="attachment";

    接着我们要把 page 的 post_typepost 改成 page,把 post_statusstatic 改成 publish。相应的 SQL 命令是:

    UPDATE wp_posts SET post_type="page" WHERE post_status="static";
    UPDATE wp_posts SET post_status="publish" WHERE post_type="page";

  3. wp_posts 表中 guid 一栏的网址都是 Blog 移植前的网址,如图二所示。
  4. 我的新 Blog 的地址是 “”,而 guid 栏里显示的是我的旧 Blog 的网址:””,因此需要把 guid 里的地址改正过来。相应的 SQL 命令是:

    UPDATE wp_posts SET guid=REPLACE(guid, '', '');

    图三、wp_posts 表里的 guid 显示的是旧 Blog 的地址。

  5. wp_posts 表里的 post_parent 栏里的很多数值都是错误的。
  6. 这个问题直接导致在 Dashboard 里面显示 Media 的时候,每个附件和其对应的帖子完全弄混。解决这个问题没有好办法,只有手工修改每一个错误的数值。

Change Page Orders in WordPress

July 8th, 2009 No comments

Directly copied from WordPress support page:

Pages are usually ordered alphabetically, but you can change the order pages are displayed in by using the order field.

Let’s say you have three pages: Animals, Machines, WordPress.

That is the order they will normally appear in. If you wanted WordPress to appear first, you need to do the following:

1. Click Pages –> Edit on the menu.
2. Find the page WordPress and click the title.
3. Find the Attributes module.
4. Put the number 1 in the text field for Order. This tells WordPress to make this the first page in the list.
5. Click the Update Page button.

Repeat the process for your other pages, but use higher numbers for the Order field: 2, 3, etc. This tells WordPress to make these the second and third items in the list.

If you are using the Pages Widget, you will want to edit the widget, click on the Sort By dropdown, and change it to Page Order. Then click the Done button on the widget options box and finally, click the Save Changes button.

Exec-PHP Plugin for WordPress

July 8th, 2009 No comments

In order to execute PHP code in your wordpress post, you will need a plugin that does that for you. I am using Sören Weber’s Exec-PHP plugin.


To install the Exec-PHP plugin:

  1. Download the Exec-PHP plugin archive and extract the files.
  2. Copy the resulting exec-php directory into /wp-content/plugins/ directory.
  3. Activate the plugin through the “Plugins” menu of WordPress (after a fresh installation only the Administrator is allowed to execute PHP code).
  4. Configure blog and user settings if needed.


With Exec-PHP you can execute PHP code in the excerpt and the content portion of your posts and pages (in the following called articles) as also as in text widgets. To execute code, just type in the PHP code as you usually would, encapsulated in <?php ?> tags.

What if I just want to print out PHP code and don’t execute it?

If you just want to print out code and don’t want to execute it, you have to make sure to convert your code to the correct XHTML representation. To do so you have to escape the following characters: < to &lt;, > to &gt;, and & to &amp;.

Categories: Wordpress Tags: , ,

Image Path Broken after Changing Permalinks in WordPress

July 8th, 2009 No comments

The Problem

It is common to use relative image paths in your wordpress post. If you put the image files in the images folder of the root directory. Then the path to, say, logo.jpg is coded as

<img src="/images/logo.jpg" />

This works until you change your permalinks. Permalinks change the url to reflect a directory stucture in place of the ugly '?page_id=127', thus breaking the path to images folder.

The Hack

Where you have used relative image paths, replace it with

<img src="<?php echo get_option('siteurl'); ?>/images/logo.jpg" />

This uses your specific siteurl as a prefix to the path of the image.

Hope this helps, it is a great hack.