PHP 5 safe_mode

Note Note: There is a parameter to index.php called safemode that is completely unrelated. See Help:Locating broken scripts for more about using that parameter.

PHP safe_mode was DEPRECATED as of PHP 5.3.0 and REMOVED on PHP 5.4.0

Safe mode criticism

PHP's safe_mode is an ill-conceived, broken-by-design setting in PHP that is supposed to make broken scripts safe. It was deprecated in PHP 5.3 and removed in PHP 5.4 (see the PHP documentation). MediaWiki can run with safe_mode enabled, but many of the advanced features will not work or need additional configuration.

To turn off safe mode, put this into your php.ini file:

  safe_mode = Off

If you cannot turn safe_mode off (such is the case with many shared webhosts), please consider finding a new host. If that is out of the question, read the next section for methods to enable some things with safe_mode on. It is recommended that if you do have access to php.ini, that you turn safe_mode off instead of using the methods described below.

Another opinion

Safe mode is really a bad solution. Default MediaWiki installs are generally safe and do not require safe_mode to be enabled, but if you are really paranoid, you still can set up full-featured wiki with some effort. However, you should not do this.

Making your wiki work in safe_mode

Image upload and deletion workaround

To make image uploads and deletions work in your wiki, you need to create the images/archive, images/temp, images/deleted, and images/thumb directories and make them all writable by your webserver (chmod 777). Then, set the following in your LocalSettings.php file:

$wgEnableUploads = true;
$wgHashedUploadDirectory = false;
$wgFileStore['deleted']['hash'] = 0;

Fixing errors with putenv() function

Sometimes you will see errors that putenv() can't work properly because of safe mode. It doesn't affect any functionality but looks rather bad for user. To remove errors, you can either comment lines in wiki source code (not recommended), or disable error reporting in PHP by setting the following at the top of your LocalSettings.php file

error_reporting(0); #Disable the reporting of errors
ini_set('display_errors', false); #same as above, need more permissions, usually disabled function

Setting time zone in safe mode

In safe mode, things like

$wgLocaltimezone = "America/New_York";
$oldtz = getenv("TZ");
putenv("TZ=$wgLocaltimezone");

Won't work. Instead, you should use the following code (place it in your LocalSettings.php file)

$wgLocaltimezone = 'CET';
$wgDefaultUserOptions['timecorrection'] = '04:00';//time offset +4, adjust accordingly to whatever timezone you wish

Maintenance scripts (creating sitemap, etc)

You won't be able to run maintenance mediawiki scripts in safe mode. But there are several workarounds. For example, even if you can't generate sitemap using shell scripts, you still can use specialized extension (for example, Extension:ManualSitemap‎).

Other issues

Thumbnails are the only thing which still shouldn't work in wiki after you changed everything above. There are several options for enabling thumbnails. For example, you can generate thumbnails on every client request, instead of generating and writing them to thumbs directory. To do it, place the following in LocalSettings.php

$wgThumbnailScriptPath = "{$wgScriptPath}/thumb.php";

If you want other solutions to this issue, please read the article linked to in the bottom of the page.


Fix for thumbnails

Upgrade file includes/filerepo/File.php, change:

	function getThumbRel( $suffix = false ) {
		$path = 'thumb';// /' . $this->getRel(); //[JS] This fix it.
		if ( $suffix !== false ) {
			$path .= '/' . $suffix;
		}
		return $path;
	}

and

	function getThumbUrl( $suffix = false ) {
		$path = $this->repo->getZoneUrl('public') . '/thumb'; // /' . $this->getUrlRel();//[JS] This fix URLs
		if ( $suffix !== false ) {
			$path .= '/' . rawurlencode( $suffix );
		}
		return $path;
	}

Make sure directories images and images/thumb are writable.

  • Guide to getting your wiki work in safe_mode - Some of the methods described may not work in newer versions of MediaWiki and/or require hacks to the core files (which get lost during upgrades). Also there is a method with changing safe mod settings so that all wiki will work fine.