4 september 2015

Champ: Apache, MySQL & PHP from source on your Chromebook




Champ = CH(romebook) A(pache) M(ySQL) P(hp)


Lamp is pretty popular and a well know term in the Web Developers world, Wamp also exists, and I have used Xampp with pleasure for many years.

But this blog posting is about the real champion: It's about Champ !!!

Ch = Chromebook
a  = Apache
m  = MySQL
p  = PHP

Turn your Chromebook into a low cost Apache/PHP/MySQL development machine, run Apache/PHP/MySQL offline, develop LAMP websites offline.

It started because I wanted to test my own PHP apps with the new PHP 7.0 version that was in in that time in beta stage. I was wondering if I could use my Chromebook for this. YES, that was possible !

( ok ok, the term Champ is technically wrong, I have used Debian in a chroot environment that is setup by Crouton, so it is still Lamp, but the term Champ is just to good not to use it )






Developer mode


First the Chromebook must be put into developer mode, this can be different for the brand Chromebook you have, for me below actions did work.
  • Press <esc> + <reload> + <power> at the same time
  • Press <ctrl> + d
See below page for more information about this step.

https://www.chromium.org/chromium-os/poking-around-your-chrome-os-device#TOC-Putting-your-Chrome-OS-Device-into-Developer-Mode




Install Debian through Crouton


Download Crouton at https://goo.gl/fd3zc and then activate it with below actions.
  • <ctrl> + <alt> + t
  • shell
  • sudo sh ~/Downloads/crouton -r jessie -t cli-extra
The rest of this document assumes you did use 'champ' as primary linux user with the last step above, you will be prompted for this.

Please read more about Crouton at below site, it's a great tool !

https://github.com/dnschneid/crouton/blob/master/README.md




Going into Debian on your Chromebook


After installation you can enter Debian on your Chromebook with below actions
  • <ctrl> + <alt> + t
  • shell
  • sudo enter-chroot
You must do this before each next step.




Get the source archives


I did use below ones, but for sure those version numbers will soon be old ...
  • httpd-2.4.16.tar.gz
  • mysql-5.6.26.tar.gz
  • php-7.0.0RC3.tar.gz
  • phpMyAdmin-4.4.14-all-languages.zip
This page assumes you have those files in the Download folder on your Chromebook, start browsing at below sites, a real champ will have located and downloaded those files in less then 2 minutes.
To compile the sources, we need compilers, build tools & libraries. Below packages will give this.
  • sudo apt-get install gcc g++ make cmake bison zip libaio-dev libapr1-dev libaprutil1-dev libpcre3-dev libxml2-dev libncurses5-dev libmcrypt-dev tidy libtidy-dev




Compile, install & start Apache


First step, the A from Champ, the Apache webserver
  • cd ~
  • tar zxvf Downloads/httpd-2.4.16.tar.gz
  • cd httpd-2.4.16
  • ./configure --with-mpm=prefork
  • make
  • sudo make instal
  • sudo /usr/local/apache2/bin/apachectl start
Wil it work ? Let's try it at below url

http://localhost




Compile, install, configure & start PHP


PHP is next.
  • cd ~
  • tar zxvf Downloads/php-7.0.0RC3.tar.gz
  • cd php-7.0.0RC3
  • ./configure --with-apxs2=/usr/local/apache2/bin/apxs --enable-mbstring --with-mysqli --with-mcrypt --with-tidy
  • make
  • sudo make install
Now we have to do some configuration, "make install" does load the PHP module inside Apache but it does not not connect it to *.php files. Edit the Apache config file with below command.
  • sudo pico /usr/local/apache2/conf/httpd.conf
Locate below line
DirectoryIndex index.html
And change it into
DirectoryIndex index.php index.html
And add below lines just after the loading of the PHP 7 module.
<FilesMatch "\.php">
SetHandler application/x-httpd-php
</FilesMatch>
Restart apache with below command
  • sudo /usr/local/apache2/bin/apachectl restart
Create a PHP test file with below command
  • sudo pico /usr/local/apache2/htdocs/phpinfo.php
and put below content in it.
<?php
   phpinfo();
?>
And got to below url in Chrome

http://localhost/phpinfo.php




Compile, setup and start MySQL


This one will take some time
  • cd ~
  • tar zxvf ~/Downloads/mysql-5.6.26.tar.gz
  • cd mysql-5.6.26
  • cmake .
  • make
  • make install
Time to setup MySQL
  • cd /usr/local/mysql
  • sudo chown -R champ:champ .
  • ./scripts/mysql_install_db --user=champ
  • chown -R root .
This step did also generate the file /champ/my.cnf file, lets edit it a bit, execute below command
  • sudo pico /usr/local/mysql/my.cnf
and add below 2 lines under [mysqld]
bind-address=127.0.0.1
user=champ
The first line makes sure that only your local Chromebook can access MySQL.
The second line defines the runtime user the MySQL deamon runs under.

