Email This List Email This List Print This List Print This List

Installing Apache, PHP and MySQL

Now that you’ve installed Linux and secured your Serv­er, it’s time to start doing stuff with it. In this guide, you’ll learn how to host a web­site. Start by installing a web serv­er, data­base, and PHP — a pop­u­lar com­bin­a­tion which is com­monly referred to a LAMP stack (Linux, Apache, MySQL, and PHP). Then cre­ate or import a data­base, upload files, and add DNS records. By the time you reach the end of this guide, your Serv­er will be host­ing one or more web­sites!

Debi­an 7 and Ubuntu 14.04 LTS are the Linux dis­tri­bu­tions we’re using as the start­ing point for the pack­ages and con­fig­ur­a­tions men­tioned in this guide.

This guide is designed for small and medi­um-size web­sites run­ning on Word­Press, Drupal, or anoth­er PHP con­tent man­age­ment sys­tem. If your web­site doesn’t belong in that cat­egory, you’ll need to assess your require­ments and install cus­tom pack­ages tailored for your par­tic­u­lar require­ments.

This guide is writ­ten for a non-root user. Com­mands that require elev­ated priv­ileges are pre­fixed with sudo.

Web Serv­er

Host­ing a web­site starts with installing a web serv­er, an applic­a­tion on your Serv­er that deliv­ers con­tent through the Inter­net. This sec­tion will help you get star­ted with Apache, the world’s most pop­u­lar web serv­er. For more inform­a­tion about Apache and oth­er web serv­ers, see our web serv­er ref­er­ence manu­als.

Installing Apache

Install Apache on your Serv­er by enter­ing the fol­low­ing com­mand:

1 sudo apt-get install apache2

Your Serv­er will down­load, install, and start the Apache web serv­er.

Optim­iz­ing Apache for a Serv­er 1GB

Installing Apache is easy, but if you leave it run­ning with the default set­tings, your serv­er could run out of memory. That’s why it’s import­ant to optim­ize Apache before you start host­ing a web­site on your Serv­er. Here’s how to optim­ize the Apache web serv­er for a Serv­er 1GB:

These guidelines are designed to optim­ize Apache for a Serv­er 1GB, but you can use this inform­a­tion for any size Serv­er. The val­ues are based on the amount of memory avail­able, so if you have a Serv­er 2GB, mul­tiply all of the val­ues by 2 and use those num­bers for your set­tings.

  1. Just to be safe, make a copy of Apache’s con­fig­ur­a­tion file by enter­ing the fol­low­ing com­mand. You can restore the duplic­ate (apache2.backup.conf) if any­thing hap­pens to the con­fig­ur­a­tion file.
1 sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.backup.conf
  1. Open Apache’s con­fig­ur­a­tion file for edit­ing by enter­ing the fol­low­ing com­mand:
1 sudo nano /etc/apache2/apache2.conf
  1. Make sure that the fol­low­ing val­ues are set.

In Ubuntu 14.04 LTS, you will need to append the mod­ule sec­tion noted below to the end of your apache2.conf file:

/etc/apache2/apache2.conf

1

2

3

4

5

6

7

8

9

10

KeepAlive Off

 

<IfMod­ule mpm_​prefork_​module>

StartServ­ers 2

Min­Spare­Serv­ers 6

Max­Spare­Serv­ers 12

Max­Cli­ents 30

MaxRe­quest­s­Per­Child 3000

</​IfModule>

  1. Save the changes to Apache’s con­fig­ur­a­tion file by press­ing Con­trol + x and then press­ingy. Press Enter to con­firm.
  2. Restart Apache to incor­por­ate the new set­tings. Enter the fol­low­ing com­mand:
1 sudo ser­vice apache2 restart

Good work! You’ve suc­cess­fully optim­ized Apache for your Serv­er, increas­ing per­form­ance and imple­ment­ing safe­guards to pre­vent excess­ive resource con­sump­tion. You’re almost ready to host web­sites with Apache.

Con­fig­ur­ing Name-based Vir­tu­al Hosts

