Setting Up Apache with PHP: Difference between revisions

From Alpine Linux
 
(12 intermediate revisions by 8 users not shown)
Line 1: Line 1:
== Installing Apache + PHP ==
== Installing Apache + PHP ==
Enable the repositories:
<pre><nowiki>
cat > /etc/apk/repositories << EOF
http://dl-cdn.alpinelinux.org/alpine/v$(cat /etc/alpine-release | cut -d'.' -f1,2)/main
http://dl-cdn.alpinelinux.org/alpine/v$(cat /etc/alpine-release | cut -d'.' -f1,2)/community
EOF
apk update
</nowiki></pre>
Add the main packages with the command:
Add the main packages with the command:


{{Cmd|#apk add apache2 php-apache2}}
{{Cmd|<nowiki>export phpverx=$(alpinever=$(cat /etc/alpine-release|cut -d '.' -f1);[ $alpinever -ge 9 ] && echo  7|| echo 5)</nowiki>}}
{{Cmd|apk add apache2 php$phpverx-apache2}}
 
If you get an exception like unsatisfible dependency use the above command like this:
{{Cmd|<nowiki>export phpverx=$(alpinever=$(cat /etc/alpine-release);[ ${alpinever//./} -ge 309 ] && echo  7|| echo 5)</nowiki>}}
then
{{Cmd|apk add apache2 php$phpverx-apache2}}
or just change {{Cmd| -f1 to -f2 }} in the original command.
 
The <code>php"$phpverx"-apache2</code> will be "5" or "7" depending of the alpine version installed, since alpine 3.9 there's only the php7 packages.


== Testing ==
== Testing ==


Move to the directory where your site will resides:
Move to the directory where your site will reside:


{{Cmd|#cd /var/www/localhost/htdocs}}
{{Cmd|cd /var/www/localhost/htdocs}}


And create an index.php file to test if everything is ok:
And create an index.php file to test if everything is ok:


{{Cmd|#vi index.php
{{Cmd|vi index.php}}
 
Add the following lines in the file:


<pre>
<?php
<?php
phpinfo();
phpinfo();
?>}}
?>
</pre>


That done, let us start apache2 web server:
That done, let us start apache2 web server:
Line 22: Line 47:
{{Cmd|rc-service apache2 start}}
{{Cmd|rc-service apache2 start}}


Now access: http://<ipa_ddress> and if everything is ok you will see the php info page.
Now access: http://<ip_address> and if everything is ok you will see the PHP info page.


== Ending ==
== Ending ==
Line 30: Line 55:
{{Cmd|rc-update add apache2}}
{{Cmd|rc-update add apache2}}


Now you can create your php site and host in this directory.
Now you can create your PHP site and host in this directory.
 
== Note ==
This is the easiest way to setup Apache with PHP support, but it's the most inefficient (resource wise) setup, please refer to [[Apache with php-fpm]]
 
[[Category:Web Server]]
[[Category:PHP]]

Latest revision as of 13:56, 1 March 2021

Installing Apache + PHP

Enable the repositories:

cat > /etc/apk/repositories << EOF
http://dl-cdn.alpinelinux.org/alpine/v$(cat /etc/alpine-release | cut -d'.' -f1,2)/main
http://dl-cdn.alpinelinux.org/alpine/v$(cat /etc/alpine-release | cut -d'.' -f1,2)/community
EOF

apk update

Add the main packages with the command:

export phpverx=$(alpinever=$(cat /etc/alpine-release|cut -d '.' -f1);[ $alpinever -ge 9 ] && echo 7|| echo 5)

apk add apache2 php$phpverx-apache2

If you get an exception like unsatisfible dependency use the above command like this:

export phpverx=$(alpinever=$(cat /etc/alpine-release);[ ${alpinever//./} -ge 309 ] && echo 7|| echo 5)

then

apk add apache2 php$phpverx-apache2

or just change

-f1 to -f2

in the original command.

The php"$phpverx"-apache2 will be "5" or "7" depending of the alpine version installed, since alpine 3.9 there's only the php7 packages.

Testing

Move to the directory where your site will reside:

cd /var/www/localhost/htdocs

And create an index.php file to test if everything is ok:

vi index.php

Add the following lines in the file:

<?php
phpinfo();
?>

That done, let us start apache2 web server:

rc-service apache2 start

Now access: http://<ip_address> and if everything is ok you will see the PHP info page.

Ending

Finally let us set up apache2 to start on operating system startup:

rc-update add apache2

Now you can create your PHP site and host in this directory.

Note

This is the easiest way to setup Apache with PHP support, but it's the most inefficient (resource wise) setup, please refer to Apache with php-fpm