Updated Tuesday, March 13th, 2012 by Linode
Now that you’ve installed Linux and secured your Linode, it’s time to start doing stuff with it. In this guide, you’ll learn how to host a website. Start by installing a web server, database, and PHP - a popular combination which is commonly referred to a LAMP stack (Linux, Apache, MySQL, and PHP). Then create or import a database, upload files, and add DNS records. By the time you reach the end of this guide, your Linode will be hosting one or more websites!
Ubuntu 12.04 LTS is the Linux distribution we’re using as the starting point for the packages and configurations mentioned in this guide.
This guide is designed for small and medium-size websites running on WordPress, Drupal, or another PHP content management system. If your website doesn’t belong in that category, you’ll need to assess your requirements and install custom packages tailored for your particular requirements.
Web Server
Hosting a website starts with installing a web server, an application on your Linode that delivers content through the Internet. This section will help you get started with Apache, the world’s most popular web server. For more information about Apache and other web servers, see our web server reference manuals.
Installing Apache
Install Apache on your Linode by entering the following command:
1
sudo apt-get install apache2
Your Linode will download, install, and start the Apache web server.
Optimizing Apache for a Linode 1GB
Installing Apache is easy, but if you leave it running with the default settings, your server could run out of memory. That’s why it’s important to optimize Apache before you start hosting a website on your Linode. Here’s how to optimize the Apache web server for a Linode 1GB:
These guidelines are designed to optimize Apache for a Linode 1GB, but you can use this information for any size Linode. The values are based on the amount of memory available, so if you have a Linode 2GB, multiply all of the values by 2 and use those numbers for your settings.
Just to be safe, make a copy of Apache’s configuration file by entering the following command. You can restore the duplicate (apache2.backup.conf) if anything happens to the configuration file.
1
sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.backup.conf
Open Apache’s configuration file for editing by entering the following command:
1
sudo nano /etc/apache2/apache2.conf
Make sure that the following values are set:
1
2
3
4
5
6
7
8
9
10
11
KeepAlive Off
---
StartServers 2
MinSpareServers 6
MaxSpareServers 12
MaxClients 80
MaxRequestsPerChild 3000
Save the changes to Apache’s configuration file by pressing Control + x and then pressing y.
Restart Apache to incorporate the new settings. Enter the following command:
1
sudo service apache2 restart
Good work! You’ve successfully optimized Apache for your Linode, increasing performance and implementing safeguards to prevent excessive resource consumption. You’re almost ready to host websites with Apache.
Configuring Name-based Virtual Hosts
Now that Apache is optimized for performance, it’s time to starting hosting one or more websites. There are several possible methods of doing this. In this section, you’ll use name-based virtual hosts to host websites in your home directory. Here’s how:
You should not be logged in as root while executing these commands. To learn how to create a new user account and log in as that user, see Adding a New User.
Disable the default Apache virtual host by entering the following command:
1
sudo a2dissite default
Navigate to your home directory by entering the following command:
1
cd ~
Create a folder to hold your website by entering the following command:
1
mkdir public
Create a set of folders inside public to store your website’s files, logs, and backups. Enter the following command, replacing example.com with your domain name:
1
mkdir -p public/example.com/{public,log,backup}
Set your home directory to be readable and accessible to all users on the system by entering the following command:
1
sudo chmod a+rx ~
Set the public directory (and all of the files in it) to be readable and accessible to all users on the system by entering the following command:
1
sudo chmod -R a+rx ~/public
Create the virtual host file for your website by entering the following command. Replace example.com.conf with your domain name:
1
sudo nano /etc/apache2/sites-available/example.com.conf
The file name must end with .conf in Apache versions 2.4 and later. It won’t hurt to use .conf in earlier versions.
Now it’s time to create a configuration for your virtual host. We’ve created some basic settings to get your started. Copy and paste the settings shown below in to the virtual host file you just created. Replace example_user with your username, and example.com with your domain name.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# domain: example.com
# public: /home/example_user/public/example.com/
# Admin email, Server Name (domain name), and any aliases
ServerAdmin webmaster@example.com
ServerName www.example.com
ServerAlias example.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /home/example_user/public/example.com/public
# Log file locations
LogLevel warn
ErrorLog /home/example_user/public/example.com/log/error.log
CustomLog /home/example_user/public/example.com/log/access.log combined
Save the changes to the virtual host configuration file by pressing Control + x and then pressing y. Press Enter to confirm.
Create a symbolic link to your new public directory by entering the following command. Replace example.com with your domain name:
1
sudo a2ensite example.com.conf
Restart Apache to save the changes. Enter the following command:
1
sudo service apache2 restart
Repeat steps 1-11 for every other website you want to host on your Linode.
Congratulations! You’ve configured Apache to host one or more websites on your Linode. After you upload files and add DNS records later in this guide, your websites will be accessible to the outside world.
Database
Databases store data in a structured and easily accessible manner, serving as the foundation for hundreds of web and server applications. A variety of open source database platforms exist to meet the needs of applications running on your Linux VPS. This section will help you get started with MySQL, one of the most popular database platforms. For more information about MySQL and other databases, see our database reference manuals.
Installing MySQL
Here’s how to install and configure MySQL:
Install MySQL by entering the following command. Your Linode will download, install, and start the MySQL database server.
1
sudo apt-get install mysql-server
You will be prompted to enter a password for the MySQL root user. Enter a password.
Secure MySQL by entering the following command to open mysql_secure_installation utility:
1
sudo mysql_secure_installation
The mysql_secure_installation utility appears. Follow the instructions to remove anonymous user accounts, disable remote root login, and remove the test database.
That’s it! MySQL is now installed and running on your Linode.
Optimizing MySQL for a Linode 1GB
MySQL consumes a lot of memory when using the default configuration. To set resource constraints, you’ll need to edit the MySQL configuration file. Here’s how to optimize MySQL for a Linode 1GB:
These guidelines are designed to optimize MySQL for a Linode 1GB, but you can use this information for any size Linode. If you have a larger Linode, start with these values and modify them while carefully watching for memory and performance issues.
Open the MySQL configuration file for editing by entering the following command:
1
sudo nano /etc/mysql/my.cnf
Make sure that the following values are set:
1
2
3
4
5
max_connections = 75
key_buffer = 32M
max_allowed_packet = 1M
thread_stack = 128K
table_cache = 32
Save the changes to MySQL’s configuration file by pressing Control + x and then pressing y.
Restart MySQL to save the changes. Enter the following command:
1
sudo service mysql restart
Now that you’ve edited the MySQL configuration file, you’re ready to start creating and importing databases.
Creating a Database
The first thing you’ll need to do in MySQL is create a database. (If you already have a database that you’d like to import, skip to Importing a Database.) Here’s how to create a database in MySQL:
Log in to MySQL by entering the following command and then entering the MySQL root password:
1
mysql -u root -p
Create a database and grant a user permission to it by entering the following command. Replace exampleDB with your own database name:
1
create database exampleDB;
Create a new user in MySQL and then grant that user permission to access the new database by issuing the following command. Replace example_user with your username, and 5t1ck with your password:
1
grant all on exampleDB.* to 'example_user' identified by '5t1ck';
MySQL usernames and passwords are only used by scripts connecting to the database. They do not need to represent actual user accounts on the system.
Tell MySQL to reload the grant tables by issuing the following command:
1
flush privileges;
Now that you’ve created the database and granted a user permissions to the database, you can exit MySQL by entering the following command:
1
quit
Now you have a new database that you can use for your website. If you don’t need to import a database, go ahead and skip to PHP.
Importing a Database
If you have an existing website, you may want to import an existing database in to MySQL. It’s easy, and it allows you to have an established website up and running on your Linode in a matter of minutes. Here’s how to import a database in to MySQL:
Upload the database file to your Linode. See the instructions in Uploading Files.
Import the database by entering the following command. Replace username with your MySQL username, password with your MySQL password, and database_name with your own:
1
mysql -u username -ppassword database_name < FILE.sql
Your database will be imported in to MySQL.
PHP
PHP is a general-purpose scripting language that allows you to produce dynamic and interactive webpages. Many popular web applications and content management systems, like WordPress and Drupal, are written in PHP. To develop or host websites using PHP, you must first install the base package and a couple of modules.
Installing PHP
Here’s how to install PHP with MySQL support and the Suhosin security module:
Install the base PHP package by entering the following command:
1
sudo apt-get install php5 php-pear
Add MySQL support by entering the following command:
1
sudo apt-get install php5-mysql
Secure PHP with Suhosin by entering the following command:
1
sudo apt-get install php5-suhosin
Optimizing PHP for a Linode 1GB
After you install PHP, you’ll need to enable logging and tune PHP for better performance. The setting you’ll want to pay the most attention to is memory_limit, which controls how much memory is allocated to PHP. Here’s how to enable logging and optimize PHP for performance:
These guidelines are designed to optimize PHP for a Linode 1GB, but you can use this information as a starting point for any size Linode. If you have a larger Linode, you could increase the memory limit to a larger value, like 256M.
Open the PHP configuration files by entering the following command:
1
sudo nano /etc/php5/apache2/php.ini
Verify that the following values are set. All of the lines listed below should be uncommented. Be sure to remove any semi-colons (;) at the beginning of the lines.
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 setting for memory_limit is a general guideline. While this value should be sufficient for most websites, larger websites and some web applications may require 256 megabytes or more.
Save the changes by pressing Control + x and then pressing y.
Create the /var/log/php/ directory for the PHP error log with the following command:
1
sudo mkdir -p /var/log/php
Change the owner of the /var/log/php/ directory to www-data, which the user PHP runs as:
1
sudo chown www-data /var/log/php
Restart Apache to load the PHP module by entering the following command:
1
sudo service apache2 restart
Congratulations! PHP is now installed on your Linode and configured for optimal performance.
No comments:
Post a Comment