{"id":114,"date":"2024-03-05T03:54:33","date_gmt":"2024-03-05T03:54:33","guid":{"rendered":"https:\/\/blog.devops955.com\/swain\/?p=114"},"modified":"2024-03-05T03:54:33","modified_gmt":"2024-03-05T03:54:33","slug":"how-to-setup-wordpress-multisite-with-lemp-stack","status":"publish","type":"post","link":"https:\/\/blog.devops955.com\/swain\/2024\/03\/05\/how-to-setup-wordpress-multisite-with-lemp-stack\/","title":{"rendered":"How To Setup WordPress Multisite with LEMP Stack"},"content":{"rendered":"<blockquote>\n<p>Estimated reading time: 11 minutes<\/p>\n<\/blockquote>\n<h1>What is Multisite?<\/h1>\n<p>Previously, we completed the <strong><a href=\"https:\/\/blog.devops955.com\/swain\/2024\/02\/28\/step-by-step-guide-to-deploying-wordpress-with-lemp-stack\/\" title=\"deployment of WordPress based on LEMP stack\">deployment of WordPress based on LEMP stack<\/a><\/strong>. Next, we'll delve into the configuration of WordPress Multisite. So, what exactly is Multisite? Simply put, Multisite allows running multiple independent WordPress websites on a single server, meaning you can operate multiple websites using the same WordPress setup, with each site having its own content, users, and permissions. For example, after creating this Blog site, if my friends or I want to open registration to allow more people to use it, and they require their own independent Blog sites with distinct styles, we can enable Multisite mode.<\/p>\n<h2>Introduction to Multisite Mode<\/h2>\n<p>WordPress Multisite enables the control and management of multiple websites within a single WordPress installation. Once enabled, the Multisite network allows the addition of new sites, utilizing installed themes and plugins. Each site can have its own independent content and users, but they share the same set of WordPress core files, plugins, themes and other resources.<\/p>\n<p>The Multisite network is managed by <strong>Super Admins<\/strong> who have full control over all sites in the network, including adding and removing sites, installing plugins and themes, and executing updates across the entire network. Other <strong>Site Administrators<\/strong> in the network can only manage their own sites and cannot access other sites' data.<\/p>\n<h2>Types of Multisite Mode<\/h2>\n<p>WordPress Multisite currently supports two main types of Multisite configurations:<br \/>\n<strong>Subdirectory:<\/strong> This type of Multisite setup installs each website as a directory of the main site. For example, if your main site is <a href=\"https:\/\/example.com\">https:\/\/example.com<\/a>, then the subdirectory sites would be <a href=\"https:\/\/example.com\/site1\">https:\/\/example.com\/site1<\/a> and <a href=\"https:\/\/example.com\/site2\">https:\/\/example.com\/site2<\/a>. This method is relatively simple and does not require special DNS configuration, usually suitable for websites that do not need independent domain names.<br \/>\n<strong>Subdomain:<\/strong> In this setup, each website uses a subdomain instead of a subdirectory. Compared to subdirectory, subdomain sites would use <a href=\"https:\/\/site1.example.com\">https:\/\/site1.example.com<\/a> and <a href=\"https:\/\/site2.example.com\">https:\/\/site2.example.com<\/a>. This method, in addition to site configuration, requires additional DNS configuration, namely adding A records or CNAME records for each subdomain, but it makes each site appear more independent.<\/p>\n<p>My current site is using the subdirectory mode, so the following content will mainly introduce the configuration of subdirectory mode Multisite. I have not verified the feasibility of implementing the subdomain mode, so it is for reference only.<\/p>\n<h1>Subdirectory Mode Multisite<\/h1>\n<h2>Enabling WordPress Multisite<\/h2>\n<p>Starting from WordPress 4.9.0, the Multisite feature is default off, so you need to manually enable it. Otherwise, you will not see the Network Setup option.<\/p>\n<ol>\n<li>\n<p>Modify <strong>wp-config.php<\/strong>: Log into the server, enter the website's root directory, and edit the <em>wp-config.php<\/em> file <code>sudo vim \/var\/www\/blog.yourdomain.com\/wp-config.php<\/code>.<br \/>\nAt the bottom of the file, there's a comment line <code>\/* That&#039;s all, stop editing! Happy blogging. *\/<\/code>. Add the following code above this line, :<\/p>\n<pre><code class=\"language-php\">\/* Multisite *\/\ndefine(&#039;WP_ALLOW_MULTISITE&#039;, true);<\/code><\/pre>\n<p>Save and close the file, then go back to the website dashboard to start configuration.<\/p>\n<\/li>\n<li>\n<p>Network installation: Log into your WordPress dashboard, go to \u201cTools\u201d &gt; \u201cNetwork Setup\u201d. Choose \u201cSubdirectory\u201d as the network type. Then follow the steps on the screen to complete the setup. You will need to enter the network's name, admin username and password, and your network address, which is used for the entire network.<\/p>\n<\/li>\n<li>\n<p>Network configuration: After the installation, WordPress will provide a piece of code that needs to be added back to the <strong>wp-config.php<\/strong> file:<br \/>\n<img decoding=\"async\" src=\"https:\/\/blog.devops955.com\/swain\/wp-content\/uploads\/sites\/2\/2024\/03\/WP-Multisite-01.png\" alt=\"image01\" \/><\/p>\n<pre><code class=\"language-php\">define( &#039;MULTISITE&#039;, true );\ndefine( &#039;SUBDOMAIN_INSTALL&#039;, false );\n\/\/ Replace blog.example.com with your domain.\ndefine( &#039;DOMAIN_CURRENT_SITE&#039;, &#039;blog.example.com&#039; );  \ndefine( &#039;PATH_CURRENT_SITE&#039;, &#039;\/&#039; );\ndefine( &#039;SITE_ID_CURRENT_SITE&#039;, 1 );\ndefine( &#039;BLOG_ID_CURRENT_SITE&#039;, 1 );<\/code><\/pre>\n<\/li>\n<\/ol>\n<blockquote>\n<p>Since we use the LEMP Stack, there's no <strong>Apache .htaccess<\/strong> configuration file to easily complete rewriting. Therefore, after creating it, you need to write rules in the <strong>Nginx<\/strong> configuration file to rewrite subdirectories (because the subdirectory actually reads the resources and settings of the WordPress main site, it does not really create different subdirectories and copy a complete set of resources).<\/p>\n<\/blockquote>\n<h2>Adding Sites<\/h2>\n<p>Before configuring Nginx, let's first add a sub-site, so we can then configure Nginx for validation.<\/p>\n<ol>\n<li>\n<p>Adding a sub-site: Log into your WordPress dashboard, go to \u201cNetwork\u201d &gt; \u201cSites\u201d, then click \u201cAdd New Site\u201d.<\/p>\n<\/li>\n<li>\n<p>Fill in the sub-site information: Enter the sub-site's name, title, and admin email as required.<br \/>\nOnce filled, the new site will be <a href=\"https:\/\/blog.example.com\/site1\">https:\/\/blog.example.com\/site1<\/a>. Click \u201cAdd Site\u201d to finish.<br \/>\nAfter setting, you can enter the site and set a password for the administrator on the users' page. (Later, you can try creating a feature to generate a random password and send it to the administrator's email, to further simplify management.)<\/p>\n<\/li>\n<\/ol>\n<h2>Nginx Configuration<\/h2>\n<p>After adding the sub-site, we need to configure Nginx to enable access to the subdirectory; otherwise, you'll get a 404 error because this path does not actually exist.<\/p>\n<ol>\n<li>\n<p>Log into the server, edit the website's Nginx configuration file, located in the \/etc\/nginx\/sites-available\/ directory according to <strong><a href=\"https:\/\/blog.devops955.com\/swain\/2024\/02\/28\/step-by-step-guide-to-deploying-wordpress-with-lemp-stack\/\" title=\"the previous article\">the previous article<\/a><\/strong>:<\/p>\n<pre><code class=\"language-bash\">sudo vim \/etc\/nginx\/sites-available\/blog.your_domain.com<\/code><\/pre>\n<\/li>\n<li>\n<p>Within the <strong>server block<\/strong> in the configuration file, add the rewrite judgment and forward the subdirectory requests to the WordPress main site:<\/p>\n<pre><code class=\"language-nginx\">#Subdirectory rewrite rules\nrewrite ^\/wp\/([_0-9a-zA-Z-]+)\/(xmlrpc\\.php|wp-[0-9a-z-]+\\.php) \/wp\/$2;\nrewrite ^\/wp\/([_0-9a-zA-Z-]+)\/(wp-(admin|content|includes).*) \/wp\/$2;\nlocation \/ {\n    index index.php index.html;\n    try_files $uri $uri\/ \/index.php?$args ;\n}\n    rewrite \/wp-admin$ $scheme:\/\/$host$uri\/ permanent;  # Redirect subdirectory wp-admin to the main site wp-admin\n#Subdirectory rewrite rules: If the requested file does not exist, try to rewrite the URL according to the WordPress Multisite structure\nif (!-e $request_filename) {\nrewrite ^\/[_0-9a-zA-Z-]+(\/wp-.*) $1 last;\nrewrite ^\/[_0-9a-zA-Z-]+.*(\/wp-admin\/.*\\.php)$ $1 last;\nrewrite ^\/[_0-9a-zA-Z-]+(\/.*\\.php)$ $1 last;\n}\n####Another way: If there is a problem with the previous rule matching, you can try this one\n#if (!-e $request_filename) {\n#    rewrite \/wp-admin$ $scheme:\/\/$host$uri\/ permanent;\n#    rewrite ^\/[_0-9a-zA-Z-]+(\/wp-.*) \/wp$1 last;\n#    rewrite ^\/[_0-9a-zA-Z-]+(\/.*\\.php)$ \/wp$1 last;\n#}\n####\n#PHP handling part\nlocation ~ \\.php$ {\n    include snippets\/fastcgi-php.conf;\n    fastcgi_read_timeout 3600s;\n    fastcgi_pass unix:\/var\/run\/php\/php-fpm.sock;\n    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n    include fastcgi_params;\n}<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>After you reload your nginx process, you should have completed all the configurations for the Multisite subdirectory mode. If there are still issues, you can refer to the <strong><a href=\"https:\/\/developer.wordpress.org\/advanced-administration\/server\/web-server\/nginx\/\" title=\"official documentation\">official documentation<\/a><\/strong> and check and adjust according to your actual situation.<\/p>\n<h1>Subdomain Mode Multisite<\/h1>\n<blockquote>\n<p>This section is for those who need to enable subdomain sub-sites. If you have set up a subdirectory mode Multisite, this section can be ignored.<\/p>\n<\/blockquote>\n<p>The method to enable subdomain mode is basically the same as the subdirectory mode. You need to modify the <strong>wp-config.php<\/strong> file to enable the Multisite feature, choosing the subdomain option as your network type during setup. The network configuration steps and adding sites are similar to the previous ones, so I'll skip directly (assuming there should be no problem based on the wordpress setup guide).<\/p>\n<h2>Nginx Configuration<\/h2>\n<ol>\n<li>Modify the Nginx configuration file <code>sudo vim \/etc\/nginx\/sites-available\/blog.your_domain.com<\/code>, add the following content in the <strong>server block<\/strong>:\n<pre><code class=\"language-nginx\">server {\nserver_name blog.yourdomain.com *.blog.yourdomain.com;  # Capture all subdomains\nroot \/var\/www\/blog.yourdomain.com;\nindex index.php index.html index.htm;\nlocation \/ {\n    try_files $uri $uri\/ \/index.php?$args;\n}\nlocation ~ \\.php$ {\n    include snippets\/fastcgi-php.conf;\n    fastcgi_pass unix:\/var\/run\/php\/php-fpm.sock;\n    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n    include fastcgi_params;\n}\n}<\/code><\/pre>\n<\/li>\n<\/ol>\n<h2>DNS Record Configuration<\/h2>\n<p>Each subdomain needs to have the corresponding records set at your DNS provider to be correctly resolved and accessed. For each sub-site, you will need to add an A record or CNAME record:<\/p>\n<ol>\n<li>\n<p><strong>A Record<\/strong>: Point the subdomain to your server's IP address. For example, if your sub-site is <strong>site1.blog.yourdomain.com<\/strong>, you need to add an A record, directing site1 directly to your server's IP address.<\/p>\n<\/li>\n<li>\n<p><strong>CNAME Record<\/strong>: Point the subdomain to the main domain. This method is suitable if you do not want to directly expose the IP address or are using CDN services. For example, if your sub-site is <strong>site1.blog.yourdomain.com<\/strong>, you need to add a CNAME record, directing site1 to <strong>blog.yourdomain.com<\/strong>. (Note that some providers require entering just <strong>site1<\/strong> for the CNAME, while others require the full <strong>site1.blog.yourdomain.com<\/strong>).<\/p>\n<\/li>\n<\/ol>\n<blockquote>\n<p>If wildcard records are supported, you can also use a wildcard record to replace, for example, directly pointing *.blog.yourdomain.com to the server IP or creating a CNAME. This simplifies subsequent configurations as you won't need to create a record for each sub-site.<\/p>\n<\/blockquote>\n<h1>Conclusion<\/h1>\n<p>By following the steps above, you should have successfully configured a WordPress Multisite. WordPress offers powerful and extensive features, allowing users to create, manage, and operate multiple websites from a single WordPress installation. This capability provides unprecedented flexibility and control for website administrators, making the management of a large number of related websites more efficient and simpler. Of course, the specific choice of how to use depends on your planning. As a personal website, the more your site grows, the more effort you will need to invest. Whether to use these features needs to be considered comprehensively. Finally, let's briefly summarize.<\/p>\n<h2>Benefits of Multisite Setup<\/h2>\n<ol>\n<li>\n<p><strong>Centralized Management<\/strong>: A Multisite network makes updating, managing, and maintaining multiple websites much simpler. Administrators can update core WordPress files, plugins, and themes from one dashboard, meaning all sites can be updated at the same time.<\/p>\n<\/li>\n<li>\n<p><strong>Resource Sharing<\/strong>: In a Multisite network, all sites can use the available plugins and themes, reducing the need for duplicate resources. This not only saves space but also ensures brand and design consistency, while sub-sites can also make certain adjustments according to personal style.<\/p>\n<\/li>\n<li>\n<p><strong>User Management<\/strong>: Multisite allows Super Admins to create and manage users across the entire network. Users can be granted different permissions on one or more sites, making user management more centralized and efficient.<\/p>\n<\/li>\n<li>\n<p><strong>Cost-Effectiveness<\/strong>: By sharing hosting and resources, a Multisite network reduces the cost of running multiple independent websites. This is a significant advantage for businesses or organizations with multiple related websites, and a good start for individuals or startups.<\/p>\n<\/li>\n<\/ol>\n<h2>Challenges Faced<\/h2>\n<ol>\n<li>\n<p><strong>Increased Maintenance Work<\/strong>: Although a Multisite network simplifies many management tasks, Super Admins need to monitor and maintain the entire network, which can increase the workload. So be sure to determine your goals and be prepared before promoting.<\/p>\n<\/li>\n<li>\n<p><strong>More Complex Configuration<\/strong>: Setting up and managing a Multisite network is more complex than managing a single WordPress site. This includes the network setup, domain configuration, server configuration, and potential security issues.<\/p>\n<\/li>\n<li>\n<p><strong>Plugin and Theme Compatibility<\/strong>: Not all plugins and themes are compatible with a Multisite environment. This may limit the resources you can use and require additional time to test and adjust to ensure stability, after all, it's no longer just you using them.<\/p>\n<\/li>\n<li>\n<p><strong>Performance Considerations<\/strong>: Running multiple websites may put greater pressure on server resources. To maintain website performance, a more powerful server configuration or optimization strategies may be needed, which will also increase costs accordingly.<\/p>\n<\/li>\n<\/ol>\n<p>In conclusion, despite these challenges, WordPress Multisite remains a powerful platform. If you want to delve deeper into these contents, further study is possible. After overcoming the challenges in the middle, I believe this will greatly enhance your technical skills.<\/p>\n<blockquote>\n<p>Reference links:<br \/>\n<a href=\"https:\/\/www.gingerdoc.com\/tutorials\/how-to-set-up-wordpress-multisite-on-ubuntu-20-04\">https:\/\/www.gingerdoc.com\/tutorials\/how-to-set-up-wordpress-multisite-on-ubuntu-20-04<\/a><br \/>\n<a href=\"https:\/\/computingforgeeks.com\/how-to-setup-wordpress-multisite-network-nginx-letsencrypt\/\">https:\/\/computingforgeeks.com\/how-to-setup-wordpress-multisite-network-nginx-letsencrypt\/<\/a><br \/>\n<a href=\"https:\/\/developer.wordpress.org\/advanced-administration\/multisite\/\">https:\/\/developer.wordpress.org\/advanced-administration\/multisite\/<\/a><br \/>\n<a href=\"https:\/\/developer.wordpress.org\/advanced-administration\/server\/web-server\/nginx\/\">https:\/\/developer.wordpress.org\/advanced-administration\/server\/web-server\/nginx\/<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Estimated reading time: 11 minutes What is Multisite? Previously, we completed the deployment of WordPress based on LEMP stack. Next, we&#8217;ll delve into the configuration of WordPress Multisite. So, what exactly is Multisite? Simply put, Multisite allows running multiple independent WordPress websites on a single server, meaning you can operate multiple websites using the same&#8230;<\/p>\n","protected":false},"author":3,"featured_media":75,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_jetpack_memberships_contains_paid_content":false},"categories":[4],"tags":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/blog.devops955.com\/swain\/wp-content\/uploads\/sites\/2\/2024\/02\/wp.png","_links":{"self":[{"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/posts\/114"}],"collection":[{"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/comments?post=114"}],"version-history":[{"count":7,"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/posts\/114\/revisions"}],"predecessor-version":[{"id":192,"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/posts\/114\/revisions\/192"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/media\/75"}],"wp:attachment":[{"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/media?parent=114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/categories?post=114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.devops955.com\/swain\/wp-json\/wp\/v2\/tags?post=114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}