Build a website using wordpress based on IIS under Windows Server

Article directory

  • Preface
  • 1. Components required to build a website in WordPress
  • 2. Component installation and configuration
    • 1. Enable IIS
    • 2. Install and configure php
    • 3. Install and configure MySQL
    • 4. Install and configure wordpress
  • 3. Analysis of error prompts and causes encountered

Foreword

When I was learning wordpress before, because I was not familiar with Linux, my original plan was to use windows server + IIS, but I encountered a 500 error halfway through and read a lot of information but couldn’t solve it, so I finally used Linux. After that, I had a better understanding of the entire system. I gained some understanding, resumed the original plan, and encountered some new problems. After some troubles, I gained a lot of new understandings.
Take notes.

1. Components required to build a website with WordPress

In the Linux-based notes, 6 items are summarized.
https://blog.csdn.net/m0_54284125/article/details/134346019
If you think about it carefully, it is actually more appropriate to combine them into 5 items:

  1. Server operating system
    This is the basis, all software must run on the operating system.

  2. Web server software
    The most common ones are IIS, Apach, and Nginx. IIS is mainly used with Windows Server.

  3. php program
    If I don’t explain it, I don’t even understand PHP, so what kind of website is this?

The above three things are the most basic. With these, you can build a website. WordPress is just a convenience tool

  1. Database software
    The reason why database software is needed is because WordPress is used to develop websites. The text content (including configuration content) in the web page is actually stored in the database, not in the server file directory.
    There are many choices, but the most commonly used ones are Mysql and Mariadb, which are open source.
    The database management software can be installed or not. In fact, it is not used very much. I only used it to explore the information architecture of WordPress. The most mentioned in various tutorials on the Internet is phpMyAdmin. The advantage is that it is a web version, which is simple and easy to understand. The disadvantage is that it must be installed on the server. You can also use other conventional database management software, such as DBeaver, the world’s number one.
  2. wordpress itself

2. Component installation and configuration

The general feeling is that configuring wordpress on Windows Server + IIS is much more complicated than on Linux + Apach.
Let’s first list the most critical minefields of summary:

  1. Be sure to check the CGI option when installing IIS
  2. The versions of each component should be chosen carefully, especially the PHP version.
    It is recommended to check the compatibility requirements on the WordPress official website before installation:
    WordPress Compatibility
    I encountered the problem of installing php8.2. When testing php, I couldn’t pass it, so I changed to 8.0 to solve the problem.
  3. “Module mapping” must be configured in IIS to map *.php to CgiModule (executable file php-cgi.exe)
  4. In IIS, you must set the “default document” to index.php
    Otherwise, when entering something like http://host/wp-admin/, a 403 error will be prompted. You may be prompted to activate the “Directory Browsing” function, which is not what you want.
  5. After installing the database software and creating a database and user for wordpress, be sure to remember to set the permissions of the database for this user.
    Otherwise, you will encounter a 500 error. I was stuck here and couldn’t solve it.
  6. After unzipping wordpress to the web root directory (assuming the normal installation method), be sure to set the web directory permissions
    Windows Server + IIS, the default web root directory is c:\interpub\wwwroot
    If permissions are not set, it is likely that the theme cannot be installed and media files cannot be uploaded.
  7. Set temporary file directory
    When uploading media files, you need to use a temporary directory. The default on Windows is C:\Windows\Temp. This is not recommended.
    It is best to specify one specifically for WordPress in php.ini, similar to upload_tmp_dir = C:/inetpub/wwwroot/wp-content/temp. If you want to use the system’s default temporary directory, you must add write permission to user ISUR.

1. Enable IIS

I use Tencent Cloud Lightweight Application Server, and the Windows Server version is 2012 R2 Datacentere. The interface of other versions may be different, but the method is similar.
“Control Panel” –> “Programs and Features” –> “Turn Windows features on or off”
The Add Roles and Features Wizard of Server Manager will start.
Select “Web Server (IIS)”, the most important thing is to check CGI under “Application Development”

After the installation is completed, you can test whether it is successful by entering http://ip (or domain)/ on the client browser.
Or enter http://localhost/ in the server’s browser
If you can see the page below, it means IIS is working normally.
The principle behind it is very simple. There is an iisstart.htm file under the IIS web root directory (C:\inetpub\wwwroot if no special settings are made). This page displays the contents of this file

After success, you can start IIS Manager and take a look. I found that its version is 6.2, which is relatively old.

Next, further set the access permissions of the wwwroot folder.
Right-click on the c:\inetpub\wwwroot directory, select “Security”, and add “Modify” permissions to Users and IIS_IUSRS. The IUSE and IIS_IUSRS groups are the built-in user names and user groups of IIS. You can refer to Microsoft official documentation:
Default permissions and user permissions for IIS 7.0 and later
Roughly equivalent to Apach’s apach:apach user group and username

2. Install and configure php

  1. download
    PHP download address: https://windows.php.net/download
    Be careful to download the non-thread-safe version, 64bit (32bit operating systems are no longer common)
    Pay more attention to the version you download and make sure it is compatible with the WordPress version.

  2. Install
    PHP requires no installation. The so-called installation is just decompression. Unzip the downloaded php file, such as c:\php

  3. Download and install Visual C++ Redistributable (C++ application runtime component package)
    When downloading php, you will notice that the official website says “More recent versions of PHP are built with VC15 or VS16 (Visual Studio 2017 or 2019 compiler respectively) and include improvements in performance and stability. The VC15 and VS16 builds require to have the Visual C++ Redistributable for Visual Studio 2015-2019 x64 or x86 installed”. Therefore, you need to download and install Visual C++ Redistributable for Visual Studio 2015-2019 x64 or x86. There is a download link on the official website.

  4. Configure php.ini
    Rename php.ini-development or php.ini-production in the root directory of the php folder to php.ini
    Then open php.ini and modify the following options. These options are preceded by ; in the original file. Comment them out and remove the semicolon. If the directory path is set, set it according to the actual path of your php. The first 5 are key, and the following extensions can be enabled depending on the situation.

extension_dir = "ext"
cgi.force_redirect = 0
cgi.fix_pathinfo = 1
fastcgi_impersonate=1
upload_tmp_dir = C:/inetpub/wwwroot/wp-content/temp
date.timezone = RPC
extension=curl
extension=gd2
extension=gmp
extension=imap
extension=mbstring
extension=mysqli
extension=openssl
extension=pdo_mysql
extension=soap
extension=sockets
  1. Configure “Module Mapping”

  2. Configure “Default Document”
    This is to prepare for WordPress, because WordPress uses PHP, and the default page is index.php. After adding index.php, you can delete the others.

  3. Configure php.ini update monitoring


  4. Configure environment variables
    Add the path to php to the system environment variable PATH

  5. Verify whether the php configuration is successful
    Create a php file, say info.php, and put the following code.

<?php
    phpinfo();
?>

After saving, open the file in your browser
http://localhost/info.php
If you see a page similar to the following, it means that PHP is successfully installed and running normally.

I referred to the following articles during the learning and practice process of IIS + php, thank them for sharing!
Windows IIS installation and configuration of php service
Install and deploy WordPress on Windows Server

3. Install and configure MySQL

When practicing on CentOS, I used MariaDB. As a result, I searched the official website for a long time on Windows but could not find the download link, so I went to the official Oracle website and downloaded MySQL 5.7. After downloading, I found that this needs to be installed manually.
I was having some trouble, but luckily I found a reliable article:
Window installation MySQL 5.7.31 installation-free version

To summarize the following key points:

  1. Create a directory data in the root directory of mysql (assumed to be c:\mysql)
    Don’t put any files in it after creating
  2. Create a configuration file my.ini and place it in the root directory of mysql
[client]
port=3306
default-character-set=utf8

[mysqld]
# Set to the installation directory of MYSQL
basedir = "C:/MySQL"
#Set as MYSQL data directory
datadir = "C:/MySQL/data"

port=3306
character_set_server=utf8
sql_mode=NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER

#Enable query cache
explicit_defaults_for_timestamp=true
  1. Set environment variables and add the mysql key directory
    Create 2 new user variables:
    MYSQL_HOME value: C:\MYSQL
    MYSQL_PATH value: %MYSQL_HOME%\bin
    Modify the user variable Path and add: %MYSQL_PATH%;%MYSQL_HOME%\lib;%MYSQL_HOME%\lib\plugin;
    Modify the system variable Path and add: %MYSQL_PATH%;%MYSQL_HOME%\lib;%MYSQL_HOME%\lib\plugin;

  2. Install mysql
    Start cmd as administrator
    (1) Installation
    mysqld -install
    If successful, it will prompt: Service successfully installed.
    (2) Initialization
    mysqld --initialize-insecure
    During execution, you can see that mysql is writing files to the newly created data directory.
    If there are already files in the data directory, it will terminate and prompt that there are files in the data directory and cannot proceed. At this time, you only need to delete all the files in the data directory and re-execute the command.
    (3) Start mysql
    net start mysql

  3. Change root user password
    After the initial installation, the root user does not have a password. Use mysql -u root -p to log in. When asked to enter the password, just press return.
    set password.

  4. Create a database and user for wordpress and assign the user the operation permissions of this database
    From here on, there is no difference from the installer installation situation.
    Special attention must be paid to assigning the operation permissions of the newly created database to the newly created user. Otherwise there will be a 500 error.

