• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

php

Ubuntu 20.04 LTS Lamp Server

November 10, 2020

Here’s how to install a LAMP server on the latest Ubuntu 20.04 LTS.

Install Apache.

apt install apache2
systemctl status apache2
systemctl is-enabled apache2

apt install apache2 systemctl status apache2 systemctl is-enabled apache2

Install MariaDB.

apt install mariadb-server mariadb-client
systemctl status mariadb
systemctl is-enabled mariadb
mysql_secure_installation

apt install mariadb-server mariadb-client systemctl status mariadb systemctl is-enabled mariadb mysql_secure_installation

Install PHP.

apt install php libapache2-mod-php php-mysql
systemctl restart apache2

apt install php libapache2-mod-php php-mysql systemctl restart apache2

Enable ssl and mod_rewrite.

a2enmod ssl
a2enmod rewrite
systemctl restart apache2

a2enmod ssl a2enmod rewrite systemctl restart apache2

Filed Under: Linux Tagged With: apache, lamp, lts, mariadb, mod_rewrite, php, ssl, ubuntu 20.04

PHP 7.0 to 7.4 Upgrade

June 28, 2020

Here are steps I took to upgrade from PHP 7.0 to 7.4 on Ubuntu 18.04 LTS.

Login as root, otherwise use sudo for all commands.

# use 3rd party ppa repo
add-apt-repository ppa:ondrej/php
apt -y update
apt -y upgrade
# install php-core
apt install php7.4 php7.4-common php7.4-cli
# add all the php extenstions your app needs
apt install php7.4-curl php7.4-json php7.4-gd php7.4-mbstring
apt install php7.4-intl php7.4-bcmath php7.4-bz2 php7.4-xml
apt install php7.4-readline php7.4-zip php7.4-mysql
# I'm using apache prefork MPM, so install it
apt install libapache2-mod-php7.4
a2enmod php7.4
# check version. Success!
php -v
# for good measure, reboot apache
systemctl restart apache2.service
# Finally, purge the old PHP versions
apt purge php7.0 libapache2-mod-php7.0

# use 3rd party ppa repo add-apt-repository ppa:ondrej/php apt -y update apt -y upgrade # install php-core apt install php7.4 php7.4-common php7.4-cli # add all the php extenstions your app needs apt install php7.4-curl php7.4-json php7.4-gd php7.4-mbstring apt install php7.4-intl php7.4-bcmath php7.4-bz2 php7.4-xml apt install php7.4-readline php7.4-zip php7.4-mysql # I'm using apache prefork MPM, so install it apt install libapache2-mod-php7.4 a2enmod php7.4 # check version. Success! php -v # for good measure, reboot apache systemctl restart apache2.service # Finally, purge the old PHP versions apt purge php7.0 libapache2-mod-php7.0

Finally, run update on more time to get all the latest updates.

apt -y update
apt -y upgrade
apt -y autoremove
apt-get --with-new-pkgs upgrade

apt -y update apt -y upgrade apt -y autoremove apt-get --with-new-pkgs upgrade

Filed Under: Linux Tagged With: 7.0, 7.4, php, ubuntu, upgrade

List Available PHP Modules

March 2, 2020

Here’s how to list the available PHP modules.

apt-cache search php- | less

apt-cache search php- | less

To get details about a module.

apt-cache show php-curl

apt-cache show php-curl

To install a module.

apt install php-curl

apt install php-curl

Or install all modules.

apt install php*

apt install php*

Restart Apache after each install.

systemctl restart apache2

systemctl restart apache2

Filed Under: Linux, PHP Tagged With: install, modules, php

Run Shell Script From Your Website

September 10, 2019

Here’s how to run a shell script from your website. You’ll need 2 files.

Here’s the contents of foo.php. Wrap your output with ‘pre’ for better formatting.

<?php
$output = shell_exec('/var/www/html/bar.sh 2>&1');
echo "$output";

<?php $output = shell_exec('/var/www/html/bar.sh 2>&1'); echo "$output";

Here’s the content of bar.sh. Output will be displayed on web page.

#!/bin/bash
now="$(date +'%y%m%d')"
echo $now
aws s3 ls

#!/bin/bash now="$(date +'%y%m%d')" echo $now aws s3 ls

Filed Under: Cloud, Linux Tagged With: bash, php, run, script, shell, website

Phpinfo Blank Page

September 10, 2019

