Installable PHP application architecture

Asked

Viewed 640 times

5

I am trying to improve my PHP applications. In this goal I would like to create an installer for a web application, a website for example.

I already know linux packages like rpm and deb, that’s not what I’m talking about, because some servers don’t have access to bash or commands like exec, shell_exec and val.

My intention is to create an application self-installable as a wordpress for example - a package to download and in one click install. Does anyone have any suggestions?

  • 2

    A little broad your question, can you specify? Some point in particular is causing you problems in creating the installer?

  • You quoted wordpress, your installation is actually nothing more than the creation of all the tables that it uses in the database, IE, It is not an "installation" exactly like what you seem to me to be asking...

  • Are you talking about self-installing scripts, or are you talking about the magic of auto-update?

4 answers

14

What you could do is learn from the examples you quoted. What Wordpress does, as well as other PHP applications (phpMyAdmin, Joomla, Drupal...), is put in a . ZIP (or .tar.gz) all the files that would be at the root of the site. Basically, you go to your project’s WWW (public_html) folder and zip.

But then you want the project to install itself... The wordpress approach is the coolest. You create a file install.php or a route, which basically if/else, in the following flow:

  • Database configuration file is ok?
  • I can connect in the bank?
  • There is the whathever table in my bank?

Ai, if you were able to connect nice but a key table of the database does not exist, you run your SQL commands that create your database.

  • I believe that this step by step is cool and also refers to auto-update. But how does it find itself in database and file changes versions? Let’s say version 1.3 added a table and 10 files?

  • 1

    It’s called a "Migration". What you can do is create a table that controls versions and the update.php script (example) that checks that table and runs Sqls from create, alter and drop Tables. Frameworks like Laravel have this built-in Migration feature.

8

Good response from @Frenetic. I would, however, like to add some pointers on how to make a self-instruction.

Main configuration file

Like Wordpress, you will need a basic file configuration that works independent of the bank. Generally the file will contain the database access information, notes to directories and main language.

Sometimes it is possible for the file to be created automatically via an installation screen. You make a screen with the necessary data, the user completes the form and you save the file in the correct location.

But unfortunately, this is not always possible, because in some hosts you cannot write to the file system and/or do not want to give write permission for the scripts. In this case, do as Wordpress: show the configuration file on the screen and ask the user to create a file on the server with the displayed content.

Database Migrations

How much of the database. There are some database migration frameworks, such as:

  • Ruckusing Migrations: follows the philosophy of Ror (Ruby on Rails). Changes in the database are made via PHP code. Supports Mysql and Postgresql.
  • Doctrine Migrations: changes are specified in XML. The Doctrine project supports Mysql, Postgresql, SQL Server, Oracle and others, but I don’t know if migrations work well on all of them.
  • Phinx: Simple PHP Database Migrations: provides another API for migrations via PHP code. See more of the documentation here. Supports Mysql, Postgresql and Sqlite.

Regardless of choosing any of these, some other or even creating another solution "in the nail", it is important to define a method so that your database can always be updated to the most current version when your system runs.

Considerations

Creating a self-installing system is something that can be achieved without many problems.

On the other hand, managing system life cycle updates can be tricky. In addition, an installation with excellent usability, intuitive and friendly as that of Wordpress is something that requires effort.

Remember that WP is an already very traditional system that has been evolving over the years. They know the main problems that occur in different environments and have the know-how to carry out various environmental checks in advance in order to anticipate possible problems. So don’t expect such a good result right away.

3

I note that your main goal is to have a better quality of software, especially a high degree of reuse in your code. And of course, practicality in delivery (deploy).

To improve your PHP applications, I suggest you use and study some framework. So you will know some architectures, good OO strategies, reuse etc.. See this research on the most promising PHP frameworks for 2014 (in English). Some of these frameworks may give you this auto-install feature (or at least a base).

Deploy tools can help in delivery. Many of these frameworks use what’s in their version control (such as git) to pack and deliver your code in a fantastic way -- installing the application, making Migration, updating code etc. And what you want the user to do, It’s not so high level is a hand on the wheel to pack the application and "install" somewhere. I know that the TYPO3 Flow does this with SURF, the Symfony also does.

In parallel, you could poke around in the http://getcomposer.org/, a dependency manager for PHP. This tutorial gives a good view. And there are good examples of packages in https://packagist.org/. In any case, the replies from @utluiz and @Frenetic are great. I would only do this with a framework -- the use accelerates productivity, quality and brings experience. Of course, everything has a counterpoint: in a scale environment, sometimes a framework may not have the best performance for a given solution.

3

From a conceptual point of view, you can reduce your problem to two points: an executable file, and another file containing the others, for example, a zip file.

I will assume that you will use PHP. I would recommend doing everything in one file, even images, JS and CSS are in this file, to avoid

  1. Create a single executable file, which, for portability reasons, should auto-contain all the Javascripts and Csss of the interface. It can be a PHP, Perl, Shell, and related script.
  2. The executable file must pre-validate, how to check if the server supports the application and check that the compressed file containing the application is not corrupted
  3. Your application should be in the archive. The executable file should unpack your application. Then you will have to opt for a two alternatives.
    1. Your single executable file should ask for instructions to install application and le even. Eg: useful when it comes to a full backup
    2. Your single executable file should pass to the screen of your application that will be in charge of the installation in the new platform
  4. After the installation is finished, you should remove the initial executable file and preferably the compressed file with the application

If you need to do something related to the upgrade after the application has been installed, you will need to integrate something into the application that does this. Both Joomla and Wordpress have systems that work exactly doing this.

Regardless of the application, the auto-update part needs to have some kind of minimal integration with the client application.

References

Item 3.1 is used by Akeeba backup, one of the most famous extensions to the Joomla CMS https://www.akeebabackup.com.

Besides, I’ve worked with something similar. It is important that you do it in a way that is as simple as possible for the user, and that gives messages that make them understand that it is the fault of the server that it is or of something that it turns (like corrupted file).

Pro-tip

That kind of question you asked is only worth it if you’re going to do it on a large scale and be economically interesting. If you are not absolutely sure from the start, do it in the simplest way you can imagine and require the least amount of knowledge in the application you are going to use, otherwise you will have a white elephant. The way I explained it is relatively easy to make, does not require high permissions to functions that mess with the system (can be used in any hosting), and has option to do not require to know much about the application you will use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.