Monday, April 15, 2019

Running Multiple WordPress Sites On Ubuntu With Apache2, MariaDB And PHP

0
Instead of running one WordPress website per server, which may not be a great way to utilize your system resources, you can run multiple sites on a single server… Nowadays, a Linux host with 1 CPU and 4GB RAM can power multiple WordPress websites and do it efficiently with Apache2…


If you want to learn how to run multiple WordPress websites on Ubuntu 16.04 LTS server, the steps below can get you started… With Apache2 HTTP server, you can run as many WordPress websites as possible as long as the host system has enough resources with a feature called VirtualHost
Apache2 virtualhost is a feature similar to Nginx server block. These features allow users and webmasters to host multiple independent websites or blogs on a single host computer with single IP address and separate domain names.
Instead or running individual website on individual server with individual IPs, Apache2 virtualhost can be configured to run all your different websites from a single host computer and one IP address…
When you’re ready, follow the steps below to get this working.

Step 1: Prepare Ubuntu  Server

Ubuntu Servers are easier to use and manage, especially for new users and students. Other Linux server will let you do the same, but I find Ubuntu to be much more user friendly then other Linux distributions.
So, prepare Ubuntu server and run the commands below to update it.
sudo apt-get update && sudo apt-get dist-upgrade && sudo apt-get autoremove

Step 2: Install Apache2 HTTP Web Server

Next, run the commands below to install Apache2 HTTP server…
sudo apt-get install apache2
After installing Apache2, the commands below can be used to stop, start and enable Apache2 service to always startup when the server boots..
sudo systemctl stop apache2.service
sudo systemctl start apache2.service
sudo systemctl enable apache2.service

Step 3: Install MariaDB Database Server

You’ll need a database server for WordPress… and MariaDB database server is a great place to start. To install it run the commands below.
sudo apt-get install mariadb-server mariadb-client
After installing, the commands below can be used to stop, start and enable MariaDB service to always start up when the server boots.
sudo systemctl stop mysql.service
sudo systemctl start mysql.service
sudo systemctl enable mysql.service
After that, run the commands below to secure MariaDB server.
sudo mysql_secure_installation
When prompted, answer the questions below by following the guide.
  • Enter current password for root (enter for none): Just press the Enter
  • Set root password? [Y/n]: Y
  • New password: Enter password
  • Re-enter new password: Repeat password
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove test database and access to it? [Y/n]:  Y
  • Reload privilege tables now? [Y/n]:  Y
Restart MariaDB server
sudo systemctl restart mysql.service

Step 4: Install PHP 7.1 and other PHP modules

PHP 7.1 isn’t available on Ubuntu default repositories… in order to install it, you will have to get it from third-party repositories.
Run the commands below to add the below third party repository to upgrade to PHP 7.1
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
Then update and upgrade to PHP 7.1
sudo apt update
Run the commands below to install PHP 7.1 and related modules.
sudo apt install php7.1 libapache2-mod-php7.1 php7.1-common php7.1-mbstring php7.1-xmlrpc php7.1-soap php7.1-gd php7.1-xml php7.1-intl php7.1-mysql php7.1-cli php7.1-mcrypt php7.1-ldap php7.1-zip php7.1-curl
After install PHP, run the commands below to open Apache2 default PHP file.
sudo nano /etc/php/7.1/apache2/php.ini
Then edit the below lines to look like this and save the file.
file_uploads = On
allow_url_fopen = On
memory_limit = 256M
upload_max_filesize = 100
max_execution_time = 360
date.timezone = America/Chicago

Step 5: Create WordPress Databases and Dsers

At this point in our setup, all required servers and packages have been installed… it’s now time to create our individual website database and config files…
We’re going to be creating configurations for three WordPress websites.. They are:
http://example.com
http://example.net
http://example.org
We’re going to create three databases for these three websites.. These databases will be called:
examplecomdb
examplenetdb
exampleorgdb
To create the databases, run the commands below to logon to the server.
sudo mysql -u root -p
Then run the commands below to create all three databases… one line at a time…
CREATE DATABASE examplecomdb;
CREATE DATABASE examplenetdb;
CREATE DATABASE exampleorgdb;
Our next task we’ll create three database users, one for each site.. they will be called:
examplecomuser
examplenetuser
exampleorguser
Run the commands below to create the three users and set their passwords
GRANT ALL ON examplecomdb.* TO 'examplecomuser'@'localhost' IDENTIFIED BY 'type_new_password_here';
GRANT ALL ON examplenetdb.* TO 'examplenetuser'@'localhost' IDENTIFIED BY 'type_new_passwored_here';
GRANT ALL ON exampleorgdb.* TO 'exampleorguser'@'localhost' IDENTIFIED BY 'type_new_password_here';
When you’re done, run the commands below to save your changes and exit.
FLUSH PRIVILEGES;
exit;

