WordPress: Difference between revisions

From NixOS Wiki
imported>Onny
mNo edit summary
imported>Onny
Update information on wordpressPackages
Line 3: Line 3:
== Installation ==
== Installation ==


{{Note|Parts of this instruction and module are not yet upstreamed and still being reviewed as an open PR.}}
{{Note|Parts of this instruction and module are not yet stable and will be available in the upcoming NixOS 22.11 release.}}


A simple local setup of Wordpress can be enabled with the following setup
A simple local setup of Wordpress can be enabled with the following setup
Line 19: Line 19:
=== Language ===
=== Language ===


The default language of the Wordpress module will be English. It is possible to package additional languages and make them available for your specific instance. Using <code>extraConfig</code> you can configure the default language. In this example, we're going to package and enable the German language.
The default language of the Wordpress module will be English. It is possible to enable additional language support for languages which are [https://search.nixos.org/packages?channel=unstable&from=0&size=50&sort=relevance&type=packages&query=wordpressPackages.languages already packaged]. Using <code>extraConfig</code> you can configure the default language. In this example, we're going to enable the German language.


<syntaxHighlight lang="nix">
services.wordpress.sites."localhost" = {
  languages = [ pkgs.wordpressPackages.languages.de_DE ];
  virtualHost.adminAddr = "hello@example.org";
  extraConfig = ''
    define ('WPLANG', 'de_DE');
  '';
};
</syntaxHighlight>
Alternatively you can package your own language files following this example:
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
{ pkgs, ... }: let
{ pkgs, ... }: let


   language-de = pkgs.stdenv.mkDerivation {
   wordpress-language-de = pkgs.stdenv.mkDerivation {
     name = "wordpress-${pkgs.wordpress.version}-language-de";
     name = "wordpress-${pkgs.wordpress.version}-language-de";
     src = pkgs.fetchurl {
     src = pkgs.fetchurl {
Line 35: Line 46:
in {
in {


   services.wordpress.sites."localhost" = {
   services.wordpress.sites."localhost".languages = [ wordpress-language-de ];
    languages = [ language-de ];
    virtualHost.adminAddr = "hello@example.org";
    extraConfig = ''
      define ('WPLANG', 'de_DE');
    '';
  };


}
}
Line 48: Line 53:
Consult the [https://translate.wordpress.org translation portal] of Wordpress for the specific country and language codes available. This example is using the code <code>de_DE</code> (Germany/German) in the source URL and also the <code>extraConfig</code> part.
Consult the [https://translate.wordpress.org translation portal] of Wordpress for the specific country and language codes available. This example is using the code <code>de_DE</code> (Germany/German) in the source URL and also the <code>extraConfig</code> part.


=== Themes ===
=== Themes and plugins ===


Unfortunately themes and plugins [https://github.com/NixOS/nixpkgs/pull/173622 haven't been packaged yet in nixpkgs]. In case you want to package and add custom themes yourself, the configuration could look like this
Themes and plugins which are [https://search.nixos.org/packages?channel=unstable&from=0&size=50&sort=relevance&type=packages&query=wordpressPackages already packaged] can be integrated like this:
<syntaxHighlight lang="nix">
services.wordpress.sites."localhost" = {
  themes = [ pkgs.wordpressPackages.themes.twentytwentytwo ];
  plugins = with pkgs.wordpressPackages.plugins; [
    antispam-bee
    opengraph
  ];
  virtualHost.adminAddr = "hello@example.org";
};
</syntaxHighlight>
 
Manually package a Wordpress theme or plugin can be accomplished like this:


<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
let
let


responsive = pkgs.stdenv.mkDerivation {
wordpress-plugin-responsive = pkgs.stdenv.mkDerivation {
   name = "responsive";
   name = "responsive";
   src = pkgs.fetchurl {
   src = pkgs.fetchurl {
Line 68: Line 85:


   services.wordpress.sites."localhost" = {
   services.wordpress.sites."localhost" = {
     themes = [ responsive ];
     themes = [ wordpress-plugin-responsive ];
     virtualHost.adminAddr = "hello@example.org";
     virtualHost.adminAddr = "hello@example.org";
   };
   };
Line 75: Line 92:
</syntaxHighlight>
</syntaxHighlight>


You can package any available Wordpress theme, for example from the [https://wordpress.org/themes official themes repository]. Be sure to replace the ''name'', url and ''sha256'' part according to your desired theme.
You can package any available Wordpress extension, for example from the [https://wordpress.org/themes official theme] or [https://wordpress.org/plugins plugin repository]. Be sure to replace the ''name'', url and ''sha256'' part according to your desired theme.


If you want to automatically enable the ''responsive'' theme, add this </code>extraConfig</code> line
If you want to automatically enable the ''responsive'' theme, add this <code>extraConfig</code> line


<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
Line 84: Line 101:
   define ('WP_DEFAULT_THEME', 'responsive');
   define ('WP_DEFAULT_THEME', 'responsive');
'';
'';
</syntaxHighlight>
=== Plugins ===
Packaging plugins, for example from the [https://wordpress.org/plugins official plugins repository], is similar to the approach used above
<syntaxHighlight lang="nix">
let
akismet = pkgs.stdenv.mkDerivation {
  name = "akismet";
  src = pkgs.fetchurl {
    url = https://downloads.wordpress.org/plugin/akismet.3.1.zip;
    sha256 = "1i4k7qyzna08822ncaz5l00wwxkwcdg4j9h3z2g0ay23q640pclg";
  };
  buildInputs = [ pkgs.unzip ];
  installPhase = "mkdir -p $out; cp -R * $out/";
};
in {
  services.wordpress.sites."localhost" = {
    plugins = [ akismet ];
    virtualHost.adminAddr = "hello@example.org";
  };
}
</syntaxHighlight>
</syntaxHighlight>



Revision as of 20:53, 6 October 2022

Wordpress is a self-hosted content management web application, especially designed for blogging but also a good start to create your own website. It can customized with themes and an in-build site editor and further extended with plugins.

Installation

Note: Parts of this instruction and module are not yet stable and will be available in the upcoming NixOS 22.11 release.

A simple local setup of Wordpress can be enabled with the following setup

services.wordpress.sites."localhost" = {
  virtualHost.adminAddr = "hello@example.org";
};

Visit http://localhost to setup your new Wordpress instance. By default, a Mysql server is configured automatically so you won't have to setup the database backend.

Configuration

Language

The default language of the Wordpress module will be English. It is possible to enable additional language support for languages which are already packaged. Using extraConfig you can configure the default language. In this example, we're going to enable the German language.

services.wordpress.sites."localhost" = {
  languages = [ pkgs.wordpressPackages.languages.de_DE ];
  virtualHost.adminAddr = "hello@example.org";
  extraConfig = ''
    define ('WPLANG', 'de_DE');
  '';
};

Alternatively you can package your own language files following this example:

{ pkgs, ... }: let

  wordpress-language-de = pkgs.stdenv.mkDerivation {
    name = "wordpress-${pkgs.wordpress.version}-language-de";
    src = pkgs.fetchurl {
      url = "https://de.wordpress.org/wordpress-${pkgs.wordpress.version}-de_DE.tar.gz";
      sha256 = "sha256-dlas0rXTSV4JAl8f/UyMbig57yURRYRhTMtJwF9g8h0=";
    };
    installPhase = "mkdir -p $out; cp -r ./wp-content/languages/* $out/";
  };

in {

  services.wordpress.sites."localhost".languages = [ wordpress-language-de ];

}

Consult the translation portal of Wordpress for the specific country and language codes available. This example is using the code de_DE (Germany/German) in the source URL and also the extraConfig part.

Themes and plugins

Themes and plugins which are already packaged can be integrated like this:

services.wordpress.sites."localhost" = {
  themes = [ pkgs.wordpressPackages.themes.twentytwentytwo ];
  plugins = with pkgs.wordpressPackages.plugins; [
    antispam-bee
    opengraph
  ];
  virtualHost.adminAddr = "hello@example.org";
};

Manually package a Wordpress theme or plugin can be accomplished like this:

let

wordpress-plugin-responsive = pkgs.stdenv.mkDerivation {
  name = "responsive";
  src = pkgs.fetchurl {
    url = http://wordpress.org/themes/download/responsive.1.9.7.6.zip;
    sha256 = "06i26xlc5kdnx903b1gfvnysx49fb4kh4pixn89qii3a30fgd8r8";
  };
  buildInputs = [ pkgs.unzip ];
  installPhase = "mkdir -p $out; cp -R * $out/";
};

in {

  services.wordpress.sites."localhost" = {
    themes = [ wordpress-plugin-responsive ];
    virtualHost.adminAddr = "hello@example.org";
  };

}

You can package any available Wordpress extension, for example from the official theme or plugin repository. Be sure to replace the name, url and sha256 part according to your desired theme.

If you want to automatically enable the responsive theme, add this extraConfig line

extraConfig = ''
  // Activate and enable theme responsive as default
  define ('WP_DEFAULT_THEME', 'responsive');
'';

In case you want to automatically enable the plugin, in this example akismet, you can add following to extraConfig

extraConfig = ''
  // Activate and enable theme responsive as default
  if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');
  require_once(ABSPATH . 'wp-settings.php');
  require_once ABSPATH . 'wp-admin/includes/plugin.php';
  activate_plugin( 'akismet/akismet.php' );
'';

Tips and tricks

Enable SSL behind reverse proxy

In case you're running Wordpress behind a reverse proxy which offers a SSL/https connection to the outside, you can force Wordpress to use the https protocol

services.wordpress.sites."localhost".extraConfig = ''
  // Needed to run behind reverse proxy
  define('FORCE_SSL_ADMIN', true);
  $_SERVER['HTTPS']='on';
'';

Troubleshooting

Enable logging

To enable logging add the following lines to extraConfig

services.wordpress.sites."localhost".extraConfig = ''
  define( 'WP_DEBUG', true );
  define( 'WP_DEBUG_LOG', true );
  ini_set( 'error_log', '/var/lib/wordpress/localhost/debug.log' );
'';

Since the default location to the folder wp-content is not writable, we redirect the log file path to /var/lib/wordpress/localhost/debug.log. All error messages will be stored there. Change the folder name localhost to the name of your site.

In case you want to print error messages directly in your browser, append following line

services.wordpress.sites."localhost".extraConfig = ''
  @ini_set( 'display_errors', 1 );
'';

Please note that this exposes sensible information about your server setup therefore this option should not be enabled in production.