User Tools

Site Tools


computing:selfhostedwp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
computing:selfhostedwp [2018/11/26 13:40] oemb1905computing:selfhostedwp [2023/12/16 20:33] (current) oemb1905
Line 7: Line 7:
 ------------------------------------------- -------------------------------------------
  
-//selfhostedwp// +//selfhostedwp//
  
 ------------------------------------------- -------------------------------------------
  
-This tutorial is for users of Debian GNU/Linux using the LAMP stack, who are self-hosting two or more websites, have already set up TLS, directory authentication, etc., and are ready to build a self-hosted WordPress (WP) site for one of those virtual hosts We need to begin by doing some ground work for the content management system WP gives us, by setting up an sftp server so we can add plug-ins, etc.+This tutorial is for setting up a self-hosted WordPress instance on Debian GNU/Linux. This tutorial assumes you have some familiarity setting up a LAMP stack. If you need help with thatcheck out [[https://wiki.haacksnetworking.com/doku.php?id=computing:apachesurvival|Apache Survival]]Alrightlet's install our LAMP stack and required/optional php modules//Make sure to review what your instance requires and don't install or configure modules you don't need.//
  
-  sudo apt install proftpd ftp ftp-ssl  +  sudo apt install apache2 mariadb-server php8.2 php-common php-cgi php-cli php-zip php-mysql php-mbstring php-intl php-fpm php-curl php-gd php-imagick php-xml php-xmlrpc php-soap php-opcache php-apcu php-bcmath memcached wget unzip
-  <sudo apt install ftpd-ssl> (not sure if this is needed any longer) +
-  cd /etc/proftpd +
-  sudo openssl req -new -x509 -days 7305 -nodes -out ftpd-rsa.pem -keyout ftpd-rsa-key.pem +
-  sudo nano /etc/proftpd/proftpd.conf+
      
-  <IfModule mod_tls.c> +Sometimes dpkg can choose which version of php you want and it's not always the version you wantIn those cases, you can explicitly specify the version you need. Some packages are only available as ''php-xx'' so run the above command first and then the command below when those situations arise:
-     TLSEngine on +
-     TLSLog /var/log/proftpd-tls.log +
-     TLSProtocol TLSv1 +
-     # Are clients required to use FTP over TLS when talking to this server? +
-     TLSRequired off +
-     TLSRSACertificateFile    /etc/proftpd/ftpd-rsa.pem +
-     TLSRSACertificateKeyFile /etc/proftpd/ftpd-rsa-key.pem +
-     # Authenticate clients that want to use FTP over TLS? +
-     TLSVerifyClient off +
-  </IfModule>+
      
-You can technically put this snippet anywhere, but its proper location is under "#Include /etc/proftpd/tls.conf" in the configurationa space designated for small TLS configurations.  Nowrestart the service:  +  sudo apt-get install php8.2-{common,cgi,cli,zip,mysql,mbstring,intl,fpm,curl,gd,imagick,xml,xmlrpc,soap,opcache,apcu,bcmath}
      
-  sudo systemctl restart proftpd.service+Apache2 will set up a 000-default.conf automatically and your host should now resolve. Be sure to set up TLS with certbot. Here's my preferred method:
  
-I do not change the TLSRequired setting above, because my apache configuration already redirects it, and having two can cause an HSTS error.  Depending on your configuration, you might toggle that parameter above to "on."  Okay, now to mysql-server set up and making index.php default.  +  sudo apt install certbot letsencrypt python3-certbot-apache 
- +  sudo certbot --authenticator standalone --installer apache -d site1.com --pre-hook "systemctl stop apache2" --post-hook "systemctl start apache2" 
-  sudo apt install mysql-server php7.0 phpmyadmin apache2-utils php libapache2-mod-php php-mcrypt php-mysql +  crontab -e 
-  sudo mysql_secure_installation +  <30 2 * * 1 /usr/bin/certbot renew >> /var/log/le-renew.log>
-  sudo nano /etc/apache2/mods-enabled/dir.conf +
-   +
-  <IfModule mod_dir.c> +
-      DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm +
-  </IfModule>+
      
-  sudo systemctl restart mysqld.service+Once you have the LAMP stack setup and TLS properly configured, it's time to make some decisions on your php handler and your apache2 multi-processing module (mpm). There's extensive debate on this which you can read up on [[https://www.datadoghq.com/blog/monitoring-apache-web-server-performance/#prefork-mpm|here]]. In this tutorial, I've chosen not to use mpm_prefork and libapache2-mod-php. Instead I am using mpm_event and php-fpm. This is not necessary for many smaller instances or self-hosted scenarios. If you are new to self-hosting, simply run ''sudo apt install libapache2-mod-php8.x'' and remove the php-fpm and mpm_event steps below.
      
-Secure phpmyadmin with user phpmyadmin and .htaccess file .phpmyadmin for security.+  sudo apt remove libapache2-mod-php* --purge 
 +  sudo apt install php8.3-fpm php8.3-cgi 
 +  sudo a2enmod ssl 
 +  sudo a2enmod headers 
 +  sudo a2enmod cache 
 +  sudo a2enmod rewrite 
 +  sudo a2enmod setenvif 
 +  sudo a2dismod php8.
 +  sudo a2dismod php8.2 
 +  sudo a2dismod php8.3 
 +  sudo a2dismod mpm_prefork 
 +  sudo a2enmod mpm_event 
 +  sudo a2enmod proxy 
 +  sudo a2enmod proxy_fcgi 
 +  sudo a2enconf php8.3-fpm 
 +  sudo a2enconf php8.3-cgi 
 +  sudo apache2ctl configtest   
 +  sudo systemctl restart apache2 
 +  sudo systemctl restart php8.3-fpm
  
 +There are two standard ways to configure php-fpm. One of those is to use ProxyPassReverse, however, this will disable the use of .htaccess in your WordPress root which is not ideal. The next common way which I prefer and use here, is to add a FilesMatch condition to your virtual host as follows. Within the <Include> and </Include> portion of your default-ssl.conf virtual host, add something like:
 +
 +  <FilesMatch ".+\.ph(ar|p|tml)$">
 +      SetHandler "proxy:unix:/run/php/php8.2-fpm.sock|fcgi://localhost"
 +  </FilesMatch>
 +
 +That takes care of configuring php-fpm and mpm_event. Before proceeding, navigate to your tld.domain in a web browser and make sure that your site resolves properly. If it does not, then you should debug your setup. To do that, there's three tools that can help: phpmyadmin, phpinfo page, and configuration checking. If your page does not even resolve, your first recourse should be to check the php handler, multi-processing modules, and your apache config files:
 +
 +  sudo apachectl -M | grep 'mpm'
 +  sudo apachectl -M | grep 'proxy'
 +  sudo apache2ctl configtest
 +
 +The output of mpm should show mpm_event and the output of proxy grep should show proxy_module and proxy_fcgi_module in use. If not, trace back over the steps above and see what went wrong. As for configtest, it should either tell you what's wrong or return "Syntax OK". If the output of the above commands is incorrect and/or you simply want a graphical way to check your handler and mult-processing module, then you can either create a phpinfo page or install phpadmin. There are many additional benefits to using phpmyadmin, so let's review how to install that first:
 +
 +  sudo apt install phpmyadmin
   sudo htpasswd -c /etc/apache2/.phpmyadmin phpmyadmin     sudo htpasswd -c /etc/apache2/.phpmyadmin phpmyadmin  
-  sudo nano /usr/share/phpmyadmin/.htaccess+  sudo nano /usr/share/phpmyadmin/.htaccess   
 +  <AuthType Basic> 
 +  <AuthName "Restricted Files"> 
 +  <AuthUserFile /etc/apache2/.phpmyadmin> 
 +  <Require valid-user> 
 + 
 +If you don't need something as heavy as phpmyadmin, you can optionally create a phpinfo page instead: 
 + 
 +  sudo nano /var/www/wordpress.com/public_html/info.php 
 +  sudo htpasswd -c /etc/apache2/.phpinfo phpinfo   
 +  sudo nano /usr/share/phpinfo/.htaccess   
 +  <AuthType Basic> 
 +  <AuthName "Restricted Files"> 
 +  <AuthUserFile /etc/apache2/.phpinfo> 
 +  <Require valid-user> 
 + 
 +Use these tools to make sure your handler and multi-processing module are configured to your preference and functional. After that's all working, let's make sure that your WordPress index.php is set to top priority as follows:
      
-  AuthType Basic +  sudo nano /etc/apache2/mods-enabled/dir.conf 
-  AuthName "Restricted Files" +  <DirectoryIndex //index.php// index.html index.cgi index.pl index.xhtml index.htm> 
-  AuthUserFile /etc/apache2/.phpmyadmin + 
-  Require valid-user +Close and save the file. Let's now set up a database now for the WordPress instance as follows:
-   +
-  sudo systemctl restart apache2.service +
-   +
-Now, the MySQL - more here than neeeded in case of trouble:+
  
   sudo mysql -u root -p   sudo mysql -u root -p
-  mysql> CREATE DATABASE database1name DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci; +  CREATE DATABASE databasename DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci; 
-  mysql> GRANT ALL ON database1name.* TO 'databaseuser'@'localhost' IDENTIFIED BY 'passwdhere'; +  GRANT ALL ON databasename.* TO 'databaseuser'@'localhost' IDENTIFIED BY 'passwordhere'; 
-  mysql> CREATE DATABASE database2name DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci; +  FLUSH PRIVILEGES; 
-  mysql> GRANT ALL ON database2name.* TO 'databaseuser'@'localhost' IDENTIFIED BY 'passwdhere'; +  EXIT; 
-  mysql> FLUSH PRIVILEGES; + 
-  mysql> EXIT;+Next up, it is time to allow overrides in your primary apache configuration. This is optional but/and it allows WordPress extensions to make configuration changes to .htaccess and/or other changes to the web server. It's often helpful, but you can leave it off if you prefer and configure everything manually.
  
-Install PHP, configure .htaccess to allow overrides, enable apache modules: 
-   
-  sudo apt update 
-  sudo apt-get install php-curl php-gd php-mbstring php-mcrypt php-xml php-xmlrpc 
   sudo nano /etc/apache2/apache2.conf   sudo nano /etc/apache2/apache2.conf
 +  <AllowOverride All>
      
-  <Directory /var/www/html/> +Let's now shell into our instance and set up WordPress.
-      AllowOverride All +
-  </Directory> +
-   +
-  sudo systemctl restart apache2 +
-  sudo a2enmod rewrite +
-  sudo apache2ctl configtest +
-   +
-If you have not set the fully qualified domain name, you may get an error - that can safely be ignored unless you desire it.+
  
-  cd ~/Downloads +  ssh root@site1.com
-  mkdir wpdownload +
-  cd wpdownload+
   curl -O https://wordpress.org/latest.tar.gz   curl -O https://wordpress.org/latest.tar.gz
   tar xzvf latest.tar.gz   tar xzvf latest.tar.gz
-  touch ~/Downloads/wordpress/.htaccess +  mv wordpress /var/www/site1.com/public_html 
-  sudo chmod 660 ~/Downloads/wordpress/.htaccess +   
-  cp ~/Downloads/wpdownload/wordpress/wp-sample-config.php ~/Downloads/wpdownload/wordpress/wp-config.php +Okay, we will need the files and directories I created once we get it running.  Now, let's move the wordpress directory to the proper location for self-hosting
-  mkdir ~/Downloads/wpdownload/wordpress/wp-content/upgrade + 
-  sudo cp -ar ~/Downloads/wpdownload/wordpress/. /var/www/site1.com/public_html/ +  sudo mv ~/Downloads/wpdownload/wordpress /var/www/site1.com/public_html 
-  sudo cp -ar ~/Downloads/wpdownload/wordpress/. /var/www/site2.com/public_html/ + 
-  sudo chown -R username:www-data /var/www/site1.com/public_html +Now, create proper permissions for your Word Press directories and files: 
-  sudo chown -R username:www-data /var/www/site2.com/public_html+ 
 +  sudo chown -R www-data:www-data /var/www/site1.com/public_html
   sudo find /var/www/site1.com/public_html -type d -exec chmod g+s {} \;   sudo find /var/www/site1.com/public_html -type d -exec chmod g+s {} \;
-  sudo find /var/www/site2.com/public_html -type d -exec chmod g+s {} \; +  sudo chmod 755 /var/www/site1.com/public_html/wp-content 
-  sudo chmod g+w /var/www/site1.com/public_html/wp-content +  sudo chmod -R 755 /var/www/site1.com/public_html/wp-content/themes 
-  sudo chmod g+w /var/www/site2.com/public_html/wp-content +  sudo chmod -R 755 /var/www/site1.com/public_html/wp-content/plugins 
-  sudo chmod -R g+w /var/www/site1.com/public_html/wp-content/themes + 
-  sudo chmod -R g+w /var/www/site2.com/public_html/wp-content/themes +It's now time to configure your ''wp-config.php'' file. To do that, however, we need to replace the example salts in the configuration file with random ones from WordPress. They have an API tool that does that for us automatically:
-  sudo chmod -R g+w /var/www/site1.com/public_html/wp-content/plugins +
-  sudo chmod -R g+w /var/www/site2.com/public_html/wp-content/plugins +
-   +
-Grab secure values from Word Press for wp-config.php:+
      
   curl -s https://api.wordpress.org/secret-key/1.1/salt/   curl -s https://api.wordpress.org/secret-key/1.1/salt/
-  sudo nano /var/www/site1.com/public_html/ +  sudo nano /var/www/site1.com/public_html/wp-config.php 
-  <swap the defined values from first curl>+  <Replace the example salts with those you just downloaded using copy/paste>
      
-  curl -s https://api.wordpress.org/secret-key/1.1/salt/ +Sometimes, for reasons I am not sure about, WordPress does not allow users direct uploadingIf/when that happens, add the entry to wp-config.php. If anyone knows why this is needed, please let me know!
-  sudo nano /var/www/site2.com/public_html/ +
-  <swap the defined values from second curl> +
- +
-Enter user name and password for database in wp-config.php:+
  
   sudo nano /var/www/site1.com/public_html/wp-config.php   sudo nano /var/www/site1.com/public_html/wp-config.php
-   +  <define('FS_METHOD','direct');> 
-  . . . + 
-  define('DB_NAME', 'database1name'); +Let's now visit site1.com in a web browser. Enter the credentials that you created for the database above. Choose the settings you prefer and set up an admin account and record your credentials securely. You should now have a proper WordPress site! Now that you have a WordPresscheck the SiteHealth tab and follow its advice and/or know why you don't. In my caseI typically adjust cache, rewrites, and headers
-  /** MySQL database username */ + 
-  define('DB_USER', 'databaseuser'); +  apt install memcached 
-  /** MySQL database password */ +  nano /etc/default/memcached 
-  define('DB_PASSWORD', 'passwdhere'); +  <ENABLE_MEMCACHED=yes> 
-  . . +  a2enmod cache 
-  define('FS_METHOD', 'direct'); +  a2enmod expires 
-   +  a2enmod headers 
-  sudo nano /var/www/site2.com/public_html/wp-config.php +  a2enmod rewrite 
-   + 
-  . . . +After isntalling memcached and enabling those modulesnavigate to your web root and adjust your .htaccess as follows: 
-  define('DB_NAME', 'database2name'); + 
-  /** MySQL database username */ +  <IfModule mod_expires.c> 
-  define('DB_USER''databaseuser'); +          ExpiresActive On 
-  /** MySQL database password *+          ExpiresByType image/jpg "access 1 year" 
-  define('DB_PASSWORD', 'passwdhere'); +          ExpiresByType image/jpeg "access 1 year" 
-  . . . +          ExpiresByType image/gif "access 1 year" 
-  define('FS_METHOD', 'direct'); +          ExpiresByType image/png "access 1 year" 
-   +          ExpiresByType text/css "access 1 week" 
-  sudo systemctl restart apache2 +          ExpiresByType text/html "access 1 month" 
-   +          ExpiresByType text/x-javascript "access 1 week" 
-Plug-ins and other WP services can mess with the .htaccess file often, so use this default configuration below when that happens; more templates can be found here:  [[https://codex.wordpress.org/htaccess|WP Codex]]+          ExpiresDefault "access 1 month" 
 +  </IfModule>
  
-  sudo nano /var/www/site1.com/public_html/.htaccess 
-   
-  # BEGIN WordPress 
   <IfModule mod_rewrite.c>   <IfModule mod_rewrite.c>
-  RewriteEngine On +          RewriteEngine On 
-  RewriteBase / +          RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] 
-  RewriteRule ^index\.php$ - [L] +          RewriteBase / 
-  RewriteCond %{REQUEST_FILENAME} !-f +          RewriteRule ^index\.php$ - [L] 
-  RewriteCond %{REQUEST_FILENAME} !-d +          RewriteCond %{REQUEST_FILENAME} !-f 
-  RewriteRule . /index.php [L]+          RewriteCond %{REQUEST_FILENAME} !-d 
 +          RewriteRule . /index.php [L] 
 +          <FilesMatch "\.(js|css|jpe?g|png|gif|eot|otf|svg|ttf|woff2?)$"> 
 +                  Header set Timing-Allow-Origin "*" 
 +          </FilesMatch>
   </IfModule>   </IfModule>
-  # END WordPress 
  
-Visit wordpress site and configure by opening a web browser of your choice and entering site1.com and site2.com in separate tabs Follow the instructions it provides, especially if you do not have a publicly writeable wp-config.php file (which is a good thing).+  <IfModule mod_headers.c> 
 +          Header always set X-Content-Type-Options "nosniff" 
 +          <IfModule mod_setenvif.c> 
 +                  SetEnvIf Origin "^(.+)$" CORS=$0 
 +          </IfModule> 
 +          Header set Access-Control-Allow-Origin %{CORS}e env=CORS 
 +          Header set Access-Control-Allow-Credentials "true" env=CORS 
 +          <FilesMatch "\.(php|html)$"> 
 +                  Header set X-Frame-Options "ALLOW" 
 +                  Header set X-XSS-Protection "0" 
 +                  Header set X-Download-Options "noopen" 
 +                  Header set X-Permitted-Cross-Domain-Policies "none" 
 +                  Header set X-DNS-Prefetch-Control "on" 
 +                  Header set Pragma "no-cache" 
 +                  Header set Age "0" 
 +                  Header set Cache-Control "" 
 +                  Header set Strict-Transport-Security "max-age=0" env=HTTPS 
 +                  Header set Referrer-Policy "" 
 +                  Header set Cross-Origin-Embedder-Policy "unsafe-none" 
 +                  Header set Cross-Origin-Opener-Policy "unsafe-none" 
 +                  Header set Report-To '{"max_age": 0, "endpoints": [{"url": ""}]}' 
 +                  Header set Content-Security-Policy "default-src * data:; script-src https: 'unsafe-inline' 'unsafe-eval'; style-src https: 'unsafe-inline'" 
 +                  Header set Referrer-Policy "no-referrer-when-downgrade" 
 +                  Header set Feature-Policy "camera 'none'; fullscreen 'self'; geolocation *; microphone 'self' https://plaza.pvpfrontier/*" 
 +          </FilesMatch> 
 +  </IfModule>
  
-  localhost+Personally, I don't think anyone should be using ftp. Sftp is fine, and if someone needs that, here's an example of a simple sftp server using proftp:
  
-Add Joomla, symlinks, directory permissions for low hanging fruit on WP,  +  sudo apt install proftpd ftp ftp-ssl  
 +  sudo a2enmod tls 
 +  cd /etc/proftpd 
 +  sudo openssl req -new -x509 -days 7305 -nodes -out ftpd-rsa.pem -keyout ftpd-rsa-key.pem 
 +  sudo nano /etc/proftpd/proftpd.conf 
 +  <enter parameters>
      
-Addenda on web roots outside of /var/www/+Next, enter the TLS module in tls.conf underneath ''#Include /etc/proftpd/tls.conf'' and then restart the service:  
 + 
 +  sudo nano /etc/proftpd/tls.conf 
 +  <IfModule mod_tls.c> 
 +     TLSEngine on 
 +     TLSLog /var/log/proftpd-tls.log 
 +     TLSProtocol TLSv1 
 +     # Are clients required to use FTP over TLS when talking to this server? 
 +     TLSRequired off 
 +     TLSRSACertificateFile    /etc/proftpd/ftpd-rsa.pem 
 +     TLSRSACertificateKeyFile /etc/proftpd/ftpd-rsa-key.pem 
 +     # Authenticate clients that want to use FTP over TLS? 
 +     TLSVerifyClient off 
 +     TLSOptions NoSessionReuseRequired 
 +  </IfModule> 
 +  sudo systemctl restart proftpd.service 
 +   
 +Refresh WordPress and it should see the sftp server and allow you to make changes that way. Note: The sftp server is public and anyone can access this with proper credentials even if it not for WordPress so use a proper password and make sure your TLS configuration is working. Your instance should now be pretty solid. The only other thing you might want is more than one WordPress site subdomain, for example, site1.cooldomain.com, site2.cooldomain.com, etc. If that's the case, then hop on over to my [[https://wiki.haacksnetworking.com/doku.php?id=computing:wpmultisite|Word Press Multisite]] tutorial.  
 + 
 + --- //[[jonathan@haacksnetworking.org|oemb1905]] 2023/06/30 03:23//
computing/selfhostedwp.1543239641.txt.gz · Last modified: 2018/11/26 13:40 by oemb1905