Here’s how to fix when phpinfo is displaying a blank page. The install is missing this php module.

apt install php libapache2-mod-php

apt install php libapache2-mod-php

Restart after install.

service apache2 restart

service apache2 restart

Filed Under: Linux, PHP Tagged With: blank, page, php, phpinfo

WordPress Numeric Pagination

September 8, 2019

Here’s how to add numeric pagination on your WordPress pages.

Add this code to functions.php.

function pagination($pages = '', $range = 4) {
  $showitems = ($range * 2)+1;
  global $paged;
  if(empty($paged)) $paged = 1;
  if($pages == '') {
    global $wp_query;
    $pages = $wp_query->max_num_pages;
    if(!$pages) {
      $pages = 1;
    }
  }
  if(1 != $pages) {
    echo '<p><div class="pagination"><span>Page' . $paged . ' of  ' . $pages . '</span>   ';
    if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo '<a href="' . get_pagenum_link(1) . '"><< First</a>';
    if($paged > 1 && $showitems < $pages) echo '<a href="'.get_pagenum_link($paged - 1).'">< Previous</a>';
    for ($i=1; $i <= $pages; $i++) {
      if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
        echo ($paged == $i)? '<span class="current">' . $i . '</span>' : '<a href="' . get_pagenum_link($i) . '"class="inactive">' . $i . '</a>';
      }
    }
    if ($paged < $pages && $showitems < $pages) echo '<a href="' . get_pagenum_link($paged + 1) . '">Next ></a>';
    if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo '<a href="' . get_pagenum_link($pages) . '">Last >></a>';
    echo '</div></p>';
  }
}

function pagination($pages = '', $range = 4) { $showitems = ($range * 2)+1; global $paged; if(empty($paged)) $paged = 1; if($pages == '') { global $wp_query; $pages = $wp_query->max_num_pages; if(!$pages) { $pages = 1; } } if(1 != $pages) { echo '<p><div class="pagination"><span>Page' . $paged . ' of ' . $pages . '</span> '; if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo '<a href="' . get_pagenum_link(1) . '"><< First</a>'; if($paged > 1 && $showitems < $pages) echo '<a href="'.get_pagenum_link($paged - 1).'">< Previous</a>'; for ($i=1; $i <= $pages; $i++) { if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) { echo ($paged == $i)? '<span class="current">' . $i . '</span>' : '<a href="' . get_pagenum_link($i) . '"class="inactive">' . $i . '</a>'; } } if ($paged < $pages && $showitems < $pages) echo '<a href="' . get_pagenum_link($paged + 1) . '">Next ></a>'; if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo '<a href="' . get_pagenum_link($pages) . '">Last >></a>'; echo '</div></p>'; } }

To display, comment out ‘the_post_navigation’ function and add the ‘pagination’ function instead in your template files.

//the_posts_navigation();
pagination();

//the_posts_navigation(); pagination();

Code courtesy of webdesignsun.

Filed Under: WP Tagged With: function, numeric, pagination, php, template, wordpress

HTTPS in CodeIgniter

July 17, 2019

If you switched to HTTPS in Apache, make sure to update CodeIgniter’s config file.

vim /var/www/applications/config/config.php

vim /var/www/applications/config/config.php

Change the base URL to https.

$config['base_url']	= 'https://yourdomain.com/ci/';

$config['base_url'] = 'https://yourdomain.com/ci/';

Filed Under: PHP Tagged With: apache, base url, codeigniter, config, https, php

PHP vs Node.js

February 9, 2017

If you would like to launch a new career as a front-end developer, which programming language would you choose? You have multiple options, but nothing could be as popular as PHP and Node.js. An article from ComputerWorld examines the strengths and weaknesses of both languages. Each language has their own pluses and minuses. PHP is simpler, with a deeper code base, easy access to databases, and it’s boosted by HHVM and the Hack language. Node.js is based on Javascript with thinner service calls. It works with JSON, and raw speed is unparalleled. Both languages has it’s own MVC frameworks. Perhaps, a hybrid solution of PHP and Node.js might be the best approach on some projects. It really depends on what you’re trying to achieve. If you want to code fast, you may choose PHP. If application raw speed is in order, then you may want to consider using Node.js.

Filed Under: PHP Tagged With: front-end, node.js, php

  • Go to page 1
  • Go to page 2
  • Go to Next Page »
  • Home
  • About
  • Archives

Copyright © 2023