Setting up a Linux Server – For Hosting Website With WordPress

Are you interested in turning your Linux machine into a web server? This is a detailed tutorial on how to build a Linux, Apache, MySQL, and PHP server also known as LAMP. These instructions are for Ubuntu 12.04.

Obviously, if you’re running Ubuntu you already have a Linux part taken care of. You will need a desktop or laptop running Ubuntu 12.04. You could probably use an old single core Pentium 4 machine with 512 MB RAM at a minimum.

First, you want to test some security settings. Ubuntu 12.04 does not allow root logins by default and you must either sudo or sudo su for root access. You will need root privileges for some of these steps.

Also, you may want to install Secure Shell, so that you may access your Linux web server remotely with command line. For this tutorial you can either type :~$ sudo or :~$ sudo su to elevate to root.

Also for this tutorial feel free to copy and paste any commands in bold. At a command prompt type:

apt-get install openssh-server

Once successfully installed there is no configuration needed. Open up a terminal application similar to Putty and then connect to your web server using port 22. Login with your username and password.

Next, you will install Apache web server software. Type following command:

apt-get update
apt-get install apache2

This will check for Ubuntu 12.04 repository updates before you install Apache and then install Apache. Test Apache by opening up your ip address in a web browser like this example:

http://192.168.1.1

You will see a page that says:

“It works!
This is the default web page for this server.
The web server software is running but no content has been added, yet.”

If you do not know your web server internet protocal address, you can type this command to obtain it:

ifconfig eth0 | grep inet | awk ‘{ print $2 }’

Next, you will install MySQL database software. Type in this entire command:

apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

MySQL will prompt you to set a root password. Now you want to activate MySQL by typing this command:

mysql_install_db

Once MySQL is activated you want to run an installation script with this command:

/usr/bin/mysql_secure_installation

You will be prompted to enter your MySQL root password. You will be prompted to change your MySQL root password, you can enter N for no. MySQL installation will ask you four final questions, which you can enter Y for all of them.

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
… Success!

Normally, root should only be allowed to connect from ‘localhost’. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
… Success!

By default, MySQL comes with a database named ‘test’ that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
– Dropping test database…
… Success!
– Removing privileges on test database…
… Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
… Success!

Cleaning up…

Now that MySQL is successfully installed, you can then install PHP. PHP is a scripting language that WordPress uses. Type this command to install PHP:

apt-get install php5 libapache2-mod-php5 php5-mcrypt

This PHP installation might prompt you twice. If so, you can enter Y each time. To server appropriate php files you want to add index.php to a directory configuration file.

Open /etc/apache2/mods-enabled/dir.conf with a text editor for example Nano or vi. You want to add index.php to this directory index:

 

DirectoryIndex index.php index.html index.cgi index.pl index.php index.xhtml index.htm

 

Save and close that file with your text editor. By default PHP has many modules available but not installed. You can type this command to list any modules available.

apt-cache search php5-

To install a module type apt-get install “module name” like this example:

apt-get install php5-cgi

You can install multiple modules at once by adding them to that command seperated by a space in between them. Now that you have successfully installed PHP, it is recommended you create a PHP info file on your web server. This allows you to see detailed information about your web server.

Open the following file with your text editor of choice.

/var/www/info.php

Insert the following phpinfo code into this info.php file.

Save and close that file. Restart Apache web server with this command:

service apache2 restart

Browse to this file in a web browser like so:

http://192.168.1.1/info.php

You will see a PHP page with your web server system information. Finally, you can install WordPress now. Take a note of what directory you are located in on your command line.

Download the latest version of WordPress with following command:

wget http://wordpress.org/latest.tar.gz

Uncompress that file with this command:

tar -xzvf latest.tar.gz

This will unzip that tar file creating a wordpress folder in your current directory. You will need to create your MySQL database and user now. Log into MySQL with this command:

mysql -u root -p

Enter your MySQL root password. Now create a MySQL database with this command:

CREATE DATABASE wordpress;

Create a user for this database with following command:

CREATE USER wordpressuser@localhost;

Set a password for this created user with a command:

SET PASSWORD FOR wordpressuser@localhost= PASSWORD(“password”);

Now you are going to grant all permissions to this user via a following command:


GRANT ALL PRIVILEGES ON wordpress.* TO wordpressuser@localhost IDENTIFIED BY ‘password’;

Refresh MySQL now:

FLUSH PRIVILEGES;

Exit out of MySQL shell by typing:

exit

You will need to finish with rest of WordPress installation. Copy this sample WordPress file to a different location by this command:

cp wordpress/wp-config-sample.php wordpress/wp-config.php

Now you want to open this configuration file with your text editor of choice:

wordpress/wp-config.php

In this file you will be changing DB_NAME, DB_USER, and DB_PASSWORD. Following is an example of what it should look like using your prior settings:

// ** MySQL settings – You can get this info from your web host ** //
/** The name of the database for WordPress */
define(‘DB_NAME’, ‘wordpress‘);

/** MySQL database username */
define(‘DB_USER’, ‘wordpressuser‘);

/** MySQL database password */
define(‘DB_PASSWORD’, ‘password‘);

Save and close that file. Now you are going to move those files and folders in a wordpress folder that you uncompressed earlier:

rsync -avP wordpress/ var/www/

You are almost finished but need to set some permissions. Change directory to the web root folder:

cd /var/www/

Give ownership now of this directory for Apache web server to:

chown www-data:www-data /var/www -R
chmod g+w /var/www -R

Install a module for WordPress with this command:

apt-get install php5-gd

Access your WordPress online installation by browsing to:

http://192.168.1.1/wp-admin/install.php

You will see a drop down list of languages to choose from. Click on “Continue”. Now you will see this page:

Welcome

Welcome to the famous five-minute WordPress installation process! Just fill in the information below and you’ll be on your way to using the most extendable and powerful personal publishing platform in the world.

You can change settings on this page later. Type in a “Site Title”. Type in a “Username” to log into WordPress.

Set your WordPress username password in both boxes in “Password, twice”. Type in your email address in order to reset your password if you forget it later in “Your E-mail”. In a “Privacy” box.

Click on “Install WordPress”. After a few moments you should see a success page with this:

Success!

WordPress has been installed. Were you expecting more steps? Sorry to disappoint.
Username username
Password Your chosen password.

Click on a “Log In” button to log into your newly created WordPress website on your Linux web server. Congratulations