Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Thursday, October 21, 2021

local machine (localhost) LAPP, email

Both of these must be entered for the monitor to remain on during inactivity. Neither command by itself is enough to keep the monitor alive.

$ xset -dpms
$ xset s noblank

With these two entered, the screen will still be receiving a signal, but it's just for the backlight, not for any display conent. If we want the display content to remain during inactivity, we must do the two above AND add the following.

$ xset s off

Do we even need this project? Re the LAPP: "no, but helpful". Re the email: probably "yes, for understanding dashboard alerts". LAPP or any other monolithic CMS (XAMP, LAMP) that require learning PHP might be a waste if we can chain cloud services and so on (eg. read comments under this video).

Since LAPP elements are servers, they typically require user switching, individual configuration, or other permission issues. A separate post will deal with production configuration like that. I wrote this one aiming for a localhost light development environment (besides Docker). Additionally, I've attempted to view each LAPP element independently, in case we learn of an app that requires only one element, eg the PHP server, or just a relational database. I also subbed out the Apache "A" for another "L", lighthttp: LLPP. More commonly though, even browser based apps (eg Nextcloud - go to about 8:30) still use a CMS LAMP, LAPP, LNPP, etc. Electron, Go, Docker, won't be covered here.

LAPP

Linux (L)

For both LAPP and email, verifying /etc/hosts is properly configured for IPv4 and 6 for localhost smoothness. Otherwise we typically already login as some user, so we shouldn't have permission issues if everything else is well configured.

PostgreSQL (P)

# pacman -S postgreqsl
# pacman -Rsn postgresql

Lunatic designers really really really don't want a person running this on their own system under their own username. There's no security advantage to this, just poor design. This is almost more difficult than the design of the database itself.

After installing with pacman, add one's username to the postgres group in /etc/group, then initialize the database. Use...

$ initdb -D /home/foo/postgres/data

...instead of the default:

$ initdb -D /var/lib/postgres/data

Attempt startup.

$ postgres -D '/home/foo/postgres/data'

If starting-up gives the following error...

FATAL: could not create lock file "/run/postgresql/.s.PGSQL.5432.lock": No such file or directory

...run the obvious

# mkdir /run/postgresql
# chown foo:foo /run/postgresql

Just do the above, don't believe anything about editing service files or the postgresql.conf file in /data. None of it works. Don't even try to run it as a daemon or service. Just start it like this:

$ postgres -D '/home/foo/postgres/data'

Now that the server is up and running, you can open another terminal and run psql commands. If you run as user postgres (see below), you'll have admin authority.

$ psql postgres

If a lot of data has been written, a good exit phrase to make sure all is safely written:

$ psql -c CHECKPOINT && pg_ctl stop -m fast

PHP (P)

Link: CLI flags ::

# pacman -S php php-pgsql
# pacman -Rsn php php-pgsql

PHP server setup (8:04) Dani Crossing, 2015. Old, but not outdated.
php.ini basics (7:25) Program With Gio, 2021.

If we've got a dynamic website, we typically need PHP (server) and then some JavScript(browser/client) to help display it. The less JavaScript the better (slows browser). I consulted the Arch PHP Wiki .

standalone PHP - start/stop

Standalone PHP is easier to troubleshoot than when starting Apache simultaneously. Stopping is CTRL-C in the terminal where it was started. Startup with web-typical TCP port 80, however leads to a permission issue.

$ php -S localhost:80
[Fri Oct 23 11:20:04 2020] Failed to listen on localhost:80 (reason: Permission denied)

Port 80 works when used with an HTTP server (Apache, NGINX, etc), but not standalone PHP (not sure why). So, use any other port, eg port 8000, and it works.

$ php -S localhost:8000
[Fri Oct 23 11:20:04 2020] PHP 8.0.12 Development Server (http://127.0.0.1:8000) started

See this to determine when best to use index.html, or index.php. But there are at least 3 ways for PHP to locate the index.html or index.php file:

  1. before starting the server, CD to the folder where the servable files are located
  2. specify the servable directory in the startup command
    $ php -S 127.0.0.1:8000 -t /home/foo/HTML
    $ php -S localhost:8000 -t ~/HTML
  3. edit /etc/php/php.ini to indicate file locations...
    # nano /etc/php/php.ini
    doc_root = "/home/foo/HTML"
    ... however this strategy can lead to mental illness and/or lost weekends: sometimes the ini file will not be parsed. Good luck.

First, take a breath. then verify which ini file is being used.

$ php -i |grep php\.ini
Configuration File (php.ini) Path => /etc/php
Loaded Configuration File => /etc/php/php.ini

If you have modified the correct ini file, hours and hours of finding the correct syntax for

standalone PHP - configuration and files

Links: PHP webserver documentation :: Arch PHP configuration subsection

PHP helpfully allows us to configure a document root, so I can keep all html files (including index.htm) inside my home directory. The open_basedir variable inside the configuration file (/etc/php/php.ini) is like a PATH command for PHP to find files. Also, when pacman installs PHP dependent programs like phpwebadmin or nextcloud it default links them to /etc/webapps, because this is a default place PHP tries to find them. Even though they are installed into /usr/share/webapps. So if I had a folder named "HTML" inside my home directory, I'd want to at least:

# nano /etc/php/php.ini
open_basedir = /srv/http/:/var/www/:/home/foo/HTML/:/tmp/

email local

Link: simple setup :: another setup :: notmuch MTA setup

$ mkdir .mail

We also need a mail server on the machine but which only sends to localhost and then only to one folder in /home/foo/.mail/ inside localhost. However, instead of setting up local email alerts, why not skip all of that (for now), and run a log analysis program like nagios?

We want to configure the lightest localhost system setup for email that we can read on a browser. Once we can reliably do alerts on, eg. systemd timers, or some consolidating log app (eg,Fluentd), or some Python/Bash script triggers, other things are possible. Our simplest model takes timer script outputs and sends email to the local system (out via SMTP port 25).

CLI suite

Linux has some default mail settings: mail to root is kept at /var/mail/root, user typically in ~/mail. It appears we'll need a webserver, no small email systems have this built-in. Appears Alot can be our MUA. It is available on the repositories, and can be configured with other light localhost services as described here.

cli configuration

browser MUA

Once the above configured, we can use a browser MUA. For Chromium, the Chrome store, has an extension for notmuch, called Open Email Client. configuration information is provided.

Thursday, April 1, 2010

PHP - MySQL arrays

PHP is a glue for many web sites. It shapes the pages of the browser interface, and also accomplishes interactions with the database, when needed.

arrays
The grist of Web activities involve databases. What comes out of and goes into a database during a web transaction often requires additional manipulation. Many times, these processes require arrays. Suppose we're using MySQL as a database, calling a few records to compare with some user-entered data. Sometimes the data can be manipulated on the hosting database server. More often, data is retrieved as an array and needs to be managed after it has been extracted. For example, it might be extracted with a PHP command, manipulated, and then the database might subsequently be updated.

Manipulating arrays is a core and somewhat complex PHP activity. Except for the complexity of security, arrays play a role in the most complex PHP functions. As noted above, user-entered data might be arriving from the browser side at the same time that info from a database is being extracted. In such a case, three or more arrays, even if temporary, might be created with PHP. PHP would also accomplish the comparisons, increments, and so on, that would take place with these arrays. If one can understand and write scripts to manipulate arrays, a programmer can quickly learn less complicated PHP functions.

Here are some resources for learning array-related PHP skills:
Dovlet Tatlok
WebMaster World
PHP Freaks
Big Resource
PHP Builder
reading directories

Monday, March 29, 2010

Python - PHP - PostgreSQL - Android - C++

Links: Python GUI thread   wxPython tutorial

A lot to learn, but probably the only way to properly get to the point where I can catalog files. PHP first (web-based), Python next (desktop), PostgreSQL (desktop, but growing on web), and then step-up from Python to C++. C++ is apparently more efficient than Python, but takes deeper knowledge. I'm not fond of Java until its Run Time Environment becomes more refined and stable.

Python flavors
The issue with Python is apparently that, once one gets to a GUI level with it, there are two main flavors. One, TKinter is essentially Python using the TK/GTK libraries. wxPython has its own library set. It seems that wxPython is more on a growth path than the older Tkinter. I read one description that said so. So far, I've been happy with wxPython.

Python/Postgresql
This does not seem problematic. It imports a module called psycopg2 which seems to have hooks for the DB. At this site, there is a simple tutorial about how to make the connection.

For programming, using arrays is the real.

Python/MySQL
Don't care, but have to learn it.

PHP
Difficult part is using arrays in best possible ways.

Saturday, February 27, 2010

more data fun - PaperHater

Links: Common MySQL Commands  WizzyWeb   VistA database

I've worked on going paperless since about 2002. It's a maddeningly dull, time consuming affair if one is not a computer programmer capable of designing a database. I am not. Just a person, a computer user. I call this project "PaperHater".

schema

critical, tax, code, projects, auto,

categories

Can it be done without a database? We have the Dewey Decimal (since 1876) and Library of Congress system for books and mags (ISBN, ISSN), so we can use their system to an extent, possibly for our library and citations grouping. But at home I have receipts from vehicles, medical lab reports, HS diploma, and on. This is also not exactly business style records management, since the categories include personal areas. Probably benefits from electronic records management (ISO 15489). File naming conventions, folder conventions and metadata conventions. Yet still might end up requiring a database.

file naming conventions (10:00) Simpletivity, 2018. Ad first 1:36. Uses 3 part naming, succinctly described. Probably best at .75 speed. Comments excellent.
Ffmpeg advanced techniques pt 2 (1:23:57) Nicholas Andre 2013. Windows-based lecture, but clear thinking fellow gives good context for what we're after. Backgrounder. Corning (NY) Community College course.

clutter issues

By mid-2002, I was regularly scanning. Everything from bank statements to letters from my deceased grandmother. I learned that the number and variety of the generated files grew rapidly and could not easily be organized; I was spending significant time categorizing and renaming the files using customary file-naming and folder-naming conventions. In spite of this effort, and with only a couple thousand files (albeit growing in number), I had to admit to myself that I was wasting more time arranging and not finding files than I previously had spent hassling with the inconvenience of paper documents. What good was the computer accomplishing for me?

A proper database sitting between thousands of stored electronic files, and the user attempting to retrieve one of those files, appeared to make all the difference. However, since an immediate solution looked neither likely nor affordable at that time, I reverted to storing documents in bankers boxes. I did this for several years, but with began to slowly acquaint myself with database programs.

By 2009, I had become routinely frustrated with paper accumulation again. Stuck between a computer and a pile of papers, there appeared to be no easy way out. Since early 2009, alternating Saturdays have been spent reading and learning installation processes, webserver configurations, and the like -- LAMP stuff. It seems to have paid off. I'm roughly at the 2/3 mark on this project. Over the next several months, I intend to add an entry here and there about PaperHater's progress to summarize for myself and to possibly help other home users who might be attempting to design and implement something similar on their own system(s). Good luck to all of us. It would be helpful if expert organizations with CMS-type experience could release reasonably priced PC/Mac database solutions instead of gouging us.

step 1

Design the database. I mostly use localhost for this, since it's fast and secure, but sometimes run things on the cloud server. In other words, I spent the many weekends required to learn how to configure reliable LAMPs on both my localhost and server site, and to have them running with the permissions and tweaks (think, eg. php.ini, config.inc.php, etc) I wanted. Following that portion came additional reading, head-scratching, and back-of-envelope sketches; then downloading and installing working applications. Finally, using phpMyAdmin and MySQL commands, I reviewed and modified table structures until I could determine a desirable schema. I also worked on php scripts. Eventually, I had a set of working databases, some copied to .sql scripts, and some partially completed php scripts. That's mostly where it stands now, but I'll move on to describe what's going to come next.

step 2

Copy the structure of a localhost or online database I've designed and tested and want to use for interaction. We put this into an .sql script.
(localhost)$ mysqldump --no-data database >/home/foo/database_template.sql

(online)$ mysqldump -h whateversite.com -uuser -ppassword --no-data -D database >database_template.sql

Then go to whatever web address I keep databases on, create a new database there (thanks to this site), and give it the structure we want from the .sql script.

$ mysql -h whateversite.com -uuser -ppassword
mysql> CREATE DATABASE newdatabase;
mysql> USE newdatabase;
mysql> SOURCE /home/foo/database_template.sql;
mysql> quit;


The new database is now ready to accept data-entry and to run queries.

step 3

Upload some php scripts. I've written a large percentage of the interfacing php scripts but, today, I learned about WizzyWeb, a $99 product which could help tune my scripts or create similar ones quickly. This seems like a very reasonable price if it does what's advertised. That is, if it saves me two hours, I've made-back my money.

step 4

Enjoy. But remember: the PHP, Postgresql/Mysql,Apache, etc. are all on the server side. Users still must create some Javascript additions for their served pages to influence the browser client. However, it is possible to write such skillful PHP code on the server side, that a client needs very little browser-side code to have a nice experience on the site. And remember that server-side is usually more secure since it can't be hacked without hacking the site, not just a webpage. Here's a list of some pre-built server-side PHP "UI Frameworks" written in PHP (as opposed to Java or .Net). Edit: Also, Sitepoint's poll results from 2015 for PHP frameworks.

PHP vs Python vs Java vs C#

Java no way, since the 2011 Oracle purchase. C# no way, since it's more or less locked to Microsoft's .NET framework. But Python now has modules which allow it to work on a server (of course it can still be used to code stand-alone programs too). PHP is designed to work on a webserver inherently, and so is more naturally integrated into HTML, but if one wants to be consistent, they could just do everything with (open-source) Python, using its server side add-on modules (scroll down to "Compared as Web Development Frameworks"). Even low-end ISP's like APlus, which has a terrible lag implementing anything, has PHP4/5 and Python capacity.

Wednesday, February 17, 2010

accounting...and php

Links: IonCube loader instructions

Thought I'd try that Nola Pro software and see what it's all about to put something enterprise level on a localhost, or what kind of security holes it opens. Blah blah blah. Along the way, I fell prey to a php problem I was embarrassed to have overlooked, and got to try-out the IonCube code obfuscator w/associated configuration challenges.

Figure 1

Ion Cube's product is a code obsfucator, one that makes it nearly impossible for visitors to determine the underlying code of a website's pages, sort of like Zend. NolaPro requires the IonCube loader as a prerequisite for NolaPro's installation. The IonCube loader is free, the complete IonCube package is not. This business model reminded me of the early PDF era when a free Adobe Reader was provided to read PDF files, but Adobe required that Acrobat be purchased, at a significant price, to produce them. At any rate, I navigated to Ion Cube's main site, then to "Products->Free Loaders for Encoded Files" and downloaded the latest .tgz. Instructions were to untar it and move its entire folder into /var/www/htdocs. The folder appeared to contain a group of libraries.

To install NolaPro itself, I untarred the NolaPro .tgz in my home directory. From there, the instructions were to move the entire folder into /var/www/htdocs. It also suggested full "777" permissions on this folder, which I don't like to do. I made sure Apache was on and pointed my browser to http://localhost/nolapro. From there NolaPro's installer displayed the necessary php.ini settings. Here's a summary of its php complaints.

Figure 2

I thought that mbstring was properly configured in my php.ini, so that was weird, along with the gd thing. It seemed an odd diagnostic at first....

php embarrassment
A check with http://localhost/phpinfo.php soon solved one problem. How could I have overlooked it not loading my php.ini file? Idiot.
Figure 3
I remedied this problem by copying php.ini to /usr/local/lib/php.ini. But, about this time, I also recalled I had initially installed php v.5.28 on my system, and had subsequently upgraded to 5.3. Although it can't be seen in the the photo clip above, http://localhost/phpinfo.php noted version 5.3 was what Apache was reporting. However checking $ php -v yielded the following:
PHP 5.2.8 (cli) (built: Jan 9 2009 16:26:32)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
Nice -- a mixed-up set of installations. Additionally, when I compiled the module last year, I apparently overlooked gd and mbstring support:
'./configure' '--with-apxs2=/usr/sbin/apxs' '--with-pgsql=/usr/local' '--with-mysql=/usr/share' '--with-libxml-dir=/usr/lib' '--with-curl=/usr/bin/curl' '--with-zlib' '--with-gettext' '--with-gdbm' '--enable-inline-optimization' '--enable-track-vars'

Accordingly I went back to my php source folder, did a $ make clean and reconfigured, made, and #made installed the php module. This time, used all of the above and added --with-gd --enable-mbstring into the configure command. Success.

more IonCube
With the php problems resolved, NolaPro was ready to install except for the IonCube loader (Figure 2). IonCube provides a diagnostic script, ioncube-loader-helper.php , but this was displaying a blank page when I attempted to load it. I then copied the IonCube folder to both the nolapro folder and the htdocs folder as possible solution. No change. In the end, ensuring I had loader files corresponding to my processor was the answer. I thought I could run loader files that that noted 64-bit processing on my 32-bit system, and that these loader files would be back-compatible. They weren't. Once I dropped to the 32-bit version, ioncube-loader-helper.php displayed a successful test screen. Just to be certain, I also updated /usr/local/lib/php.ini with

zend_extension = /var/www/htdocs/ioncube/ioncube_loader_lin_5.3.so

I used loader 5.3 which corresponded to my php release; others should change it to the loader for their php release.

snagware
NolaPro was finally ready to install and did so routinely. At first look, NolaPro seems pleasing. It also appears to be a foot in the door to lure users into purchasing premium add-ons. If that's right, it's not freeware, but more like crippleware. I'll take a closer look over the coming week. It was a good experience insofar as I gained additional experience with php and IonCube configurations.

Wednesday, September 16, 2009

layman data III

Helpful links: **Google MySQL Primer CERT Bulletins Webmaster World Forum Simulate foreign keys - MyISAM Cascading and key constraints - MyISAM, InnoDB, NDB Create tables using PHP script
This is the third in the series, though not meant as a coherent progression. A random collection of tidbits or crumbs to follow. Recently: * Cascading and foreign key constraints with different engines. My webhoster provides only the MyISAM engine, so no foreign keys. Foreign keys are the "relation" in an RDBMS, auto-updating child relations when a parent is updated, cascading inserts and deletes, and so on. This apparently can be approximated in a number of ways in MyISAM. TRIGGERs can be created, loops which do multiple inserts, etc. The InnoDB engine makes this process native from the time of creating the tables. Much easier. To switch between engines in existing tables, we use: ALTER TABLE tablename TYPE = MyISAM; * Added CERT link above. The CERT bulletin link above quickly reveals the many injection threats arising each week. It appears one has to lock-down the code of a production server which, in turn, apparently requires time and patience to learn and implement. * Scripts to install tables. Appear to format as .sql dump files but without the data inside. * Proper documentation, once this is more focused and defined. So far, a simple RTF file using underline for primary, and italic for foreign key, has been helpfully direct. Seen it elsewhere too, but read it in Welling, L., Thomson, L. (2008). PHP and MySQL® Web Development, Fourth Edition. Addison-Wesley Professional. pg 208-209 informit link ~$50. REFERENTIAL INTEGRITY