PHP Database Connection Test

Testing and Deploying a PHP App in Ubuntu

If you’ve been learning PHP in an online classroom like Codecademy you may find yourself surprised when setting up your workstation for PHP development. Unlike HTML or JavaScript code which will run automatically in your browser, PHP code — even if it works perfectly well in your online IDE — will not work on localhost unless you are running it from a server. Without a server to render your PHP code, your may find your browser prompting you to download the PHP file you’re trying to open, or just displaying the PHP code without rendering it. In technical terms, while JavaScript is a client-side scripting language, PHP is a server-side language. Hence, to get your PHP code working in localhost, you will need to run it with a server. One popular solution is to use a prepackaged stack like XAMPP which will contain Apache (the server), MariaDB (a popular MySQL database), and PHP. Such prepackaged stacks will save you a lot of installation time. However, if your OS is a Linux distribution like Ubuntu, you can take the opportunity to set up a classic LAMP stack (Linux, Apache, MySQL, PHP). Setting this up will however require extra configuration in addition to installing the software. MySQL Let’s start with the database installation. I recommend MariaDB as it offers an optimized installation of MySQL. These commands will install MariaDB and set up the database’s security options according to your preferences:

sudo apt update sudo apt install mariadb-server sudo mysql_secure_installation 

Alt Text

Now that you have an Apache server running on your machine, you will need to configure Ubuntu’s firewall. You can run sudo ufw app list to see the available firewall profiles and sudo ufw app info «Profile Name» to get information on a particular profile: If you like a particular profile, say in this case, Apache Full, you can activate it with the command sudo ufw allow in «Apache Full» You can now open http://localhost/ in your browser to see your Apache server running: However, additional configuration is still needed for Apache to render your PHP code. By default, Apache will open files saved in the /var/www directory. However, this directory is protected and you will need to sudo to write into it. Hence it won’t be good practice to keep your code here. A better solution will be to activate Apache’s User Directory feature, which will allow you to run your PHP code from a new public_html directory in your home folder. The following command will activate the User Directory feature:

Читайте также:  Dropdown in html page

Next, you will need create the User Directory ( ~/public_html ) and set the correct directory permissions:

mkdir $HOME/public_html chmod +x $HOME chmod 755 $HOME/public_html 

You will then need to enable PHP scripting in the User Directory (by default Ubuntu disables this). To do this, first:

Alt Text

When you are in the /etc/apache2/mods-enabled/ directory, use the command ls to look for the PHP configuration file. It will be named php[version_number].conf : In this example, the version number is 7.4 and you will need to edit php7.4.conf to activate PHP scripting in the User Directory:

  php_admin_flag engine Off  

Add an # at the start of each line to deactivate Ubuntu’s disabling of PHP scripting in the User Directory:

# # # php_admin_flag engine Off # # 

Alt Text

The file should now look like this: Press Control-O to save the file and then Control-X to exit the text editor. In that same directory, you will also need to edit another Apache configuration file:

sudo nano /etc/apache2/mods-enabled/dir.conf 
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm 
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm 
sudo systemctl restart apache2 
sudo apt install php libapache2-mod-php php-mysql 

This command will install PHP as well as the packages needed for PHP to access Apache as well as MariaDB. You should now be able to run your PHP code in your new LAMP stack. To test this, write and save a PHP file in your public_html directory. Here’s a very simple — but useful! — PHP script you can use:

Save this in your public_html directory. I have named mine test.php but you can name it anything you want; just make sure the filename has the .php suffix. Open your browser and go the following address (replace username with your Ubuntu username and filename with the name you gave your test file):

http://localhost/~username/filename.php 

In my case, the URL is http://localhost/~alvin/test.php . My LAMP stack has successfully rendered the PHP script in the browser: Alt Text Deploying to Production Given these complicated steps just to render a PHP script in localhost, you might imagine that deploying a PHP app would be similarly complicated, and you would be right! Apps written in PHP will not run on platforms like Netlify which are designed to host static apps. While you can host and run your PHP app on Heroku, it has to be deployed with a composer.json companion file otherwise the deployment will fail. To generate a composer.json file, you will need to use install and use Composer. Please refer to its documentation for instructions on how to do this. The purpose of composer.json is to keep track of the dependencies of your PHP app, but even if your app has no such dependencies, you will still need to generate a composer.json file if you want to deploy your app to Heroku. My Magic 8-Ball PHP app, which is hosted on Heroku, has one such dependency-free composer.json file: Alt Text But it’s important to keep the goal in mind. Once you get past the hurdle of creating a composer.json file, you will have a shiny new PHP app for your portfolio! Alt Text

Источник

Your first PHP-enabled page

Create a file named hello.php and put it in your web server’s root directory ( DOCUMENT_ROOT ) with the following content:

Example #1 Our first PHP script: hello.php

Use your browser to access the file with your web server’s URL, ending with the /hello.php file reference. When developing locally this URL will be something like http://localhost/hello.php or http://127.0.0.1/hello.php but this depends on the web server’s configuration. If everything is configured correctly, this file will be parsed by PHP and the following output will be sent to your browser:

