Saturday 6 March 2021

Php: apache: hello world application

Step 1: Remove built-in apache server if any.

sudo apachectl stop

sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

 

Step 2: Install Apache server on your mac system by executing below command.

brew install httpd

$brew install httpd
==> Downloading https://homebrew.bintray.com/bottles/httpd-2.4.46_2.catalina.bottle.tar.gz
Already downloaded: /Users/krishna/Library/Caches/Homebrew/downloads/2b61f61ac696b8d453066d99a2efa1ae71c4e08041ff8f692f23c9be28551c79--httpd-2.4.46_2.catalina.bottle.tar.gz
==> Pouring httpd-2.4.46_2.catalina.bottle.tar.gz
==> Caveats
DocumentRoot is /usr/local/var/www.

The default ports have been set in /usr/local/etc/httpd/httpd.conf to 8080 and in
/usr/local/etc/httpd/extra/httpd-ssl.conf to 8443 so that httpd can run without sudo.

To have launchd start httpd now and restart at login:
  brew services start httpd
Or, if you don't want/need a background service you can just run:
  apachectl start
==> Summary
🍺  /usr/local/Cellar/httpd/2.4.46_2: 1,657 files, 31.4MB

 

From the command output, I can see that DocumentRoot is /usr/local/var/www.

 

Step 3: Start httpd server by starting apache server.

 

To have launchd start httpd now and restart at login:

  brew services start httpd

Or, if you don't want/need a background service you can just run:

  apachectl start

 

Open the url 'http://localhost:8080/' to check whether apache server started or not.

 



Now let’s render a php file in browser.

 

Update httpd.conf file

 

Step 1: Update /usr/local/etc/httpd/httpd.conf with below statement.

 

LoadModule php_module /usr/local/opt/php@8.0/lib/httpd/modules/libphp.so

 

Depends on php version that  you are using, you should use either of following statements.

#LoadModule php5_module /usr/local/opt/php@5.6/lib/httpd/modules/libphp5.so
#LoadModule php7_module /usr/local/opt/php@7.0/lib/httpd/modules/libphp7.so
#LoadModule php7_module /usr/local/opt/php@7.1/lib/httpd/modules/libphp7.so
#LoadModule php7_module /usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so
#LoadModule php7_module /usr/local/opt/php@7.3/lib/httpd/modules/libphp7.so
#LoadModule php7_module /usr/local/opt/php@7.4/lib/httpd/modules/libphp7.so
#LoadModule php_module /usr/local/opt/php@8.0/lib/httpd/modules/libphp.so

 

Step 2: Tell Apache to parse certain extensions as PHP. Add following lines to httpd.conf file.

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

 

Let’s create hello world php application and place it in /usr/local/var/www folder

 

hello.php

<?php 
	echo "hello world!"
?>

 

Step 2: Restart apache server by executing below command.

apachectl start

 

Open the url ‘http://localhost:8080/hello.php’ in browser, you will see below kind of screen.

 


 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment