Tag: php

  • CloudLinux PHP Selector on Cpanel Server

    CloudLinux come with PHP Selector, this allow you to select differnt PHP versions. When you enable PHP selector, you should do the following.

    1) EasyApache 4 and bild profile "CloudLinux + All PHP Options + OpCache + mod_lsapi".
    2) MultiPHP Manager > System PHP Version set to any ea-php Version
    3) MultiPHP Manager > System PHP-FPM click "Turn Off" button.
    4) MultiPHP Manager > set INHERIT php version for domain.
    5) Users PHP selector > choose needed alt-php version and modules.
    6) WHM > Feature Manager > default => disable MultiPHP Manager and MultiPHP INI Editor.
    

    Related Posts

    Enable CloudLinux PHP lsapi
    CloudLinux

  • CentOS 7 VestaCP Upgrade PHP to 7.x

    On CentOS 7 VestaCP install PHP 5.6 by default. This is very old version of PHP. To upgrade PHP to latest version 7.x, you can install remi repo.

    Install EPEL repo

    yum install -y epel-release
    

    Install yum-utils

    yum install -y yum-utils
    

    Install remi repo

    rpm -ivh http://rpms.remirepo.net/enterprise/remi-release-7.rpm
    

    Select PHP version you need

    yum-config-manager --enable remi-php73
    

    Here i selected PHP 7.3. You can select differnt PHP versions with commands like.

    yum-config-manager --enable remi-php56
    yum-config-manager --enable remi-php71
    yum-config-manager --enable remi-php72
    yum-config-manager --enable remi-php73
    yum-config-manager --enable remi-php74
    

    Make sure you only enable one PHP version. If you enabled a PHP version from remi repo, disable it with

    yum-config-manager --disable remi-php56
    yum-config-manager --disable remi-php70
    yum-config-manager --disable remi-php71
    yum-config-manager --disable remi-php72
    yum-config-manager --disable remi-php73
    yum-config-manager --disable remi-php74
    

    Once you have desired version enabled, run yum upgrade

    yum upgrade
    

    Or only for PHP, run

    yum upgrade php*
    
  • PHP Script to monitor Apache/php-fpm

    I moved a web site to new dedicated server. But for some reason, php-fpm crashed. I increased the max_children settings, but it happend again. I do not want down time while i am investigating the problem. So i created a PHP script, that will check if site is working or not.

    Script have 2 part.

    health-check.php

    
    

    It is simple PHP script, that get a param and print it.

    This file is placed on root of your web site, so it can be accessed using URL http://yoursite/health-check.php

    monitor-server.php

    Create

    mkdir  /usr/serverok/
    vi  /usr/serverok/monitor-server.php
    

    Add following content

    
    

    On the script, replace YOUR_DOMAIN_HERE with your actual domain name.

    systemctl restart apache2 is for restart apache web server. If you use nginx, replace it. systemctl restart php7.2-fpm restart php-fpm, if you have differnt version of php, you need to change it.

    The script is generate a random number, pass it to health-check.php script. Compared the value returned with generated random number to make sure the value is correct. If web server or php-fpm fail, this check will fail.

    Now set a cronjob

    crontab -e
    

    Add

    */5 * * * * /usr/bin/php /usr/serverok/monitor-server.php
    

    Related Posts

    Server Monitoring

  • Install PHP 7.3 in CentOS 8

    CentOS 8 comes with PHP 7.2. To install PHP 7.3, you need to enable remi repo. You can download remi-release rpm file from

    http://rpms.remirepo.net

    Download and install remi-release rpm

    cd ; wget http://rpms.remirepo.net/enterprise/remi-release-8.rpm
    dnf install remi-release-8.rpm
    

    if you have older php version and don’t want to keep it, uninstall it with

    dnf remove php-gd php-xml php-mbstring php-common php php-odbc php-mysqlnd php-json php-process php-cli php-fpm php-intl php-bcmath php-soap php-pdo 
    

    Install PHP 7.3

    dnf install php73
    

    Set it as default PHP version

    update-alternatives --install /usr/bin/php php /usr/bin/php73 1
    

    Install PHP modules

    dnf install -y php73-php php73-php-gd php73-php-fpm php73-php-pdo php73-php-xml php73-php-json php73-php-imap php73-php-intl php73-php-json php73-php-soap php73-php-bcmath php73-php-xmlrpc php73-php-mysqlnd php73-php-mbstring php73-php-zip
    

    Install php-fpm package

    dnf install php73-php-fpm
    

    set php-fpm to start on boot

    systemctl enable php73-php-fpm
    

    Start php-fpm

    systemctl start php73-php-fpm
    

    Restart Apache

    systemctl restart httpd
    

    PHP 7.3 php.ini located at

    /etc/opt/remi/php73/php.ini
    

    Module directory for PHP 7.3 at

    /opt/remi/php73/root/usr/lib64/php/modules/
    

    Related Posts

    CentOS 8

    PHP

  • Redirect a site to HTTPS using PHP

    This PHP script will redirect website visitors to HTTPS (SSL) URL. You can add this in your index.php of the website

    if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on" ) {
        header("HTTP/1.1 301 Moved Permanently");
        $newUrl = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        Header("Location: $newUrl");
        exit;
    }

    To redirect visitors to a new URL using PHP, use the following PHP code

    $newUrl = "https://NEW-URL-HERE" . $_SERVER['REQUEST_URI'];
    header("HTTP/1.1 301 Moved Permanently");
    Header("Location: $newUrl");
    exit;

    You can also use Apache mod_rewrite .htacess to do the redirection.

  • Large file upload in PHP

    To allow large file upload in PHP, you need to edit php.ini file and change fllowing settings

    memory_limit = 512M
    upload_max_filesize = 800M
    post_max_size = 800M
    max_input_time = 6000
    max_execution_time = 600
    

    If you have Dedicated or VPS, you can edit php.ini file using ssh. To find location of this file, upload a phpinfo page to your server, that will display location of php.ini used by your web site.

    Some control panel like CPanel have option to edit php.ini settings.

  • PHP running system commands with exec

    To execure a system command on a server, use PHP exec function.

    For example

    &1", $result);
    echo "
    ";
    print_r($result);
    

    See php

  • Run .html files as PHP in Apache

    On Ubuntu, to execute .htm files as PHP, create file

    vi /etc/apache2/conf-enabled/php-html.conf
    

    Add following content

    
        SetHandler application/x-httpd-php
    
    

    This is similar code from your PHP configuration. In this case, it is from /etc/apache2/mods-available/php5.6.conf

    If you want it only for a specific website, edit VirtualHost entry for the website and add

    
            SetHandler application/x-httpd-php
    
    

    Example

    
        ServerName serverok.in
        ServerAlias www.serverok.in
        DocumentRoot /home/www/serverok.in/html
        CustomLog ${APACHE_LOG_DIR}/serverok.in.log combined
        
            Options All
            AllowOverride All
            Require all granted
            Order allow,deny
            allow from all
        
        
                SetHandler application/x-httpd-php
        
    
    

    Now restart apache

    service apache2 restart
    

    Apache | PHP

  • Laravel Database Migration Error Key too long

    When doing a database migration on Laravel, I get the following error

    $ php artisan migrate
    
      [Illuminate\Database\QueryException]                                                                                                                                                 
      SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))  
    
      [PDOException]                                                                                                   
      SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes  
    $ 

    This is because MariaDB uses a different UTF8 format.

    To fix, edit file

    vi ./app/Providers/AppServiceProvider.php

    Inside, find

        public function boot()
        {
            //
        }

    Replace with

        public function boot()
        {
            Schema::defaultStringLength(191);
        }

    You will also need to add

    use Illuminate\Support\Facades\Schema;

    here is what my file looks like after editing.

    $ cat ./app/Providers/AppServiceProvider.php
    <?php
    
    namespace App\Providers;
    
    use Illuminate\Support\Facades\Schema;
    use Illuminate\Support\ServiceProvider;
    
    class AppServiceProvider extends ServiceProvider
    {
        /**
         * Bootstrap any application services.
         *
         * @return void
         */
        public function boot()
        {
            Schema::defaultStringLength(191);
        }
    
        /**
         * Register any application services.
         *
         * @return void
         */
        public function register()
        {
            //
        }
    }
    $ 

    Back to Laravel

  • PHP Script to List Enabled Extensions

    Create a php script with following content

    ";
    
    foreach (get_loaded_extensions() as $extn) {
    	echo $extn  . "\n";
    }
    

    See php

  • Install PHP 5.6 on Debian

    Debian 9 come with PHP 7. To install PHP 5.6, run

    apt install apt-transport-https lsb-release ca-certificates -y
    wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
    sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
    apt-get update
    

    Install PHP 5.6 with

    apt install -y php5.6 php5.6-mysql php5.6-gd php5.6-mbstring php5.6-mcrypt php5.6-zip php5.6-curl php5.6-xml
    

    Enable SimpleXML module

    phpenmod -v 5.6 simplexml
    

    Install php-fpm if required

    apt install -y php5.6-fpm
    

    See php