Skip to content

Instantly share code, notes, and snippets.

@geekwolverine
Forked from superjojo140/apache_and_php.md
Created October 17, 2023 23:17
Show Gist options
  • Save geekwolverine/a4290707aba1a2e69dafe29d3d8069e7 to your computer and use it in GitHub Desktop.
Save geekwolverine/a4290707aba1a2e69dafe29d3d8069e7 to your computer and use it in GitHub Desktop.
Apache, PHP, MariaDB and PhpMyAdmin on Archlinux

Guide to install Apache, PHP, MariaDB and PhpMyAdmin on your archlinux system and serve php-based database applications

Install Packages

pacman -S apache php php-apache mariadb phpmyadmin

Config PHP

The main PHP configuration file is well-documented and located at /etc/php/php.ini

Error reporting

If you want to display errors in browser set: display_errors = On
You can also enable error reporting in .htaccess file: php_flag display_errors On

Sql Module

If you want to use mysqli operations to connect to a mysql database uncomment theese lines in php.ini

extension=mysqli
extension=pdo_mysql

Sessions

If you want to use sessions go to the [Session] section in php.ini
Uncomment the line:

session.save_path = "/tmp"

Sendmail

Configure how PHP sends mails when usign the mail() function at the [mail function] section in php.ini
For testing you can simulate sending mails to a logfile:

sendmail_path = 'cat >> /your/path/sendmail.log'
mail.log = "/your/path/sendmail.log"

Make sure Apache has permissoins to write to that file

Config Apache

The main Apache configuration file is located at /etc/httpd/conf/httpd.conf The default Doument Root is /srv/http

On linux lammp installation the httpd.conf is located at /opt/lampp/etc/httpd.conf
The default Document root is: /opt/lampp/htdocs

Make sure to restart apache after a configuration to see it's effects.

Config file for your webapp

To tell apache where your app is located and with which alias it can be accessed create a new config file: /etc/httpd/conf/extra/<YOUR_APP>.conf

Alias /<YOUR_ALIAS> "path/to/application"
<Directory "path/to/application">
    DirectoryIndex index.html
    AllowOverride All
    Options FollowSymlinks
    Require all granted
</Directory>

Now we have to tell apache to use this new config file. Add the following line to your /etc/httpd/conf/httpd.conf

Include conf/extra/<YOUR_APP>.conf

Permissions

This was the main part, but mostly the apache user does not have access rights to the specified folder in your filesystem. The result is a 403 Error.

To fix this, you can give every user full access to your project folder (Attention: make sure if that makes sense in your use case):

chmod -R 777 /path/to/project/

Make sure, that apache has also has access to the folder that contains your project (Read access is enough here) E.g. if your project is located under /home/username/projectuse:

chmod 711 /home/username/

Let Apache use PHP

By default Apache would just serve your php files as plain text files. To make apache execute your php scripts follow theese steps:

mod_mpm

Make sure that the mpm_event module is disabled and the mpm_prefork module is enabled

In your httpd.conf

#LoadModule mpm_event_module modules/mod_mpm_event.so
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so

Enable PHP module

Add to your httpd.conf:

LoadModule php_module modules/libphp.so
AddHandler php-script .php
Include	conf/extra/php_module.conf

Start apache server

systemctl start   httpd.service
systemctl stop    httpd.service
systemctl restart httpd.service
systemctl enable  httpd.service  #autostart service on boot

MariaDB

Run mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql

Start the mariadb service: systemctl enable --now mariadb

Create user and Database

Connect to mysql server from your root systemuser via socket (no password required)

mysql --protocol=socket #run this command as root (e.g. prefixed with sudo)

Create a new user

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

Create new database and grant privilleges

CREATE DATABASE `db_name`;
GRANT ALL PRIVILEGES ON `db_name` . * TO 'username'@'localhost';

PhpMyAdmin

Create the Apache configuration file:

/etc/httpd/conf/extra/phpmyadmin.conf

Alias /phpmyadmin "/usr/share/webapps/phpMyAdmin"
<Directory "/usr/share/webapps/phpMyAdmin">
    DirectoryIndex index.php
    AllowOverride All
    Options FollowSymlinks
    Require all granted
</Directory>

And include it in /etc/httpd/conf/httpd.conf:

# phpMyAdmin configuration
Include conf/extra/phpmyadmin.conf

Use your application

Make sure to restart the apache daemon after your configurations: systemctl restart httpd Open your browser and go to: localhost/your-app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment