Ensure you have a fresh Ubuntu installation with sudo privileges.
Before installing new software, update your system packages:
sudo apt update && sudo apt upgrade -yInstall essential utilities like unzip and p7zip:
sudo apt-get install unzip -y
sudo apt-get install p7zip -yInstall the Apache web server:
sudo apt install apache2 -yEnsure Apache is running:
sudo systemctl status apache2If Apache is not running, start it:
sudo systemctl start apache2First, add the ondrej/php repository, which provides multiple PHP versions:
sudo add-apt-repository -y ppa:ondrej/php
sudo apt updateNow, install PHP 8.0:
sudo apt install php8.0 -yVerify the installation:
php -vInstall necessary PHP extensions required for the project:
sudo apt install php8.0-dom php8.0-gd php8.0-intl php8.0-mbstring php8.0-xml php8.0-xsl php8.0-zip -y
sudo apt-get install php8.0-curl php8.0-pdo-mysql php8.0-bcmath -yNavigate to the web root directory and create an /api directory:
cd /var/www/html/
sudo mkdir /api
cd /apiCreate the number_classify.php file:
sudo nano number_classify.phpAdd the following sample code to classify-number.php:
paste the number_classify.php code insideSave the file (CTRL + X, then Y, then ENTER).
Test the API by visiting:
http://34.228.212.209/api/number_classify.php?number=371
Navigate to the Apache configuration directory:
cd /etc/apache2/sites-available/Remove the default Apache configuration file:
sudo rm 000-default.confCreate a new 000-default.conf file:
sudo nano 000-default.confAdd the following configuration:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
<Directory /var/www/html/>
AllowOverride All
Require all granted
Options Indexes FollowSymLinks
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>Save the file (CTRL + X, then Y, then ENTER).
Restart Apache to apply changes:
sudo systemctl restart apache2This script processes a given number and classifies it based on specific properties such as being prime, perfect, Armstrong, even, or odd. Below is an explanation of its components:
-
Headers for CORS and JSON Response:
header("Access-Control-Allow-Origin: *"); header("Content-Type: application/json");
These lines ensure that the API can be accessed from any domain and that the response is in JSON format.
-
Prime Number Check:
function is_prime($n) { if ($n <= 1) return false; for ($i = 2; $i <= sqrt($n); $i++) if ($n % $i == 0) return false; return true; }
This function checks whether a number is prime by iterating up to its square root.
-
Perfect Number Check:
function is_perfect($n) { if ($n <= 1) return false; $sum = 1; for ($i = 2; $i <= sqrt($n); $i++) if ($n % $i == 0) $sum += $i + ($n / $i); return $sum == $n; }
This function determines if a number is perfect by summing its divisors.
-
Armstrong Number Check:
function is_armstrong($n) { $num = abs($n); $digits = str_split((string)$num); $power = count($digits); $sum = array_sum(array_map(fn($d) => pow($d, $power), $digits)); return $sum == $num; }
An Armstrong number is one where the sum of its digits, each raised to the power of the number of digits, equals the original number.
-
Processing User Input:
$input = $_GET['number'] ?? null;
The script retrieves the number parameter from the URL.
-
Validating Input:
if (!is_numeric($input) || !ctype_digit((string)abs($input))) { http_response_code(400); echo json_encode(["number" => $input, "error" => true]); exit; }
Ensures that the provided input is a valid number.
-
Determining Properties:
$properties = []; if (is_armstrong($number)) { ... } elseif (is_prime($abs_num)) { ... } elseif (is_perfect($abs_num)) { ... } $properties[] = ($abs_num % 2 == 0) ? "even" : "odd";
Classifies the number based on its properties.
-
Returning the Response:
echo json_encode([...]);
The results are returned as a structured JSON response.
You have successfully set up an Apache server with PHP 8.0 and implemented a number classification API. Test the setup using the provided API endpoint.
For troubleshooting, check Apache logs:
sudo tail -f /var/log/apache2/error.logWhen uploading, use:
http://34.228.212.209/api/number_classify.php?number=371# without parameter
Repository: GitHub