This program is extremely simple and you really did not need to use PHP to create a page like this. All it does is display: Hello World using the PHP echo statement. Note that the file does not need to be executable or special in any way. The server finds out that this file needs to be interpreted by PHP because you used the «.php» extension, which the server is configured to pass on to PHP. Think of this as a normal HTML file which happens to have a set of special tags available to you that do a lot of interesting things.

If you tried this example and it did not output anything, it prompted for download, or you see the whole file as text, chances are that the server you are on does not have PHP enabled, or is not configured properly. Ask your administrator to enable it for you using the Installation chapter of the manual. If you are developing locally, also read the installation chapter to make sure everything is configured properly. Make sure that you access the file via http with the server providing you the output. If you just call up the file from your file system, then it will not be parsed by PHP. If the problems persist anyway, do not hesitate to use one of the many » PHP support options.

The point of the example is to show the special PHP tag format. In this example we used . You may jump in and out of PHP mode in an HTML file like this anywhere you want. For more details, read the manual section on the basic PHP syntax.

Note: A Note on Line Feeds

Line feeds have little meaning in HTML, however it is still a good idea to make your HTML look nice and clean by putting line feeds in. A linefeed that follows immediately after a closing ?> will be removed by PHP. This can be extremely useful when you are putting in many blocks of PHP or include files containing PHP that aren’t supposed to output anything. At the same time it can be a bit confusing. You can put a space after the closing ?> to force a space and a line feed to be output, or you can put an explicit line feed in the last echo/print from within your PHP block.

Note: A Note on Text Editors

There are many text editors and Integrated Development Environments (IDEs) that you can use to create, edit and manage PHP files. A partial list of these tools is maintained at » PHP Editors List. If you wish to recommend an editor, please visit the above page and ask the page maintainer to add the editor to the list. Having an editor with syntax highlighting can be helpful.

Note: A Note on Word Processors

Word processors such as StarOffice Writer, Microsoft Word and Abiword are not optimal for editing PHP files. If you wish to use one for this test script, you must ensure that you save the file as plain text or PHP will not be able to read and execute the script.

Now that you have successfully created a working PHP script, it is time to create the most famous PHP script! Make a call to the phpinfo() function and you will see a lot of useful information about your system and setup such as available predefined variables, loaded PHP modules, and configuration settings. Take some time and review this important information.

Example #2 Get system information from PHP

Источник

How to test a PHP script

PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. PHP runs on all major operating systems, from Unix variants including Linux, FreeBSD, Ubuntu, Debian, and Solaris to Windows and Mac OS X. It can be used with all leading web servers, including Apache, Nginx, OpenBSD servers to name a few; even cloud environments like Azure and Amazon are on the rise.

Below are some of the ways in which a PHP script can be tested.

Testing Simple PHP Script

1. Create a file with the following contents. Give the file a name such as myphpInfo.php:

2. Copy the file to the your webservers DocumentRoot directory, for example – /var/www/html. You may have a different DocumentRoot directory depending on which webserver you are using and the configuration done for it.

3. Change the permissions to 755 (Linux only):

4. Call the file from a browser:

http://Fully-Qualified-Hostname:PORT#/phpinfo.php

Testing a PHP Script that uses Database Connections

1. Create a file with the following contents. Give the file a name such as phpdbchk.php:

     $query = 'SELECT SYSDATE FROM DUAL'; $stmt = ociparse($conn, $query); ociexecute($stmt, OCI_DEFAULT); print 'Checking for the Date and Database Connectivity
'; $success = 0; while (ocifetch($stmt)) < print "Date: " . ociresult($stmt, "SYSDATE") . "
\n"; $success = 1; > if ($success) < print 'Success.

'; > else < print 'Failed to retrieve the date.

\n'; > OCILogoff($conn); print 'PHP Configuration
'; print '======================

'; phpinfo(); ?>

2. Set ORACLE_HOME and TNS_ADMIN to the proper values.

3. Copy the file to the DocumentRoot directory.

4. Modify the variables $username, $password, $database_hostname, $database_port, $database_sid and $database_srvc as necessary for the test system

5. Change the permissions to 755 (Linux only):

6. Call the file from a browser:

http://Fully-Qualified-Hostname:PORT#/phpdbchk.php

The following error occurs if ORACLE_HOME\network\admin\tnsnames.ora is not set up correctly or missing. If it is missing, the one from the database can be copied over and used as is.

Warning: ocilogon(): _oci_open_server: ORA-12560: TNS:protocol adapter error in [oracle_home]\apache\apache\htdocs\phpdbchk.php on line 25 ORA-12560: TNS:protocol adapter error

Running PHP Script to another directory outside of htdocs

For example, if you want to place php scripts to $ORACLE_HOME/Apache/Apache/phpsrc and run them from there via browser e.g http:FQHN:[port]/php/info.php, then do the following:

1. make directory $ORACLE_HOME/Apache/Apache/phpsrc

2. Copy info.php script to $ORACLE_HOME/Apache/Apache/phpsrc

3. Edit httpd.conf and add this line:

Alias /php/ $ORACLE_HOME/Apache/Apache/phpsrc

4. Restart http server and now it should work:

Note: The php script info.php was used as an example, you can use any name you choose for your php scripts

Источник

Оцените статью