Tag: WordPress

  • WordPress  CherryFramework lessphp fatal error

    WordPress CherryFramework lessphp fatal error

    On a WordPress site using CherryFramework based theme, was getting following error.

    lessphp fatal error: load error: failed to find /home/mediaxtreme/sitedomain.com/wp-content/themes/theme50607/bootstrap/less/bootstrap.lesslessphp fatal error: load error: failed to find /home/mediaxtreme/sitedomain.com/wp-content/themes/theme50607/style.less
    Warning: Cannot modify header information - headers already sent by (output started at /home/mediaxtre/public_html/wp-content/themes/CherryFramework/includes/less-compile.php:157) in /home/mediaxtre/public_html/wp-login.php on line 423
    

    This was due the web site have migrated to a new server and the path in new server was differnt than old server.

    To fix, edited file

    wp-content/themes/CherryFramework/includes/less-compile.php
    

    On line 157, you see following code

            try {
                    $less->compileFile($inputFile, $outputFile);
            } catch (Exception $ex) {
                    echo "lessphp fatal error: ".$ex->getMessage();
            }
    

    Replace ith with

            try {
                    $less->compileFile($inputFile, $outputFile);
            } catch (Exception $ex) {
                    # echo "lessphp fatal error: ".$ex->getMessage();
            }
    

    You will be able to login to wordpress admin area. Once you logged in, go to theme settings and just update, this will update the paths. Now you can remove the change you done in less-compile.php

  • Change WordPress URL

    Method 1: Editing MySQL backup

    To change the WordPress site URL, backup your MySQL database.

    mysqldump -u USERNAME -p WP_DB_NAME > db.sql

    Run sed command to replace the URL in MySQL backup file.

    sed -e 's/OLD_URL/NEW_URL/gi' db.sql > db-modified.sql

    Restore the new db-modified.sql, which will have the database with the URL changed.

    mysql -u USERNAME -p WP_DB_NAME < db-modified.sql

    Method 2: Using Plugin

    Edit your wp-config.php and add the following line.

    define('WP_HOME','https://yourdomain.com');
    define('WP_SITEURL','https://yourdomain.com');

    if you want the site available on multiple URL

    define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST'] . '/');
    define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST'] . '/');

    You can also edit the wp_options table

    update wp_options set option_value="https://yourdomain.extn" where option_name="siteurl" or option_name="home";

    Now you need to use some WordPress plugins to change URLs in the database, here are some useful plugins for changing site URL.

    See WordPress

  • Find WordPress Version

    To find WordPress version, check file

    wp-includes/version.php
    

    WordPress version

    See WordPress

  • Enable Amazon Cloudfront CDN in WordPress

    I was setting up a wordpress web site to use Amazon S3 + Cloudfront as CDN. I used w3 total cache plugin for this. w3 total cache plugin have option to auto create Amazon S3 bucket and cloudfront distribution, for for this, you need to give Amazon AWS API keys.

    WordPress CDN Setup W3 Total Cache

    Clicking the button “Create as new bucket with distribution” will auto create s3 bucket and cloudfront for you.

    if you site have images already uploaded, you need to upload them to Amazon S3. This can be done with W3 Total Cache.

    Upload WordPress media to CDN

    If you have lot of files, upload may fail. You may need to use Amazon AWS CLI tool to do the upload.

    aws s3 sync /opt/bitnami/apps/wordpress/htdocs/wp-content/uploads/ s3://your-bucket-name/wp-content/uploads/
    

    If needed, you can upload themes and plugins folder too

    aws s3 sync /opt/bitnami/apps/wordpress/htdocs/wp-content/plugins/ s3://your-bucket-name/wp-content/plugins/
    aws s3 sync /opt/bitnami/apps/wordpress/htdocs/wp-content/themes/ s3://your-bucket-name/wp-content/themes/
    
  • WordPress

    WordPress Installation

    WordPress Migration

    WordPress Developement

    Tools

    https://scanwp.net – Scan WordPress for plugins/theme installed

    Errors

    WordPress Attacks

    WordPress Behind Reverse Proxy

    Add the following code to wp-config.php to get WordPress work behind a reverse proxy

    if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
        $_SERVER['HTTPS']='on';
    }

    Enable SSL

    update wp_options set option_value="https://yourdomain.extn" where option_name="siteurl" or option_name="home";

    Increase WordPress memory

    Add the following to wp-config.php

    define( 'WP_MEMORY_LIMIT', '256M' );
  • WordPress Updating with FTP

    To update wordpress from admin area, you need to be running web server as the user owning files. This is not possible when you run PHP as apache module.

    To fix this, you need to edit your wp-config.php and add following to end of the file.

    define('FS_METHOD', 'ftpext');
    define('FTP_USER', 'FTP_USERNAME');
    define('FTP_PASS', 'FTP_PASSWORD');
    define('FTP_HOST', 'localhost');
    define('FTP_SSL', false);
    

    You can read about about this at

    https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants

    Above configuration work fine for me when files are inside public_html folder of FTP homed directory.

  • Nginx WordPress

    Here is nginx configuration for wordpress

    server {
        listen 80;
        server_name serverok.in www.serverok.in;
        root /var/www/html;
        index index.php;
        client_max_body_size 100M;
    
        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }
    
        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }
    
        location / {
                try_files $uri $uri/ /index.php?$args;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            proxy_read_timeout 180;
            fastcgi_intercept_errors on;
            fastcgi_buffers 16 16k;
            fastcgi_buffer_size 32k;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
    
        location ~* \.(txt|xml|js)$ {
                expires max;
                log_not_found off;
                access_log off;
        }
    
        location ~* \.(css)$ {
                expires max;
                log_not_found off;
                access_log off;
        }
    
        location ~* \.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac|eot|ttf|otf|woff|svg)$ {
                expires max;
                log_not_found off;
                access_log off;
        }
    
        location ~* \.(jpg|jpeg|png|gif|swf|webp)$ {
                 expires max;
                 log_not_found off;
                 access_log off;
        }
    
        gzip on;
        gzip_http_version  1.1;
        gzip_comp_level    5;
        gzip_min_length    256;
        gzip_proxied       any;
        gzip_vary          on;
        gzip_types
            application/atom+xml
            application/javascript
            application/json
            application/rss+xml
            application/vnd.ms-fontobject
            application/x-font-ttf
            application/x-web-app-manifest+json
            application/xhtml+xml
            application/xml
            font/opentype
            image/svg+xml
            image/x-icon
            text/css
            text/plain
            text/x-component;
    }
    

    Nginx Config with FCGI Cache + gzip compression

    fastcgi_cache_path /tmp/nginx-cache levels=1:2 keys_zone=WORDPRESS:500m inactive=60m;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";
    fastcgi_cache_use_stale error timeout invalid_header http_500;
    
    server {
        listen *:443 ssl http2;
        server_name www.serverok.in serverok.in;
        root /var/www/html;
        index index.php;
        client_max_body_size 100M;
    
        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }
    
        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }
    
        location / {
                try_files $uri $uri/ /index.php?$args;
        }
    
        #fastcgi_cache start
        set $no_cache 0;
    
        # POST requests and urls with a query string should always go to PHP
        if ($request_method = POST) {
                set $no_cache 1;
        }   
        if ($query_string != "") {
                set $no_cache 1;
        }   
    
        # Don't cache uris containing the following segments
        if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
                set $no_cache 1;
        }   
    
        # Don't use the cache for logged in users or recent commenters
        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
                set $no_cache 1;
        } 
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            proxy_read_timeout 180;
            fastcgi_intercept_errors on;
            fastcgi_buffers 16 16k;
            fastcgi_buffer_size 32k;
    
            fastcgi_cache_bypass $no_cache;
            fastcgi_no_cache $no_cache;
            fastcgi_cache WORDPRESS;
            fastcgi_cache_valid 200 60m;
    
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
    
        location ~* \.(txt|xml|js)$ {
                expires max;
                log_not_found off;
                access_log off;
        }
    
        location ~* \.(css)$ {
                expires max;
                log_not_found off;
                access_log off;
        }
    
        location ~* \.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac|eot|ttf|otf|woff|svg)$ {
                expires max;
                log_not_found off;
                access_log off;
        }
    
        location ~* \.(jpg|jpeg|png|gif|swf|webp)$ {
                 expires max;
                 log_not_found off;
                 access_log off;
        }
    
        ssl on;
        ssl_certificate /etc/ssl/serverok.in.crt;
        ssl_certificate_key /etc/ssl/serverok.in.key;
    
        # Enable HSTS. This forces SSL on clients that respect it, most modern browsers. The includeSubDomains flag is optional.
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
    
        # Set caches, protocols, and accepted ciphers. This config will merit an A+ SSL Labs score as of Sept 2015.
        ssl_session_cache shared:SSL:20m;
        ssl_session_timeout 10m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES256-SHA:ECDHE-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:CAMELLIA256-SHA:CAMELLIA128-SHA256;
    
        # Compression
    
        # Enable Gzip compressed.
        gzip on;
    
        # Enable compression both for HTTP/1.0 and HTTP/1.1.
        gzip_http_version  1.1;
    
        # Compression level (1-9).
        # 5 is a perfect compromise between size and cpu usage, offering about
        # 75% reduction for most ascii files (almost identical to level 9).
        gzip_comp_level    5;
    
        # Don't compress anything that's already small and unlikely to shrink much
        # if at all (the default is 20 bytes, which is bad as that usually leads to
        # larger files after gzipping).
        gzip_min_length    256;
    
        # Compress data even for clients that are connecting to us via proxies,
        # identified by the "Via" header (required for CloudFront).
        gzip_proxied       any;
    
        # Tell proxies to cache both the gzipped and regular version of a resource
        # whenever the client's Accept-Encoding capabilities header varies;
        # Avoids the issue where a non-gzip capable client (which is extremely rare
        # today) would display gibberish if their proxy gave them the gzipped version.
        gzip_vary          on;
    
        # Compress all output labeled with one of the following MIME-types.
        gzip_types
            application/atom+xml
            application/javascript
            application/json
            application/rss+xml
            application/vnd.ms-fontobject
            application/x-font-ttf
            application/x-web-app-manifest+json
            application/xhtml+xml
            application/xml
            font/opentype
            image/svg+xml
            image/x-icon
            text/css
            text/plain
            text/x-component;
    }
     
    server {
        listen 80;
        server_name  www.serverok.in serverok.in;
        return       301 https://serverok.in$request_uri;
    }
    
  • WordPress bitnami

    Install LetsEncrypt SSL on Bitnami

    To stop/start service use ctlscript.sh

    root@ip-172-31-26-46:~# /opt/bitnami/ctlscript.sh 
    usage: /opt/bitnami/ctlscript.sh help
           /opt/bitnami/ctlscript.sh (start|stop|restart|status)
           /opt/bitnami/ctlscript.sh (start|stop|restart|status) mysql
           /opt/bitnami/ctlscript.sh (start|stop|restart|status) php-fpm
           /opt/bitnami/ctlscript.sh (start|stop|restart|status) apache
    
    help       - this screen
    start      - start the service(s)
    stop       - stop  the service(s)
    restart    - restart or start the service(s)
    status     - show the status of the service(s)
    
    root@ip-172-31-26-46:~# 
    

    To stop MySQL, run

    root@ip-172-31-26-46:~# /opt/bitnami/ctlscript.sh stop mysql
    Unmonitored mysql
    /opt/bitnami/mysql/scripts/ctl.sh : mysql stopped
    root@ip-172-31-26-46:~# 
    

    To disable Banner on WordPress site, run

    /opt/bitnami/apps/wordpress/bnconfig --disable_banner 1
    

    Disable MySQL in Bitnami instance after moving Database to RDS

    /opt/bitnami/ctlscript.sh stop mysql
    mv /opt/bitnami/mysql/scripts/ctl.sh /opt/bitnami/mysql/scripts/ctl.sh.disabled
    mv /opt/bitnami/config/monit/conf.d/mysql.conf /opt/bitnami/config/monit/conf.d/mysql.conf.disabled
    

    Apache Config

    VirtualHost entry for wordpress is available on file

    vi /opt/bitnami/apache2/conf/bitnami/bitnami.conf
    
  • WordPress Cron

    To disable wordpress cronjob, add

    define('DISABLE_WP_CRON', true);
    

    to end of wp-config.php

    Now you can set a cronjob with following code

    */5 * * * *  cd /var/www/html; php -q wp-cron.php