Install and Configure Gearman Job server for PHP on Ubuntu 16.04
Gearman is great framework for doing job scheduling, load balancing and call functions of other languages. Greaman can do forground jobs, background jobs, and parallel execution of multiple tasks to speed up the execution. So gearman act as nervous sytem for distributed systems. Learn more about Gearman here.
This article describes how to configre Gearman for PHP on Ubuntu 16.04.
Install Apache
To run our PHP code, we need a server. PHP is supported by many webserves. Here we are using the popular Apache webserver.
If you have already installed Apache, skip this step.
sudo apt-get update
sudo apt-get install apache2
Set Global ServerName to Suppress Syntax Warnings
Check if there any syntax erros in apache configuration
sudo apache2ctl configtest
If you get following output
Output
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK
Open up the main configuration file with your text edit:
sudo nano /etc/apache2/apache2.conf
Inside, at the bottom of the file, add a ServerName directive, pointing to your primary domain name. If you do not have a domain name associated with your server, you can use your server's public IP address.
If you don't know your server's IP address, skip down to the section on how to find your server's public IP address to find it.
/etc/apache2/apache2.conf
. . .
ServerName serverdomainor_IP
Save and close the file when you are finished.
Next, check for syntax errors by typing:
sudo apache2ctl configtest
Since we added the global ServerName directive, all you should see is:
Output
Syntax OK
Restart Apache to implement your changes:
sudo systemctl restart apache2
Adjust the Firewall to Allow Web Traffic
Next, assuming that you have followed the initial server setup instructions to enable the UFW firewall, make sure that your firewall allows HTTP and HTTPS traffic. You can make sure that UFW has an application profile for Apache like so:
sudo ufw app list
Output
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH
If you look at the Apache Full profile, it should show that it enables traffic to ports 80 and 443:
sudo ufw app info "Apache Full"
Output
Profile: Apache Full
Title: Web Server (HTTP,HTTPS)
Description: Apache v2 is the next generation of the omnipresent Apache web
server.
Ports:
80,443/tcp
Allow incoming traffic for this profile:
sudo ufw allow in "Apache Full"
You can do a spot check right away to verify that everything went as planned by visiting your server's public IP address in your web browser (see the note under the next heading to find out what your public IP address is if you do not have this information already):
http://yourserverIP_address
You will see the default Ubuntu 16.04 Apache web page, which is there for informational and testing purposes. It should look something like this:
Ubuntu 16.04 Apache default
If you see this page, then your web server is now correctly installed and accessible through your firewall.
How To Find your Server's Public IP Address
If you do not know what your server's public IP address is, there are a number of ways you can find it. Usually, this is the address you use to connect to your server through SSH.
From the command line, you can find this a few ways. First, you can use the iproute2 tools to get your address by typing this:
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
This will give you two or three lines back. They are all correct addresses, but your computer may only be able to use one of them, so feel free to try each one.
An alternative method is to use the curl utility to contact an outside party to tell you how it sees your server. You can do this by asking a specific server what your IP address is:
sudo apt-get install curl
curl http://icanhazip.com
Regardless of the method you use to get your IP address, you can type it into your web browser's address bar to get to your server.
Step 2: Install MySQL
Now that we have our web server up and running, it is time to install MySQL. MySQL is a database management system. Basically, it will organize and provide access to databases where our site can store information.
Again, we can use apt to acquire and install our software. This time, we'll also install some other "helper" packages that will assist us in getting our components to communicate with each other:
sudo apt-get install mysql-server
Note: In this case, you do not have to run sudo apt-get update prior to the command. This is because we recently ran it in the commands above to install Apache. The package index on our computer should already be up-to-date.
Again, you will be shown a list of the packages that will be installed, along with the amount of disk space they'll take up. Enter Y to continue.
During the installation, your server will ask you to select and confirm a password for the MySQL "root" user. This is an administrative account in MySQL that has increased privileges. Think of it as being similar to the root account for the server itself (the one you are configuring now is a MySQL-specific account, however). Make sure this is a strong, unique password, and do not leave it blank.
When the installation is complete, we want to run a simple security script that will remove some dangerous defaults and lock down access to our database system a little bit. Start the interactive script by running:
sudo mysqlsecureinstallation
You will be asked to enter the password you set for the MySQL root account. Next, you will be asked if you want to configure the VALIDATE PASSWORD PLUGIN.
Warning: Enabling this feature is something of a judgment call. If enabled, passwords which don't match the specified criteria will be rejected by MySQL with an error. This will cause issues if you use a weak password in conjunction with software which automatically configures MySQL user credentials, such as the Ubuntu packages for phpMyAdmin. It is safe to leave validation disabled, but you should always use strong, unique passwords for database credentials.
Answer y for yes, or anything else to continue without enabling.
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No:
You'll be asked to select a level of password validation. Keep in mind that if you enter 2, for the strongest level, you will receive errors when attempting to set any password which does not contain numbers, upper and lowercase letters, and special characters, or which is based on common dictionary words.
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
If you enabled password validation, you'll be shown a password strength for the existing root password, and asked you if you want to change that password. If you are happy with your current password, enter nfor "no" at the prompt:
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n
For the rest of the questions, you should press Y and hit the Enter key at each prompt. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MySQL immediately respects the changes we have made.
At this point, your database system is now set up and we can move on.
Step 3: Install PHP
Ubuntu 16.04 comes with PHP7 as the standard, so there are no PHP5 packages. But Greaman has no support for PHP7. So we have to add PPA to get those packages.
Remove all the stock php packages
List installed php packages with dpkg -l | grep php| awk '{print $2}' |tr "\n" " " then remove unneeded packages with sudo aptitude purge yourpackageshere or if you want to directly remove them all use :
sudo aptitude purge dpkg -l | grep php| awk '{print $2}' |tr "\n" " "
Add the PPA
sudo add-apt-repository ppa:ondrej/php
Install your PHP Version
sudo apt-get update
sudo apt-get install php5.6
Verify your version
sudo php -v
Install Required PHP5.6 Modules
sudo apt-get install php5.6-mbstring php5.6-mcrypt php5.6-mysql php5.6-xml php5.6-cli libapache2-mod-php5.6 php-pear php5.6-dev
Step 4: Install Gearman
sudo apt-get install gearman-job-server
sudo apt-get install libgearman-dev
sudo apt-get install libgearman7
Step 5: Install Gearman PHP extension using PECL
sudo pecl install gearman
edit /etc/php/5.6/cli/php.ini and /etc/php/5.6/apache2/php.ini
and add extension="gearman.so" at the end of file.
Then restart apache
sudo service apache2 restart
Now test gearman installtion by
php gearman_version.php
if it returns version No. then installation is correct.
Step 6: Try examples
Now try this examples at http://gearman.org/getting-started/#gearman-php-extension
Step 7: Configure worker to run background using supervisrod
This sections needs to be updated. To Do - learn about upstart and supervisrod. Use following with caution
Now configure php workers to run in background using supervisord
http://stackoverflow.com/questions/5518724/constantly-running-gearman-worker
See
https://gist.github.com/droidlabour/96febc0c37ba1a370b26
http://stackoverflow.com/questions/2036654/run-php-script-as-daemon-process
for reference
Step 5 (optional): Setup PhpMyadmi
https://www.digitalocean.com/community/articles/how-to-install-and-secure-phpmyadmin-on-ubuntu-12-04
References