Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

Setting up a LAMP (Linux, Apache, MySql, PHP) stack on your server will allow for the creation and hosting of websites and web applications. This guide shows you how to install a LAMP stack on Debian 8 (Jessie).

Note
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

Prior to installing your LAMP stack ensure that:

  • You have followed the Getting Started and Securing Your Server guides.

  • You have a hostname and fully-qualified domain name (FQDN) configured on your Linode. To ensure this is set run:

    hostname
    hostname -f
    

    The first command should output your hostname, with the second providing your FQDN.

  • Your Linode’s repositories and packages are up-to-date:

    sudo apt-get update && sudo apt-get upgrade
    

Apache

Install and Configure Apache

  1. Install Apache 2.4:

    sudo apt-get install apache2
    
  2. Open /etc/apache2/mods-available/mpm_prefork.conf in your text editor and edit the values as needed. The following is optimized for a 2GB Linode:

    File: /etc/apache2/mods-available/mpm_prefork.conf
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    
    # prefork MPM
    # StartServers: number of server processes to start
    # MinSpareServers: minimum number of server processes which are kept spare
    # MaxSpareServers: maximum number of server processes which are kept spare
    # MaxRequestWorkers: maximum number of server processes allowed to start
    # MaxConnectionsPerChild: maximum number of requests a server process serves
    
    <IfModule mpm_prefork_module>
            StartServers              4
            MinSpareServers           20
            MaxSpareServers           40
            MaxRequestWorkers         200
            MaxConnectionsPerChild    4500
    </IfModule>
    
    # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
    Note
    These settings are good starting points, but they should be adjusted to best suit your deployment’s needs.
  3. On Debian 8, the event module is enabled by default. This should be disabled, and the prefork module enabled:

    sudo a2dismod mpm_event
    sudo a2enmod mpm_prefork
    
  4. Restart Apache:

    sudo systemctl restart apache2
    

Configure Name-Based Virtual Hosts

There can be as many virtual hosts files as needed to support the amount of domains hosted on the Linode.

  1. Create directories for your websites and websites’ logs, replacing example.com with your own domain name:

    sudo mkdir -p /var/www/html/example.com/public_html
    sudo mkdir /var/www/html/example.com/logs
    

    Repeat the process if you intend on hosting multiple websites on your Linode.

  2. Create an example.com.conf file in /etc/apache2/sites-available with your text editor, replacing instances of example.com with your own domain URL in both the configuration file and in the file name:

    File: /etc/apache2/sites-available/example.com.conf
    1
    2
    3
    4
    5
    6
    7
    8
    
    <VirtualHost *:80>
         ServerAdmin webmaster@example.com
         ServerName example.com
         ServerAlias www.example.com
         DocumentRoot /var/www/html/example.com/public_html/
         ErrorLog /var/www/html/example.com/logs/error.log
         CustomLog /var/www/html/example.com/logs/access.log combined
    </VirtualHost>

    Repeat this process for any other domains you host:

    File: /etc/apache2/sites-available/example.org.conf
    1
    2
    3
    4
    5
    6
    7
    8
    
    <VirtualHost *:80>
         ServerAdmin webmaster@example.org
         ServerName example.org
         ServerAlias www.example.org
         DocumentRoot /var/www/html/example.org/public_html/
         ErrorLog /var/www/html/example.org/logs/error.log
         CustomLog /var/www/html/example.org/logs/access.log combined
    </VirtualHost>
  3. Symbolically link your virtual hosts files from the sites-available directory to the sites-enabled directory. Replace the filename with your own:

    sudo a2ensite example.com.conf
    sudo a2ensite example.org.conf
    
    Note
    Should you need to disable a site, you can use a2dissite example.com.
  4. Restart Apache:

    sudo systemctl restart apache2
    

MySQL

MySQL is a relational database management system (RDBMS) and is a popular component of many applications.

Install MySQL

  1. Install MySQL:

    sudo apt-get install mysql-server
    

    Input a secure password when prompted by the installation.

  2. Run mysql_secure_installation to remove the test database and any extraneous user permissions added during the initial installation process:

    sudo mysql_secure_installation
    

    It is recommended that you select yes (y) for all questions. If you already have a secure root password, you do not need to change it.

Set Up a MySQL Database

Next, you can create a database and grant your users permissions to use databases.

  1. Log in to MySQL:

    mysql -u root -p
    

    Enter MySQL’s root password when prompted.

  2. Create a database and grant your users permissions on it. Change the database name (webdata) and username (username). Change the password (password):

    create database webdata;
    grant all on webdata.* to 'username' identified by 'password';
    
  3. Exit MySQL:

    quit
    

PHP

PHP makes it possible to produce dynamic and interactive pages using your own scripts and popular web development frameworks.

Install PHP

PHP 7.3 is the latest version available and has the longest period of support offered as of this guide’s publishing:

  1. Add the sury.org repository, which packages PHP 7.3 for Debian 8:

    sudo apt install lsb-release apt-transport-https ca-certificates
    sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
    echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php7.3.list
    sudo apt update
    
  2. Install PHP 7.3 and the PHP Extension and Application Repository:

    sudo apt-get install php7.3 php-pear
    

Configure PHP

  1. Open /etc/php/7.3/apache2/php.ini in your text editor and edit the following values. These settings are optimized for the 2GB Linode:

    File: /etc/php/7.3/apache2/php.ini
    1
    2
    3
    
    error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
    error_log = /var/log/php/error.log
    max_input_time = 30
    Note
    Ensure that all values are uncommented, by making sure they do not start with a semicolon (;).
  2. Create the log directory for PHP and give the Apache user ownership:

    sudo mkdir /var/log/php
    sudo chown www-data /var/log/php
    
  3. If you need support for MySQL in PHP, then you must install the php7.3-mysql package:

    sudo apt-get install php7.3-mysql
    
  4. Restart Apache:

    sudo systemctl restart apache2
    

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 page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.
The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please update your Cookie Preferences on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser.