Creating a Second WordPress Site on Single Server
It turns out that our prefectural Karate Federation doesn’t have a decent site that has been recently updated. So, I decided to volunteer some time and server space to get one going. Thinking it would be an easy install to put another WordPress site in a sub-directory I did a quick install and immediately started to run into problems with permalinks. When looking online, I couldn’t find anything that seemed to fix the problem, just a lot of random stuff about changing the .htaccess files this way and that.
Turns out, it’s a simple fix in your Apache Virtual host file. Anyway, here’s a quick walk-through of how I did it. Remember, before you do anything, back up your server and read the entire tutorial to make sure that you’ll be able to complete all the steps involved. This tutorial is based on a headless Ubuntu 16.04 server using MySQL and serving with Apache.
#Create Databasemysql -u root -p
CREATE DATABASE your_database_name DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
#Create User for this databaseCREATE USER 'adminuser'@'%' IDENTIFIED WITH mysql_native_password BY 'super_secret_password';
GRANT ALL ON database_user.* TO 'admin_mail'@'localhost' IDENTIFIED BY 'super_secret_password';
FLUSH PRIVILEGES;
EXIT;
#Download WordPresssudo mkdir tmp
cd tmp/
sudo curl -O https://wordpress.org/latest.tar.gz
sudo tar xzvf latest.tar.gz
#Add dummy .htaccesssudo touch wordpress/.htaccess
#Copy configuration filesudo cp wordpress/wp-config-sample.php wordpress/wp-config.php
#Make upgrade directorysudo mkdir wordpress/wp-content/upgrade
#Copy all files to document root => -a maintains permissions and the . makes sure hidden files are copied as wellsudo cp -a wordpress/. /var/www/html/your_subdirectory
#Configure the Directory (Some of this may be overkill, I generally just do the first 3 lines and haven’t had any issues up to now.)
#Ownershipsudo chown -R www-data:www-data /var/www/html/your_subdirectory
#Permissions for folders:sudo find /var/www/html/your_subdirectory/ -type d -exec chmod 775 {} \;
#and files:sudo find /var/www/html/your_subdirectory/ -type f -exec chmod 664 {} \;
#Ensure all future directories are created with same permissions (set the setgid)sudo find /var/www/html/your_subdirectory -type d -exec chmod g+s {} \;
#Allow group write access to the wp-content directory for theme and plugin changessudo chmod g+w /var/www/html/your_subdirectory/wp-content
#Give web server write access to the directorysudo chmod -R g+w /var/www/html/your_subdirectory/wp-content/themes
sudo chmod -R g+w /var/www/html/your_subdirectory/wp-content/plugins
#Get secure keys from WordPress for this installation (IMPORTANT!)sudo curl -s https://api.wordpress.org/secret-key/1.1/salt/
#Copy the output and paste in the configuration filesudo nano /var/www/html/your_subdirectory/wp-config.php
#Also change the required database information as per our user information when setting up the database – (change the database name, user name and password in the config file)
#In addition, add the following line to allow wordpress to write directly to the filesystemdefine('FS_METHOD', 'direct');
#Save & Close
Now for the Apache settings. Create a virtual hosts configuration file for your site and add the example virtual host code from below to the conf file. Be sure to set your DocumentRoot to the correct subdirectory where you placed the WordPress files and replace ALL the example.com references with your proper URL.
#Create Conf file
sudo nano /etc/apache2/sites-available/site_name.conf
<VirtualHost *:80>
# The primary domain for this host
ServerName example.com
# Optionally have other subdomains also managed by this Virtual Host
ServerAlias example.com *.example.com
DocumentRoot /var/www/html/your_subdirectory
<Directory /var/www/html/your_subdirectory>
Require all granted
# Allow local .htaccess to override Apache configuration settings
AllowOverride all
</Directory>
# Enable RewriteEngine
RewriteEngine on
RewriteOptions inherit
# Block .svn, .git
RewriteRule \.(svn|git)(/)?$ - [F]
# Catchall redirect to www.example.com
RewriteCond %{HTTP_HOST} !^www.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) https://www.example.com/$1 [L,R]
# Recommended: XSS protection
<IfModule mod_headers.c>
Header set X-XSS-Protection "1; mode=block"
Header always append X-Frame-Options SAMEORIGIN
</IfModule>
</VirtualHost>
#Enable the site. This will create a symlink to the example.com Apache conf file in /etc/apache2/sites-enabled/:
sudo a2ensite example.com
go to the url: http://example.com/wp-admin/install.php
#Complete installation online in WP
Site Title Your Site Name
Username WP_User_Name
Password Super_Secret_Password
#Within WP – Go to settings and change:
Site URL
SIte Language (if necessary)
Permalinks
#SECURE YOUR SITE WITH SSLsudo certbot --apache -d example.com
Hopefully, that will give you good results and you’ll have an SSL secured WP site ready to go. You can see the results of my work here: https://nkkf.jp
Thanks for reading and good luck!!
OH, Hang on there!!! One last thing!!
Don’t forget to edit your /etc/hosts file and add your site. This allows it to run the loopback processes required to make WP happy.