phpshellcomposer-phpaliasqnap

Add php and composer alias on QNAP Startup


I came across a couple of issues with my QNAP NAS TS-251+ whilst developing a new project these are:

1) There is no php alias and when I add one via command line it is removed on NAS Restart.

2) A similar thing happens for Composer except on restart it removes Composer as well from the system.

How can I stop this from happening or get around it so that when my NAS restarts the php and composer alias are already set.


Solution

  • I managed to resolve this issue by adding a new script that runs when my NAS starts up. QNAP have provided some basic instructions on how to add a startup script on their wiki page under Running Your Own Application at Startup. However I added a couple more steps t

    These steps are fairly basic:

    1. Login to your NAS Server via SSH.
    2. Run the following command mount $(/sbin/hal_app --get_boot_pd port_id=0)6 /tmp/config (Running ls /tmp/config will give you something similar to below)

    <code>mount /sbin/hal_app</code> command output

    1. Run vi /tmp/config/autorun.sh this will allow you to edit/create a file called autorun.sh **

    2. For me I wanted to keep this file as simple as possible so I didn't have to change it much, so the script is just called from with this Shell Script. So add the following to autorun.sh.

    autorun.sh code example:

     #!/bin/sh
     # autorun script for Turbo NAS
     /share/CACHEDEV1_DATA/.qpkg/autorun/autorun_startup.sh start
     exit 0
    

    You will notice a path of /share/CACHEDEV1_DATA/.qpkg/autorun/ this is where my new script that I want to run is contained, you don't have to have yours here if you don't want to however I know the script will not be removed if placed here. autorun_startup.sh this is the name of the script I want to be running, and start is the command in the script I want to be running.

    1. Run chmod +x /tmp/config/autorun.sh to make sure that autorun.sh is actually runnable.

    2. Save the file and run umount /tmp/config (Important).

    3. Navigate to the folder you have put in the autorun.sh (script in my case /share/CACHEDEV1_DATA/.qpkg/autorun/) and create any folders along the way that you need.

    4. Create your new shell file using vi and call it whatever you want (Again in my case it is called autorun_startup.sh) and add your script to the file. The script I added is below but you can add whatever you want to you startup script.

    autorun_startup.sh code example:

    #!/bin/sh
    
    RETVAL=0
    QPKG_NAME="autorun"
    APACHE_ROOT=`/sbin/getcfg SHARE_DEF defWeb -d Qweb -f 
    /etc/config/def_share.info`
    QPKG_DIR=$(/sbin/getcfg $QPKG_NAME Install_Path -f /etc/config/qpkg.conf)
    
    addPHPAlias() {
        /bin/cat /etc/profile | /bin/grep "php" | /bin/grep "/usr/local/apache/bin/php" 1>>/dev/null 2>>/dev/null
        [ $? -ne 0 ] && /bin/echo "alias php='/usr/local/apache/bin/php'" >> /etc/profile
    }
    
    addComposerAlias() {
        /bin/cat /etc/profile | /bin/grep "composer" | /bin/grep "/usr/local/bin/composer" 1>>/dev/null 2>>/dev/null
        [ $? -ne 0 ] && /bin/echo "alias composer='/usr/local/bin/composer'" >> /etc/profile
    }
    
    addPHPComposerAlias() {
        /bin/cat /etc/profile | /bin/grep "php-composer" | /bin/grep "/usr/local/apache/bin/php /usr/local/bin/composer" 1>>/dev/null 2>>/dev/null
        [ $? -ne 0 ] && /bin/echo "alias php-composer='php /usr/local/bin/composer'" >> /etc/profile
    }
    
    download_composer() {
        curl -sS https://getcomposer.org/installer | /usr/local/apache/bin/php -- --install-dir=/usr/local/bin --filename=composer
    }
    
    case "$1" in
        start)
            /bin/echo "Enable PHP alias..."
                /sbin/log_tool -t 0 -a "Enable PHP alias..."
            addPHPAlias
    
            /bin/echo "Downloading Composer..."
            /sbin/log_tool -t 0 -a "Downloading Composer..."
            download_composer
    
            /bin/echo "Enable composer alias..."
            /sbin/log_tool -t 0 -a "Enable composer alias..."
            addComposerAlias
    
            /bin/echo "Adding php composer alias..."
            /sbin/log_tool -t 0 -a "Adding php composer alias..."
            addPHPComposerAlias
    
            /bin/echo "Use it: php-composer"
            /sbin/log_tool -t 0 -a "Use it: php-composer"
    
            ;;
    stop)
            ;;
    
    restart)
            ;;
    *)
            echo "Usage: $0 {start|stop|restart}"
            exit 1
    
    esac
    
    exit $RETVAL
    
    1. Run chmod +x /share/CACHEDEV1_DATA/.qpkg/autorun/autorun_startup.sh to make sure your script is runnable.

    2. Restart your NAS System to make sure the script has been run. After restart for my script I just did php -version via terminal to make sure that the php alias worked and it did.

    (*) With steps 3 and 8 you can either do this via something like WinSCP or continue doing it via command line (SSH). For me I chose to do it via WinSCP but here is the command still for SSH

    I am fairly new to server related stuff so if anyone has a better way cool.