How to optimize WordPress with Ubuntu VPS

Ubuntu Virtual Private Server optimized for WordPress

Category: WordPress

WordPress

  • How to move WordPress to a new server and change the domain

    How to move WordPress to a new server and change the domain

    Moving WordPress to a new server or changing the domain name  is usually an easy and straight forward task. Please contact me if you need advice or help on moving your WordPress site to a new server or to a new domain. Usually, you can get a fixed price for the job. Please contact me for a quote.

     

     

    How to move WordPress to a new server or a new domain or both.

    Manually move WordPress to a new server with a new domain

    • Disable all plugins
    • Create a backup of your database.
    • Create a backup of WordPress.
    • At the menu «Settings» – «General» change to a new domain.
    • Create a new backup of the database and a backup of all the files.
    • Restore the database and the files on the new server.
    • In the file wp-config.php change the name of the database, user and password if needed
    • In .htaccess change to a new domain name if needed
    • If needed add the following to lines of code to you wp-config.php file:
    define('WP_SITEURL', 'http://www.example.com');

    And this:

    define(‘WP_HOME', 'http://www.example.com');

     

     

     

     

    Useful links

     

     

     

    Migrating WordPress to the new server

    Things to do on the new server after the move

    • Log in on the new server
    • Activate your plugins one-by-one. If something bad happens then you will know which plugin that has created the trouble.
    • Change all the links. You can do this directly in the database or use a link checker plugin for WordPress. In the MySQL database go to the value
    wp_options

    Find the values

    site_url

    And

    home

    Change these values to the new domain.

     

     

     

     

    Change the traffic from the old domain to then new domain

    To avoid losing your ranking on the search engines and losing visitors the most favorable technique is to use a redirect 301 statement in your .htaccess file. Do this:

    Open the file on the old server with the name:

    .htaccess

    Add the following text:

    Redirect 301 / http://www.tbt.no

    A 301 forward setup will forward all traffic from the old domain name to the new domain name.

     

     

     

    How to change the domain name on your WordPress site

    Search and replace in the MySQL database

    With this plugin can you do a search and replace in the WordPress database to change from the old to the new domain name.

     

     

     

     

    Change to a new domain on Google FeedBurner

    Remember to change the Google FeedBurner source so your RSS feeds continues to work

     

     

     

     

    Twitter, Facebook, Mailchimp or aWeber

    Have you connected Facebook,  aWeber and Twitter to your site they will also need an update to the new URL.

     

     

     

  • How to automatically restart apache every day, week or month

    How to automatically restart apache every day, week or month

    Your server might be more stable if you restart the apache service once a day, once a week or once a month. You can do this by adding a single line of text in the «Cron» service on your apache server.

     

    300x300-apache

     

    This command restarts the Apache service every morning 04:00 AM. That’s properly when there are few visitors. Takes a few seconds and properly is the users not noticing the downtime.

     

    Start Cron. First time you run this command you will probably be asked which text editor you want to use. I recommend that you choose Nano.

    sudo crontab -e

    In the text editor Nano, just add the following line. Then save and close the text editor Nano. That’s all

    0 4 * * 4 root service apache2 restart > /dev/null 2>&1

    This command will restart apache at 4 in the morning every Thursday.

    To check if this work use the command:

    sudo service apache2 status
  • How to solve WordPress admin problems when using CloudFlare

    How to solve WordPress admin problems when using CloudFlare

    Sometimes the WordPress admin dosen’t behave as expected when using CloudFlare. Her are some advice that could help:

     

     

     

    Using CloudFlare together with WordPress makes your site much faster and safer

    Alternative A.

    Turn off Cloudflare while upgrading or performing troublesome tasks

    The fast and easy way is to disable CloudFlare while editing your WordPress site. Then enable it when finished. Pausing CloudFlare is probably the easiest way.

     

     

     

     

    Alternative B.

    Create an exception

    Use the «Page Rule» to create an exception. You do this in the admin panel.
    With page rules the admin panel should work like before you changed to CloudFlare.
    The downside is that you sacrifice security to get better usability.

    Exceptions and «Page Rules» are described her:

    Introducing Page Rules: Fine Grained Control over CloudFlare’s Features

    WordPress Security: Protecting your WordPress site with (and without) CloudFlare

    CloudFlare support pages about WordPress and Page Rules

     

     

     

    HostBuddy is the easiest way to maintain your hosts file.

    Alternativ C.

    Use a  hosts to override the DNS service and connect directly to your website.

    The hosts file let you bypass CloudFlare. Just enter the website IP address with a space or a tab and the domain name. That’s all it takes.

    I put together a short list with software for Mac and Windows you can use to edit your hosts file

    Another nice thing about using the hosts file is that you can limit access to the admin section with a .htaccess file.

    In the firewall, you can limit access to your website to the IP range that belongs to CloudFlare and the IP range that belongs to your workplace. This gives you an extra layer of security. The IP range that belongs to CloudFlare is available her: https://www.cloudflare.com/ips

     

     

     

     

    Resources regarding how to bypass CloudFlare with a hosts file

     

     

    Watch a intro to CloudFlare

    http://youtu.be/facXND9eV3c

     

     

     

    Norwegian version:

  • Install WordPress on Ubuntu with Amazon AWS EC2 VPS server the old manual way

    Install WordPress on Ubuntu with Amazon AWS EC2 VPS server the old manual way

    Create your MySQL database on Amazon AWS RDS

    The main difference on this tutorial  about EC2 WordPress setup is that we use a RDS database in stead of installing the database on the Ubuntu server. This saves considerable with CPU and RAM. When the EC2 VPS runs short of CPU and or memory, the first thing that suffers is the MySQL database. This again leads to that your WordPress goes down.

    Use Sequel Pro, MySQL Workbench or Navicat create a empty database. The easiest way is to tunnel trough EC2 to RDS using SSH.

    Download WordPress and create the root directory

    Become admin on the server

    sudo su

    Create a directory where you will install WordPress.

    mkdir /var/www/c.tbt.no

    (replace c.tbt.no with your domain name)

    Go to the home user area.

    cd /home/ubuntu

    Then dowload WordPress

    wget https://wordpress.org/latest.zip

    Unzip the package after installing the unzip utility.

    apt-get install unzip

    Unzip wordpress

    unzip latest.zip

    Copy WordPress to the correct folder

    cd wordpress
    
    cp -r . /var/www/c.tbt.no 

    (replace c.tbt.no with your domain name)

        Remove the installation files from the home area

    cd ..
    rm -r wordpress
    rm latest.zip
    Check to se that the files and folders really are removed:

    ls -a

     

    EC2 WordPress setup
    EC2 WordPress setup

    WordPress need permissions on the file system

    Give permissions so that the WP updates will work.

    chown -R www-data /var/www/c.tbt.no

     

    Activate .htaccess

    Sometimes you might need to activate .htaccess in the main apache config file. The config file is located here:

    sudo nano /etc/apache2/apache2.conf

     

    Search for the expression “Options Indexes FollowSymLinks“b. Do this by pressing “CTRL + W” and paste the expression into the Nano search field.
    Find the following three lines:

     Options Indexes FollowSymLinks
     AllowOverride None
     Require all granted
    

    Change the word “None” to “All”.
    The correct version would look like this:

     Options Indexes FollowSymLinks
     AllowOverride All
     Require all granted
    

    Then restart Apache

     sudo service apache2 restart
    

     

    Activate mod_rewrite

    Sometimes you might need to activate mod_rewrite

    a2enmod rewrite

    – and then restart Apache

    service apache2 restart

     

    Add PHP-GD support to the Apache webserver

    If you are running NextGenGallery you will ned this:

    sudo apt-get install php7.0-gd
    

     

    Add this to your php.ini file:

    sudo nano /etc/php/7.0/apache2/php.ini

    Add the following line:

    extension=php_gd2.so

     

    Run the WP installer to complete the EC2 WordPress setup

    Go to your domain and run the installer.

     Checking the Apache logs

    If something is wrong then the log files could be an nice starting point.

    nano /var/log/apache2/error.log

    To delete the error log:

    rm /var/log/apache2/error.log

    Check the Apache log:

    nano /var/log/apache2/access.log

    Delete the Apache log:

    rm /var/log/apache2/access.log

    Restart Apache

    service apache2 restart

    Optional steps

    Set up centralized administration using ManageWP. Set up SMTP sending from WordPress using Amazon SES and appropriate plugin.  Buy a 3 year contract on your EC2 instance to get the best price.

    Recomendations

    We recommend using Thesis design framework from http://DIYthemes.com

    Articles in this serie

     

  • Using video in you WordPress blog

    Using video in you WordPress blog

    When YouTube or Vimeo isn´t an option. Then you should consider these solutions:

    logo-1

    Netflix and many other large vendors use the Amazon AWS data centers for the whole process of distribution videos. NetFlix has an large amount of EC2 VPS servers and uses the streaming solutions from Amazon. Digital Media on AWS.  Amazon Web Services provides a set of secure and on-demand storage, archive, computing, transcoding, streaming, and application services for media companies. Resources about streaming video

    logo2

    VideoPress is a solution from the same company that creates WordPress. Quite possibly the easiest way to upload beautiful videos to your blog. It’s inexpensive, ad-free, and it just works with WordPress. You’ll be able to upload videos from your phone or through your WP admin directly to your blog.

    Strict privacy control

    VideoPress uses your blog’s built in privacy settings to determine who can see your videos.

    No max time duration

    With VideoPress, you can upload videos up to 1GB in size, with no cap on the length of your video.

    logo3
    JW Player Publish Your Videos

    Get broadcast-quality playback everywhere, every time. Customize your player, distribute video, and view your analytics. Hundreds of features work seamlessly in HTML5 & Flash.

    Run Video Ads

    Serve desktop & mobile video ads. Integrate with any ad server or network or set up advanced integrations with our ads API. Everything ads, out of the box.

    Stream With Us

    Upload any audio or video file and we’ll optimize it for all screens. Manage your entire video library and deliver billions of streams reliably and fast.

  • How to install Apache, PHP7 and MySQL connector

    Setting up your MySQL database on Amazon RDS

    Use the free MySQL WorkBench to create your database, create database user, password and assign rights on Amazon RDS.

    Alternatively use Sequel Pro or use Navicat to set up your database on the  Amazon RDS database server. It dos not matter which of the 3 tools you are using.

    Install Apache2, PHP7, Curl and MySQL connector on Ubuntu

    This will install:

    • Apache
    • PHP7
    • MySQL database connector to access Amazon RDS. You will need this connector use the remote Amazon AWS RDS server.
    sudo apt-get install apache2 php7.0 php7.0-mysql libapache2-mod-php

    Install curl, gd and zip

    • Curl is widly used and needed by WordShell.
    • GD is needed by WordPress to change the size of uploaded images.
    • Zip is a easy way to compress directories and files when backing up or moving files and directories.
    sudo apt-get install curl php7.0-curl php7.0-gd zip

    Apache modules to activate

    sudo a2enmod rewrite

    Then restart Apache to activate the change:

    sudo service apache2 restart

     

  • Add virtual host and domains using a single IP

    The first IP address is free with Amazon AWS. Extra IP’s are not. This how-to guides you through the process of setting up as many virtual host domain in Apache as you need on a single IP. All WordPress sites using SSL / httpS://.

    Sites without SSL / httpS:// is given a lower SEO ranking by Google

    We have blocked port 80 in the firewall. All sites are using port 443 with a valid SSL certificate provided by CloudFlare. The only port open in the firewall is 22 for SSH admin and 443 for the hosts. SEO ranking for non-SSL sites are lower on Google.com. Since valid  SSL certificates are free. Who would create a site without encryption?

    Make sure Apache is configured correct for SSL and virtual host

    Ubuntu is not SSL prepared by default. Especially not for using many SSL sites on the same IP.  We will walk you through the needed modifications here:

     

    [themedy_button url=”https://www.wpjeos.no/enable-ssl-on-apache-ubuntu/” icon=”wrench” font_awesome_att=”” label=”How to configure Apache for SSL with virtual hosts on a single IP” colour=”blue” colour_custom=”” size=”large” edge=”rounded” target=”_self”]

     

    [themedy_button url=”https://www.wpjeos.no/adding-ssl-certificate-cloudflare/” icon=”adjust” font_awesome_att=”” label=”Adding SSL certificate from CloudFlare” colour=”blue” colour_custom=”” size=”large” edge=”rounded” target=”_self”]

    Create root folders to store your content

    First you need to create the website route folders on the file system where the files is to be stored.

    cd /var/www/
    sudo mkdir /var/www/MyDomain1.com
    sudo mkdir /var/www/MyDomain2.com
    sudo mkdir /var/www/MyDomain3.com

    Create a test file to use when verifying that the hosting works.

    sudo nano /var/www/MyDomain1.com/index.html

    Write:

    This is a test page for the domain https://www.mydomain1.com and/or https://mydomain1.com

    Save the file and exit Nano.

    Then make sure Apache can access these folders:

    chown -R www-data /var/www/
    
    

    Create the first site and use it as a template

    To add new domains to your configuration is a quite easy task on Apache.

    You just go to the folder where you find your existing config files.

    cd /etc/apache2/sites-available

    Then add a new config file for the new domain. Some times it’s easier to just copy an existing config file and edit it to reflect the name of the domain. To make it easier to troubleshoot problems it is a good habit to give each config file a number. This is because Apache will start one host at a time based on the number sequence.

    01-MyDomain1-com.conf
    02-MyDomain2-com.conf
    03-MyDomain3-com.conf
    

    To copy and the modifying an existing Apache configuration file

    Stop all the active config files and running Apache hosts.

    cd /etc/apache2/sites-available
    sudo a2dissite *.conf

    Then restart Apache so the changes takes effect

    sudo service apache2 restart

    First backup the default config files:

    sudo zip default-apache-config-files.zip *.conf

    Move the backup to your home folder:

    sudo mv zip default-apache-config-files.zip /home/ubuntu/default-apache-config-files.zip

    You might want to remove the non-SSL config file to keep the folder orderly:

     sudo rm 000-default.conf

    Now rename the default ssl config file

    sudo mv default-ssl.conf 01-MyDomain1-com.conf

    Finally we will modify the the configuration file:

    sudo nano 01-MyDomain1-com.conf

    This is what the Apache config file looks like before you start editing:

    <ifmodule mod_ssl.c>
     <virtualhost _default_:443>
     ServerAdmin mail@locahost
    
     DocumentRoot /var/www/html
    
     # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
     # error, crit, alert, emerg.
     # It is also possible to configure the loglevel for particular
     # modules, e.g.
     #LogLevel info ssl:warn
    
     ErrorLog ${APACHE_LOG_DIR}/error-default-.log
     CustomLog ${APACHE_LOG_DIR}/access-default-.log combined
    
    

    Modify the values in the file to look like this:

    <IfModule mod_ssl.c>
    <VirtualHost *:443>
    ServerAdmin mail@mydomain1.com
    ServerName mydomain1.com
    ServerAlias mydomain1.com
    DocumentRoot /var/www/MyDomain1.com ErrorLog ${APACHE_LOG_DIR}/01-mydomain1-com-error.log CustomLog ${APACHE_LOG_DIR}/01-mydomain1-com-access.log combined 

    Save the file.
    Activate the config file:

    sudo a2ensite 01-MyDomain1-com.conf

    Restart Apache

    sudo service apache2 restart

    GoCheck that the configuration file is correct with these tools:

    sudo apachectl configtest
    
    sudo systemctl status apache2.service
    
    sudo journalctl -xe
    
    

    Start a web browser and go to https://mydomain1-com or https://www.mydomain1-com to verify that you can access the page you created earlier with the text:

    This is a test page for the domain https://www.mydomain1.com and/or https://mydomain1.com

    If this page is not displayed, you should not continue before the error is solved. Continueing will make the troubleshooting more complex.

    Continuing with the 2. and 3. domain / virtual host

    Now use 01-MyDomain1-com.conf as the template for the two next virtual hosts.

    To copy an existing file:

    sudo cp 01-MyDomain1-com.conf 02-MyDomain2-com.conf
    
    sudo nano 02-MyDomain2-com.conf

    Then edit the config to match the second WordPress site:

    <IfModule mod_ssl.c>
    <VirtualHost *:443>
    ServerAdmin mail@mydomain2.com
    ServerName mydomain2.com
    ServerAlias mydomain2.com
    DocumentRoot /var/www/MyDomain2.com
    ErrorLog ${APACHE_LOG_DIR}/02-mydomain2-com-error.log
    CustomLog ${APACHE_LOG_DIR}/02-mydomain2-com-access.log combined
    
    
    Save.
    Then activate the configuration.
    
    sudo a2ensite *.conf

    Finally restart Apache so the new site gets published.

    sudo service apache2 restart

    Check and verify that the new site are online.

    Now repeat the process above to create the 3. WordPress site:

    sudo cp 01-MyDomain1-com.conf 03-MyDomain3-com.conf

    Then edit the config file

    sudo nano 03-MyDomain3-com.conf

    Change the values so it looks like this:

    <IfModule mod_ssl.c>
    <VirtualHost *:443>
    ServerAdmin mail@mydomain3.com
    ServerName mydomain3.com
    ServerAlias mydomain3.com
    DocumentRoot /var/www/MyDomain3.com ErrorLog ${APACHE_LOG_DIR}/03-mydomain3-com-error.log CustomLog ${APACHE_LOG_DIR}/03-mydomain3-com-access.log combined
    

    Save.
    Then activate the configuration.

    sudo a2ensite *.conf

    Finally restart Apache so the new site gets published.

    sudo service apache2 restart

    Check that the Apache config file is correct:

    sudo apachectl configtest
    
    sudo systemctl status apache2.service
    
    sudo journalctl -xe
    

    When troubleshooting then, disable, enable and activate Apache domain config files with these commands:

    sudo a2ensite *.conf
    
    sudo a2dissite *.conf
    
    sudo service apache2 restart
    

     

    Check and verify that the new site is online.

    Check the server error logs

    It’s a good practice to check the error logs. They are here:

    cd /var/log/apache2/
    sudo nano error.log
    sudo nano 01-mydomain1-com-error.log
    sudo nano 02-mydomain2-com-error.log
    sudo nano 03-mydomain3-com-error.log

    Set correct permissions so Apache can access

    sudo chown -R www-data /etc/apache2/sites-enabled
    sudo chown -R www-data /var/www/MyDomain1.com