Now that Apache is optim­ized for per­form­ance, it’s time to start­ing host­ing one or more web­sites. There are sev­er­al pos­sible meth­ods of doing this. In this sec­tion, you’ll use name-based vir­tu­al hosts to host web­sites in your home dir­ect­ory. Here’s how:

You should not be logged in as root while execut­ing these com­mands. To learn how to cre­ate a new user account and log in as that user, see Adding a New User.

  1. Dis­able the default Apache vir­tu­al host by enter­ing the fol­low­ing com­mand:
1 sudo a2dissite *default
  1. Nav­ig­ate to your /​var/​www dir­ect­ory:
1 cd /​var/​www
  1. Cre­ate a folder to hold your web­site by enter­ing the fol­low­ing com­mand, repla­cing ‘example​.com’ with your domain name:
1 sudo mkdir example​.com
  1. Cre­ate a set of folders inside the folder you’ve just cre­ated to store your website’s files, logs, and backups. Enter the fol­low­ing com­mand, repla­cing example​.com with your domain name:
1

2

3

sudo mkdir ‑p example​.com/​p​u​b​l​i​c​_​h​tml

sudo mkdir ‑p example​.com/​log

sudo mkdir ‑p example​.com/​b​a​c​k​ups

  1. Cre­ate the vir­tu­al host file for your web­site by enter­ing the fol­low­ing com­mand. Replace theexample​.com in example.com.conf with your domain name:
1 sudo nano /etc/apache2/sites-available/example.com.conf
  1. The file name must end with .conf in Apache ver­sions 2.4 and later, which Ubuntu 14.04 uses. The .con­fex­ten­sion is back­wards-com­pat­ible with earli­er ver­sions.
  2. Now it’s time to cre­ate a con­fig­ur­a­tion for your vir­tu­al host. We’ve cre­ated some basic set­tings to get your star­ted. Copy and paste the set­tings shown below in to the vir­tu­al host file you just cre­ated. Replace example​.com with your domain name.

/etc/apache2/sites-available/example.com.conf

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

# domain: example​.com

# pub­lic: /var/www/example.com/public_html/

 

<Vir­tu­al­Host *:80>

# Admin email, Serv­er Name (domain name), and any ali­ases

ServerAd­min webmaster@​example.​com

Server­Name  www​.example​.com

Server­Ali­as example​.com

 

# Index file and Doc­u­ment Root (where the pub­lic files are loc­ated)

Dir­ect­ory­In­dex index.html index.php

Doc­u­ment­Root /var/www/example.com/public_html

# Log file loc­a­tions

Log­Level warn

Error­Log  /var/www/example.com/log/error.log

Cus­tom­Log /var/www/example.com/log/access.log com­bined

</​VirtualHost>

  1. Save the changes to the vir­tu­al host con­fig­ur­a­tion file by press­ing Con­trol + x and then press­ing y. Press Enter to con­firm.
  2. Enable your new web­site by enter­ing the fol­low­ing com­mand. Replace example​.com with your domain name:
1  sudo a2ensite example.com.conf
  1. This cre­ates a sym­bol­ic link to your example.com.conf file in the appro­pri­ate dir­ect­ory for act­ive vir­tu­al hosts.
  2. The pre­vi­ous com­mand will alert you that you need to restart Apache to save the changes. Enter the fol­low­ing com­mand to apply your new con­fig­ur­a­tion:
1 sudo ser­vice apache2 restart
  1. Repeat steps 1–11 for every oth­er web­site you want to host on your Serv­er.

Con­grat­u­la­tions! You’ve con­figured Apache to host one or more web­sites on your Serv­er. After you upload files and add DNS records later in this guide, your web­sites will be access­ible to the out­side world.

Data­base

Data­bases store data in a struc­tured and eas­ily access­ible man­ner, serving as the found­a­tion for hun­dreds of web and serv­er applic­a­tions. A vari­ety of open source data­base plat­forms exist to meet the needs of applic­a­tions run­ning on your Linux VPS. This sec­tion will help you get star­ted with MySQL, one of the most pop­u­lar data­base plat­forms. For more inform­a­tion about MySQL and oth­er data­bases, see our data­base ref­er­ence manu­als.

Installing MySQL

Here’s how to install and con­fig­ure MySQL:

  1. Install MySQL by enter­ing the fol­low­ing com­mand. Your Serv­er will down­load, install, and start the MySQL data­base serv­er.
1 sudo apt-get install mysql-serv­er
  1. You will be promp­ted to enter a pass­word for the MySQL root user. Enter a pass­word.
  2. Secure MySQL by enter­ing the fol­low­ing com­mand to open mysql_​secure_​installation util­ity:
1 sudo mysql_​secure_​installation
  1. The mysql_​secure_​installation util­ity appears. Fol­low the instruc­tions to remove anonym­ous user accounts, dis­able remote root login, and remove the test data­base.

That’s it! MySQL is now installed and run­ning on your Serv­er.

Optim­iz­ing MySQL for a Serv­er 1GB

MySQL con­sumes a lot of memory when using the default con­fig­ur­a­tion. To set resource con­straints, you’ll need to edit the MySQL con­fig­ur­a­tion file. Here’s how to optim­ize MySQL for a Serv­er 1GB:

These guidelines are designed to optim­ize MySQL for a Serv­er 1GB, but you can use this inform­a­tion for any size Serv­er. If you have a lar­ger Serv­er, start with these val­ues and modi­fy them while care­fully watch­ing for memory and per­form­ance issues.

  1. Open the MySQL con­fig­ur­a­tion file for edit­ing by enter­ing the fol­low­ing com­mand:
1 sudo nano /etc/mysql/my.cnf
  1. Make sure that the fol­low­ing val­ues are set:

/etc/mysql/my.cnf

1

2

3

4

5

max_​connections = 75

key_​buffer = 32M

max_​allowed_​packet = 1M

thread_​stack = 128K

table_​cache = 32

  1. Save the changes to MySQL’s con­fig­ur­a­tion file by press­ing Con­trol + x and then press­ing y.
  2. Restart MySQL to save the changes. Enter the fol­low­ing com­mand:
1 sudo ser­vice mysql restart

Now that you’ve edited the MySQL con­fig­ur­a­tion file, you’re ready to start cre­at­ing and import­ing data­bases.

Cre­at­ing a Data­base

The first thing you’ll need to do in MySQL is cre­ate a data­base. (If you already have a data­base that you’d like to import, skip to Import­ing a Data­base.) Here’s how to cre­ate a data­base in MySQL:

  1. Log in to MySQL by enter­ing the fol­low­ing com­mand and then enter­ing the MySQL root pass­word:
1 mysql ‑u root ‑p
  1. Cre­ate a data­base by enter­ing the fol­low­ing com­mand. Replace exampleDB with your own data­base name:
1 cre­ate data­base exampleDB;
  1. Cre­ate a new user in MySQL and then grant that user per­mis­sion to access the new data­base by issu­ing the fol­low­ing com­mand. Replace example_​user with your user­name, and 5t1ck with your pass­word:
1 grant all on exampleDB.* to ‘example_​user’ iden­ti­fied by ‘5t1ck’;
  1. MySQL user­names and pass­words are only used by scripts con­nect­ing to the data­base. They do not need to rep­res­ent actu­al user accounts on the sys­tem.
  2. Tell MySQL to reload the grant tables by issu­ing the fol­low­ing com­mand:
1 flush priv­ileges;
  1. Now that you’ve cre­ated the data­base and gran­ted a user per­mis­sions to the data­base, you can exit MySQL by enter­ing the fol­low­ing com­mand:
1 quit

Now you have a new data­base that you can use for your web­site. If you don’t need to import a data­base, go ahead and skip to PHP.

Import­ing a Data­base

If you have an exist­ing web­site, you may want to import an exist­ing data­base in to MySQL. It’s easy, and it allows you to have an estab­lished web­site up and run­ning on your Serv­er in a mat­ter of minutes. Here’s how to import a data­base in to MySQL:

  1. Upload the data­base file to your Serv­er. See the instruc­tions in Upload­ing Files.
  2. Import the data­base by enter­ing the fol­low­ing com­mand. Replace user­name with your MySQL user­name and database_​name with the data­base name you want to import to. You will be promp­ted for your MySQL pass­word:
1 mysql ‑u user­name ‑p database_​name < FILE.sql

Your data­base will be impor­ted in to MySQL.

PHP

PHP is a gen­er­al-pur­pose script­ing lan­guage that allows you to pro­duce dynam­ic and inter­act­ive webpages. Many pop­u­lar web applic­a­tions and con­tent man­age­ment sys­tems, like Word­Press and Drupal, are writ­ten in PHP. To devel­op or host web­sites using PHP, you must first install the base pack­age and a couple of mod­ules.

Installing PHP

Here’s how to install PHP with MySQL sup­port:

  1. Install the base PHP pack­age by enter­ing the fol­low­ing com­mand:
1 sudo apt-get install php5 php-pear
  1. Add MySQL sup­port by enter­ing the fol­low­ing com­mand:
1 sudo apt-get install php5-mysql

Optim­iz­ing PHP for a Serv­er 1GB

After you install PHP, you’ll need to enable log­ging and tune PHP for bet­ter per­form­ance. The set­ting you’ll want to pay the most atten­tion to is memory_​limit, which con­trols how much memory is alloc­ated to PHP. Here’s how to enable log­ging and optim­ize PHP for per­form­ance:

These guidelines are designed to optim­ize PHP for a Serv­er 1GB, but you can use this inform­a­tion as a start­ing point for any size Serv­er. If you have a lar­ger Serv­er, you could increase the memory lim­it to a lar­ger value, like 256M.

  1. Open the PHP con­fig­ur­a­tion files by enter­ing the fol­low­ing com­mand:
1 sudo nano /etc/php5/apache2/php.ini
  1. Veri­fy that the fol­low­ing val­ues are set. All of the lines lis­ted below should be uncom­men­ted. Be sure to remove any semi-colons (;) at the begin­ning of the lines.

/etc/php5/apache2/php.ini

1

2

3

4

5

6

7

max_​execution_​time = 30

memory_​limit = 128M

error_​reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR

display_​errors = Off

log_​errors = On

error_​log = /var/log/php/error.log

register_​globals = Off

The 128M set­ting for memory_​limit is a gen­er­al guideline. While this value should be suf­fi­cient for most web­sites, lar­ger web­sites and some web applic­a­tions may require 256 mega­bytes or more.

  1. Save the changes by press­ing Con­trol + x and then press­ing y.
  2. Cre­ate the /​var/​log/​php/​ dir­ect­ory for the PHP error log with the fol­low­ing com­mand:
1 sudo mkdir ‑p /​var/​log/​php
  1. Change the own­er of the /​var/​log/​php/​ dir­ect­ory to www-data, which the PHP user runs as:
1 sudo chown www-data /​var/​log/​php
  1. Restart Apache to load the PHP mod­ule by enter­ing the fol­low­ing com­mand:
1 sudo ser­vice apache2 restart

Con­grat­u­la­tions! PHP is now installed on your Serv­er and con­figured for optim­al per­form­ance.

Upload­ing Files

You’ve suc­cess­fully installed Apache, MySQL, and PHP. Now it’s time to upload a web­site to your Serv­er. This is one of the last steps before you “flip the switch” and pub­lish your web­site on the Inter­net. Here’s how to upload files to your Serv­er:

  1. If you haven’t done so already, down­load and install an SFTP cap­able cli­ent on your com­puter. We recom­mend using the FileZ­illa SFTP cli­ent.
  2. Fol­low the instruc­tions in the guides lis­ted above to con­nect to your Serv­er.
  3. Upload your website’s files to the /var/www/example.com/public_html dir­ect­ory. Replaceexample​.com with your domain name.

If you con­figured name-based vir­tu­al hosts, don’t for­get to upload the files for the oth­er web­sites to their respect­ive dir­ect­or­ies.

If you’re using a con­tent man­age­ment sys­tem like Word­Press or Drupal, you may need to con­fig­ure the appro­pri­ate set­tings file to point the con­tent man­age­ment sys­tem at the MySQL data­base.

 

Related Post

admin has written 133 articles