Starting MySQL
  • cd /usr/local/mysql
  • sudo ./bin/mysqld_safe &
And make it secure with below script
  • cd /usr/local/mysql
  • sudo ./bin/mysql_secure_installation
Accept all defaults (and give twice the password you want for the user root)




Setup the real website.


Lets make our website, as a start, a single PHP page with "Hello world"

Ok ok, a real linux champ will develop websites in a vt100 terminal with emacs, but wannabies like me will use Chrome OS apps like Caret, or a web edit like Codiac, that's why we put our website in ~/Downloads, so that Chrome OS apps like Caret can see it.
  • chmod 755 ~/Downloads
  • mkdir ~/Downloads/champ
  • mkdir ~/Downloads/champ/htdocs
And put a file with the name index.html and below context in the htdocs directory
pico ~/Downloads/champ/htdocs/index.php
Put below content in the file.
<?php
   echo "Hello world";
?>



More Apache configuration


Some more Apache configuration is needed to let Apache use above website and some other little tweeks
  • sudo pico /usr/local/apache2/conf/httpd.conf
First a few changes, find below options in the http.conf file and change it to whats given below.
Listen 127.0.0.1:80
User champ
Group champ
ServerName localhost:80
DocumentRoot "/home/champ/Downloads/champ/htdocs"
Then add below new lines at the end of the httpd.conf file.
<Directory /home/champ/Downloads/champ>
  Options Indexes FollowSymLinks
  AllowOverride None
  Require all granted
</Directory>
Restart apache to activate this new configuration
  • sudo /usr/local/apache2/bin/apachectl restart





phpMyAdmin


Install
  • cd ~/Downloads/champ/htdocs
  • unzip ~/Downloads/phpMyAdmin-4.4.14-all-languages.zip
  • mv phpMyAdmin-4.4.14-all-languages phpMyAdmin
Create the supporting Database
  • cd ~/Downloads/champ/htdocs/phpMyAdmin/sql
  • /usr/local/mysql/bin/mysql --user=root --password=????? < create_tables.sql
Configure
  • cd ~/Downloads/champ/htdocs/phpMyAdmin
  • mv config.sample.inc.php config.inc.php
  • pico config.inc.php 
Change below line to something, every value seems to work.
$cfg['blowfish_secret'] = 'your_biggest_secret';
Go to it

          http://localhost/phpMyAdmin




Starting and stopping MySQL & Apache/PHP


To make standard Linux start scripts, execute below commands.
  • sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
  • sudo cp /usr/local/apache2/bin/apachectl /etc/init.d/apache
And now we can use below standard actions for starting & stopping
  • sudo service mysql start
  • sudo service mysql stop
  • sudo service apache start
  • sudo service apache stop
if you want to automatically start MySQL & Apache/PHP when you go into your chroot with the "enter-chroot" command, add then below 2 lines to /etc/rc.local
  • sudo service mysql start
  • sudo service apache start



Starting in the background & closing the chrosh tab afterwards


If you do the development with Chrome Apps and/or browser based, then there is no need to keep a crosh chrome tab open all the time.

Add the service start commands to /etc/rc.local as described in the previous paragraph. And then you execute below sequence of statements and afterwards close the crosh tab. The -b option submits the chroot to ghe background and the sleep make sure it never ends.
  • <cntrl> <alt> t
  • shell
  • sudo enter-chroot -b sleep infinity
Both the tboth the Crosh and Bash shells remember the commands you execute, so the next times you only need the <arrow up> & <enter> keys for this, it can be done in 2 seconds




Automatic starting MySQL & Apache/PHP


For me the previous paragraph is good enough, I don't need Champ every time I use my Chromebook.

However, it is possible to start Champ automatically after you login in your Chromebook. Add below file as champ.conf to /etc/init , note that this is in the ChromeOS file sytem, not the chrooted linux file system.
start on start-user-session
stop on stopping ui or starting halt or starting reboot
 
script
  sudo enter-chroot sleep infinity
end script
!!! HA !!! You could not do this !!! Even the user root is not alowed to make changes to the root filesystem on ChromeOS. I suggest you leave it this way, I'm sure you like it that your Chromebook is a very secure system in the bad bad internet world.

Ok ok, of course you are not going to listen to the my good advice as given in the previous line, on below page you can find a script that will make the root file system on your Chromebook writable.

Poking around your Chrome OS Device

Please see also below page, that page covers in dept making the ChromeOS root file system writable and starting the Chroot environment automatically.

Autostart crouton chroot at ChromeOS startup

If you are going to use above page to autostart your Chroot, then below settings must be done in ~/Downloads/crouton.init to match the setup as given in this blog posting.
CHROOT=jessie
START_DE=enter-chroot
CHROOT_APP=sleep infinity
XMETHOD=default



Geen opmerkingen: