LAMP Server on Arch Linux

Updated by Alex Fornuto

Contribute on GitHub

View Project | View File | Edit File

A LAMP (Linux, Apache, MySQL, PHP) stack is a common web stack used to prepare servers for hosting web content. This guide shows you how to install a LAMP stack an Arch Linux server.

Since Arch does not come in specific versions, this guide is up-to-date as of the December 2015 Arch update.

This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you’re not familiar with the sudo command, you can check our Users and Groups guide.

Before You Begin

  1. Ensure that you have followed the Getting Started and Securing Your Server guides, and the Linode’s hostname is set.

  2. Update your system:

    1
    sudo pacman -Syu
    

Apache

Install and Configure

  1. Install Apache 2.4:

    1
    sudo pacman -Syu apache
    
  2. Edit the httpd-mpm.conf Apache configuration file in /etc/httpd/conf/extra/ to adjust the resource use settings. The settings shown below are a good starting point for a Linode 2GB:

    Before changing any configuration files, it is advised that you make a backup of the file. To make a backup:

    1
    cp /etc/httpd/conf/extra/httpd-mpm.conf ~/httpd-mpm.conf.backup
    
    /etc/httpd/conf/extra/httpd-mpm.conf
    1
    2
    3
    4
    5
    6
    7
    <IfModule mpm_prefork_module>
            StartServers            4
            MinSpareServers         20
            MaxSpareServers         40
            MaxRequestWorkers       200
            MaxConnectionsPerChild  4500
    </IfModule>
    
  3. Edit the httpd-default.conf file to turn KeepAlive off.

    /etc/httpd/conf/extra/httpd-default.conf
    1
    KeepAlive Off
    
  4. Set Apache to start at boot:

    1
    sudo systemctl enable httpd.service
    

Add Name-Based Virtual Hosts

Virtual hosting can be configured so that multiple domains (or subdomains) can be hosted on the server. These websites can be controlled by different users, or by a single user, as you prefer. There are different ways to set up virtual hosts; however, we recommend the method below.

  1. Open httpd.conf and edit the line DocumentRoot /srv/http to define the default document root:

    /etc/httpd/conf/httpd.conf
    1
    DocumentRoot "/srv/http/default"
    
  2. Uncomment the line that reads Include conf/extra/httpd-vhosts.conf near the end of the /etc/httpd/conf/httpd.conf file:

    /etc/httpd/conf/httpd.conf
    1
    Include conf/extra/httpd-vhosts.conf
    
  3. Open httpd-vhosts.conf, under the extra folder. Edit the example virtual hosts block to resemble the ones below, replacing example.com with your domain.

    /etc/httpd/conf/extra/httpd-vhosts.conf
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <VirtualHost *:80> 
         ServerAdmin webmaster@example.com
         ServerName example.com
         ServerAlias www.example.com
         DocumentRoot /srv/http/example.com/public_html/
         ErrorLog /srv/http/example.com/logs/error.log 
         CustomLog /srv/http/example.com/logs/access.log combined
                <Directory />
                   Order deny,allow
                   Allow from all
                </Directory>
    </VirtualHost>
    

    Remove the second example in the file, or use it configure a second website.

    ErrorLog and CustomLog entries are suggested for more fine-grained logging, but are not required. If they are defined (as shown above), the logs directories must be created before you restart Apache.

  4. Create the directories referenced in the configuration above:

    1
    2
    3
    sudo mkdir -p /srv/http/default
    sudo mkdir -p /srv/http/example.com/public_html
    sudo mkdir -p /srv/http/example.com/logs
    
  5. After you’ve set up your virtual hosts, issue the following command to run Apache for the first time:

    1
    sudo systemctl start httpd.service
    

    You should now be able to access your website. If no files are uploaded you will see an Index of / page.

    Should any additional changes be made to a configuration file restart Apache:

    1
    sudo systemctl restart httpd.service
    

MySQL

Install and Configure

By default, Arch Linux provides MariaDB as a relational database solution. MariaDB is an open source drop-in replacement for MySQL, and all system commands that reference mysql are compatible with it.

  1. Install the mariadb, mariadb-clients and libmariadbclient packages:

    1
    sudo pacman -Syu mariadb mariadb-clients libmariadbclient
    
  2. Install the MariaDB data directory:

    1
    sudo mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
    
  3. Start MySQL and set it to run at boot:

    1
    2
    sudo systemctl start mysqld.service
    sudo systemctl enable mysqld.service
    
  4. Run mysql_secure_installation, a program that helps secure MySQL. mysql_secure_installation gives you the option to set your root password, disable root logins from outside localhost, remove anonymous user accounts, remove the test database and then reload the privilege tables:

    1
    mysql_secure_installation
    

Create a Database

  1. Log into MySQL:

    1
    mysql -u root -p
    

    -u <user> specifies the user, and -p will prompt you for the password.

  2. You will see the MariaDB prompt. Create a database and create and grant a user permissions on the database:

    1
    2
    CREATE DATABASE webdata;
    GRANT ALL ON webdata.* TO 'webuser' IDENTIFIED BY 'password';
    

    In this example webdata is the name of the database, webuser is the username, and password is the user’s password. Note that database usernames and passwords do not correlate to system user accounts.

  3. Quit MariaDB:

    1
    quit
    

With Apache and MySQL installed, you are now ready to move on to installing PHP to provide scripting support for your web application.

PHP

PHP makes it possible to produce dynamic and interactive pages using your own scripts and popular web development frameworks. Many popular web applications like WordPress are written in PHP. If you want to develop your websites using PHP, you must first install it.

  1. Install PHP:

    1
    sudo pacman -Syu php php-apache
    
  2. Edit /etc/php/php.ini for better error messages and logs, and upgraded performance. These modifications provide a good starting point for a Linode 2GB:

    /etc/php/php.ini
    1
    2
    3
    4
    5
    error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
    log_errors = On 
    error_log = /var/log/php/error.log
    max_input_time = 30
    extension=mysql.so
    

    Ensure that all lines noted above are uncommented. A commented line begins with a semicolon (;).

  3. Create the log directory for PHP and give the Apache user ownership:

    1
    2
    sudo mkdir /var/log/php
    sudo chown http /var/log/php
    
  4. Enable the PHP module in the /etc/httpd/conf/httpd.conf file by adding the following lines in the appropriate sections:

    /etc/httpd/conf/httpd.conf
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # Dynamic Shared Object (DSO) Support
    LoadModule php5_module modules/libphp5.so
            
    # Supplemental configuration
    # PHP 5
    Include conf/extra/php5_module.conf
    
    # Located in the <IfModule mime_module>
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
    
  5. In the same file, comment out the line LoadModule mpm_event_module modules/mod_mpm_event.so by adding a # in front, and add the line LoadModule mpm_prefork_module modules/mod_mpm_prefork.so:

    /etc/httpd/conf/httpd.conf
    1
    2
    #LoadModule mpm_event_module modules/mod_mpm_event.so
    LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
    
  6. Restart the Apache:

    1
    sudo systemctl restart httpd.service
    

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This guide is published under a CC BY-ND 4.0 license.