Step 6: Create Apache2 Server Blocks

Now we’re going to create three server blocks for our three websites.. run the commands below to create each… for each, add th configure code below, replace the domain name and root to their appropriate folders.
sudo nano /etc/apache2/sites-available/example.com.conf
sudo nano /etc/apache2/sites-available/example.net.conf
sudo nano /etc/apache2/sites-available/example.org.conf
For each file, copy and paste the code below… replacing the domain name reference and root folder highlighted.
<VirtualHost *:80>
     ServerAdmin admin@example.com
     DocumentRoot /var/www/html/example.com/
     ServerName example.com
     ServerAlias www.example.com

     <Directory /var/www/html/example.com/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
     </Directory>

     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Example.net server block looks like this:
<VirtualHost *:80>
     ServerAdmin admin@example.net
     DocumentRoot /var/www/html/example.net/
     ServerName example.net
     ServerAlias www.example.net

     <Directory /var/www/html/example.net/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
     </Directory>

     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Do this as many time for each website…
When you’re done, run the commands below to enable the server blocks…
sudo a2ensite example.com.conf
sudo a2ensite example.net.conf
sudo a2ensite example.org.conf

Step 7: Create Document Root Folder for Each Site

In each of the server block or site configuration file, there’s a root location. This is where each website content should be stored.. and should be different for all websites. Now that we’ve defined the root locations for all our site, go and create them below.
sudo mkdir -p /var/www/html/example.com
sudo mkdir -p /var/www/html/example.net
sudo mkdir -p /var/www/html/example.org

Step 8: Download WordPress content and copy it to each of the root location

Now download WordPress content and extract into the root directory for each site. To do that run the commands below
cd /tmp/ && wget http://wordpress.org/latest.tar.gz
Then extract the downloaded file.
tar -xzvf latest.tar.gz
And copy to each root folder for each site.
sudo cp -R wordpress/* /var/www/html/example.com
sudo cp -R wordpress/* /var/www/html/example.net
sudo cp -R wordpress/* /var/www/html/example.org

STEP 9: CONFIGURE WORDPRESS DATABASE SETTINGS

After that, run the commands below to create WordPress wp-config.php settings for each of the domains…
sudo cp /var/www/html/example.com/wp-config-sample.php /var/www/html/example.com/wp-config.php
sudo cp /var/www/html/example.net/wp-config-sample.php /var/www/html/example.net/wp-config.php
sudo cp /var/www/html/example.org/wp-config-sample.php /var/www/html/example.org/wp-config.php
Then run the commands below to open each of the config file and enter the database connection info.. then save the file.
sudo nano /var/www/html/example.com/wp-config.php
sudo nano /var/www/html/example.net/wp-config.php
sudo nano /var/www/html/example.org/wp-config.php
When each file opens type the database name and password for each site you created above.. and save the file.
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'examplecomdb');

/** MySQL database username */
define('DB_USER', 'examplecomuser');

/** MySQL database password */
define('DB_PASSWORD', 'type_password_here');

/** MySQL hostname */
define('DB_HOST', 'localhost');
Do the above for each of the sites you create, making sure the database connection info is correct for each site.
When you’re done, run the commands below to configure the directories permissions for all the sites…
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
After running the commands above, run the commands below to check Apache2 configurations, enable the rewrite module and restart the web server
sudo apachectl configtest
sudo a2enmod rewrite
sudo systemctl restart apache2.service
After restarting Apache2 with no errors, you should be able to access your sites the domain names.. and see WordPress default setup page.
ex… http://example.com
WordPress default setup page
Follow WordPress wizard until setup is complete for each site.. That’s it!
This is how to set up multiple WordPress websites on Ubuntu 16.04 LTS
Enjoy!
Author Image

About Sushil
Soratemplates is a blogger resources site is a provider of high quality blogger template with premium looking layout and robust design

No comments: