How to optimize WordPress with Ubuntu VPS

Ubuntu Virtual Private Server optimized for WordPress

Author: .

  • How to automatically set the correct time on the server once a day on Ubuntu and then set the correct timezone

    How to automatically set the correct time on the server once a day on Ubuntu and then set the correct timezone

    How to set the correct timezone on the server

    sudo dpkg-reconfigure tzdata
    

     

     

     

     

    Remove the old NTP server

    Previous versions of Ubuntu used another NTP server.

    sudo apt autoremove ntp

     

     

     

    How to automatically synchronize the time on the server with a NTP server

     

    The following port must be open for outgoing traffic in the firewall:

    UDP port: 123

     

     

    Check current status:

    date

     

     

    Turn on NTP syncronisation:

    sudo timedatectl set-ntp on

     

     

    The current status of time and time configuration:

    timedatectl status

     

     

    Check the status more in detail via:

    systemctl status systemd-timesyncd

     

     

    Restart the service

    sudo systemctl restart systemd-timesyncd.service

     

     

  • 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 activate and mount a second HDD on EC2 Ubuntu VPS

    How to activate and mount a second HDD on EC2 Ubuntu VPS

    On Amazon AWS EC2 VPS server it might be useful to mount a 2. hard disk drive.  Maybe your primary disk is full. This is how it’s done.

    Recover data from a EC2 VPS instance with Ubuntu that won’t boot

    Sometimes you need to restore files from a snapshot or a volume that wont boot. In this case the volume could be opened by mounting it as a 2. drive on a temporary EC2 instance. This procedure is useful if your EC2 instance is unable to boot. Then you can set up a new ECS VPS server and mount the non bootable disk as a second disk and extract the files from it. Need help? Don’t hesitate to contact me.

    Sometimes you need to restore files from a snapshot or a volume that wont boot. In this case the volume could be opened by mounting it as a 2. drive on a temporary EC2 instance.

     

    First see if the drive is attached with this command:

    sudo fdisk –l

    You will get a listing of available drives. If the drive already exist it will reveal itself with this line:

    Disk /dev/xvdf: 10.7 GB, 10737418240 bytes

    Now format the disk with this command, if you haven’t already done so:

    sudo mkfs -t ext4 /dev/xvdf

    Create a directory where the drive should be mounted:

    mkdir /mnt

     

     

    This is it. Now you can mount the drive with the command:

    sudo mount /dev/xvdf /mnt

     

    Check if the disk has been mounted correctly with this command:

    mount -l

    You should get this result:

    /dev/xvdf on /mnt1 type ext4 (rw)

     

    Mount on startup

    Set the drive to mount automatically on system startup with adding a line to the configuration file:

    sudo nano /etc/fstab

    and add this line to the file:

    /dev/xvdf /mnt1 auto defaults,nobootwait 0 0

    Save the file and exit Nano.

     

    Reboot the server to check that everything works correctly

    After reboot you can access the  new drive with the command:

    cd /mnt1

     

    mkdir /www.tbt.no

    That’s it.

     

    Your new root folder for Apache is ready for use at the address:

    /mnt/www.tbt.no/

    Credits

    Source for this article is Mikhael Plavskiy

     

    Alternativ address:

    http://www.tbt.no/ec2-mount-2-hd-to-retrieve-files/

  • 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:

  • How to edit the hosts file from a graphical interface

    How to edit the hosts file from a graphical interface

    Being able to edit the hosts file is a valuable tool for any web developer. If you are a http://CloudFlare.com customer, you can bypass the CloudFlare systems by using a hosts file and direct the traffic directly from your computer is to the server. If you are moving WordPress site from one server to another server, then you can test that the website works correctly on the new server before you change the DNS setting. This way you can avoid any downtime the service. The hosts file enables you to create a website before the domain is effective or before the domain is being registered. Reason for this is that the host file will override the DNS services. You can also create our update website locally on your computer while the production site is still running on the Internet.

    With at graphical user interface for the hosts file changes are made in a snap, and you can reload so that the services takes effect immediately. Here is a list over MAC and Windows Graphical Interface.

     

    hostbuddy-featured

    Mac

    http://clickontyler.com/hostbuddy/

    Hostbuddy is the easiest way to manage and update the /etc/hosts file on your Mac. Add new entries and turn on and off existing ones with one click. It’s perfect for switching between staging and production servers or just for testing out your new website before it goes live.
    You should actively use the hosts file already on your computer.
    What it Does

    • Quickly add host entries without using the command line
    • Turn on/off host groups with one click
    • Won’t overwrite your existing /etc/hosts file
    • Compatible with VirtualHostX
    • Automatically flushes your DNS cache

    40003_scr_sm

    Hosts

    http://www.macupdate.com/app/mac/40003/hosts

    Hosts are a system preference pane to manage your hosts file.
    Hosts add a preference pane to your system preferences which let you toggle your host file entries on and off, as well as add and remove them.

    Hosts.prefpane likes to keep your hosts file clean. When an entry is toggled off, it is not commented out in the hosts file but instead stored in the app’s preferences storage. This means you will have to uncomment all your unused host entries and untoggle them in the pane if you want to use them. Comments are treated as comments and will never show up in the list of host entries!

    Of course, Hosts.prefpane will detect edits you made to the /etc/hosts directly and merge them into the listing.

    Backups of your hosts file is made in ~/Library/Application Support/Hosts. The backup or your hosts file from before you started using Hosts.prefpane is called hosts.orig and each session a backup is made called hosts.session.

    The default host entries that are needed by the OS are hidden and can not be edited using Hosts.prefpane.

     

     

     

     

    Windows

     

     

    Blue_life_hosts_editor
    BlueLifeHosts editor

    http://www.sordum.org/8266/bluelifehosts-editor-v1-2/

    Blue life hosts editor is a Portable Freeware hosts-file editor. It Allows you to add easily/delete, Block, Update domain names to your hosts file. Simply type the hostname you wish to add and use “Resolve domain name and add into the list” button.

    When you type a domain name in your browser that you wish to connect to, your application must first convert it to an IP address that it will use to connect to.The way these hostnames are resolved to their mapped IP address is called Domain Name Resolution. On almost all operating systems whether they are Apple, Linux, Unix, Netware, or Windows the majority of resolutions from domain names to IP addresses are done through a procedure called DNS. DNS stands for Domain Name System. IT is the standard domain name resolution service used on the Internet. There is another way to resolve domain names without using the Domain Name System, and that is by using your HOSTS file. Almost every operating system that communicates via TCP/IP, the standard of communication on the Internet, has a file called the HOSTS file. This file allows you to create mappings between domain names and IP addresses Another way to think of a hosts file is like an address translator and redirector.

     

     

    HostsMan_4.2_001

    HostsMan

    http://www.abelhadigital.com/hostsman
    HostsMan is a freeware application that lets you manage your Hosts file with ease.
    Easy way to manage your host file with a GUI on any windows computer.
    While you can always manually manage or edit Hosts file manually, I would like to suggest that you use a third-party freeware tool like HostsMan to do it.
    HostsMan is a feature-rich tool that lets you add, remove, edit entries in the Hosts file. It also lets you easily add third-party Hosts lists that help in blocking malware sites and lets you easily enable or disable the hosts file. It includes a built-in hosts file updater & a hosts editor. It lets you scan the hosts file for errors, duplicates and possible hijacks – and also lets you create an Exclusion list. One more useful feature it offers is the Hosts file backup manager. Back up your Hosts file to a safe place and use it to restore it, should the need arise.

    Features

    • Built-in hosts file updater
    • Enable/Disable hosts file
    • Built-in hosts editor
    • Scan hosts for errors, duplicates and possible hijacks
    • Hosts file backup manager
    • Exclusion list
    • Hostman also lets you Flush DNS cache.
    • Open Hosts with a text editor
    • Count the number of hosts
    • Find duplicates
    • Replace IP
    • Scan hosts for malicious entries
    • Rearrange hosts
    • Manage DNS Client Service
    • And more.
    • In short, it’s the only Hosts Manager that you will even need.

     

     

     

     

    hosts_file_editor

    Hosts File Editor

    http://hostsfileeditor.codeplex.com

    Hosts File Editor makes it easy to change your hosts file as well as archive multiple versions for easy retrieval.

  • 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