4. Install and configure wordpress

The installation configuration of wordpress is basically the same as that under Linux.

  1. download wordpress
    The latest version is always posted on the official website. If you want to download the specified version, you can go here:
    Release Archive – WordPress.org
  2. Unzip the downloaded wordpress to the web service root directory (the default is c:\inetpub\wwwroot)
  3. Modify the wp-config.php configuration file
    The original file name is wp-config-sample.php, copy it to wp-config.php
    Open the file and modify the database-related parts

    save.
  4. Open http://host/wp-admin/install.php
    The follow-up is the same.

3. Analysis of error prompts and causes encountered

  1. 403 error
    An error occurred when accessing from the client without a file name.
    Accessed from the server side, the error description is more detailed.

    The error description is easily misleading. According to its suggestion, the directory browsing function needs to be turned on. Of course not.
    The actual problem is: I forgot to change the default document to index.php when configuring PHP. As a result, IIS did not find a suitable default document under /wp-admin/, so it thought that the user wanted to browse the directory. It was solved after configuring the default document.

  2. 502 error
    I forgot to take a screenshot on the client side, and the server side browser displays the following. It is suspected that the Visual C++ Redistributable is incompatible with the php version. The error disappeared after switching to php8.0.
    Error messages on the client side are generally brief, but much clearer on the server side, so there is a problem. Reproducing it on the server side will help

    PS: Regarding the version problem, it may not be the root cause. Because I wanted to take a screenshot of a picture, I changed it back to php8.2. Miraculously, it actually works. To be further studied

  3. 500 error
    There are several different 500 errors

    This problem was solved after correctly configuring the mapping between the php file and php-cgi.exe in IIS according to the prompts.
    Another person forgot to take a screenshot, which was caused by forgetting to authorize the WordPress database user.

  4. Unable to upload media file
    This was also encountered on the Linux server. At that time, it was caused by forgetting to change the user of the /var/www/html/wp-content directory and its subordinate files to apach.
    The same problem also occurs on Windows Server, but it is much more difficult to solve.
    (1) No matter from the client or server browser, the prompt is very simple, I only know that an error has occurred.

    Based on previous experience, it is speculated that it is caused by file permissions. But I tried it and the theme can be installed normally, which means the folder permissions are not an issue. Try to delete the wp-content\uploads directory, and then check “Organize my uploads into month- and year-based folders” in the setting. I found that the uploads directory was automatically created, and the year and month directories were created. are also created automatically. It was further confirmed that it was not caused by directory permissions.
    When testing on the server-side browser, I found that WordPress provides a built-in browser function upload mode, similar to this: http://host/wp-admin/media-new.php?browser-uploader
    Using this is even worse, reporting a 500 error directly, but interestingly, in this mode, after switching to the multi-file uploader, it gives a useful error message: Missing a temporary folder.

    Now the direction is clear.
    I found a solution online, it’s a long one (original link), it actually means adding define('WP_TEMP_DIR', dirname(__FILE__) . '/ to wp-config.php) wp-content/temp/');, but it doesn’t work.
    It wasn’t until I read this article (original text) that I really understood the root cause of the problem: by default, the php process will read and write the system’s temporary directory (the default is C:/windows/temp for Windows and /tmp for Linux). Obviously, only php needs to be modified. This configuration in the .ini will do the trick:

    After modification, the actual test was successful.
    After understanding the principle, you will naturally think that if you do not change the php.ini configuration, it should also be possible to directly modify the access permissions of the system’s temporary directory (pure testing, this is definitely not a good idea in a formal environment).
    Actual measurements confirmed this idea.
    First comment out the modification of php.ini, then modify the permissions of c:\windows\temp, and increase the read and write permissions of the IUSR user. After restarting IIS, the upload can still be successful. Note that in actual testing, adding the user group IIS_IUSRS user group is invalid.