Category Archives: Apache

How To Install MediaWiki on Ubuntu 12.04

About MediaWiki

MediaWiki is a free open source wiki program that allows users to create their own personal wiki sites. Originally built for Wikpedia, MediaWiki is written in PHP and uses a backend database, like MySQL, to store its data.

Setup


The steps in this tutorial require the user to have root privileges.  
Before working with MediaWiki, you need to have LAMP installed on your server. If you don’t have the Linux, Apache, MySQL, PHP stack on your server, you can find the tutorial for setting it up http://mohammedwaseem.in/install-lamp-ubuntu/  .

Once you have the user and required software on the virtual private server, you can start installing MediaWiki!

Install Media Wiki

Because the apt-get package of MediaWiki is very out of date, MediaWiki should be installed from source. We can start off by downloading the latest version:

curl -O http://releases.wikimedia.org/mediawiki//1.19/mediawiki-1.19.9.tar.gz
After downloading tar, untar it.
tar -xvzf mediawiki-1.19.9.tar.gz

Move the contents of the MediaWiki directory to a a more convenient location:
sudo mv mediawiki-1.19.9 /etc/mediawiki
Create a symbolic link between the MediaWiki directory and Apache’s document root:
sudo ln -s /etc/mediawiki/ /var/www
And finish up by restarting apache.
sudo service apache2 restart

Create a MySQL User and Database


Although this step is not required for a successful installation of MediaWiki, it is definitely a recommended measure for added security. 

During the installation process, you will be asked to supply either a database and associated user for a MediaWiki database or your MySQL root password. MediaWiki uses this information to create the required wiki database and tables.

Setting up a dedicated database can be done very quickly and removes the need for sharing the MySQL root information and the associated security risks. We can go ahead and set up the database now:

Log into MySQL on your server:

mysql -u root -p


Once you are logged in, create your MediaWiki database. The default name that MediaWiki has in their configuration form is my_wiki, but you can name it whatever you prefer:

create database my_wiki;


Additionally, you will need to provide a user for the new database with the permissions that MediaWiki requires (replace the username and password below with details of your choice):

grant index, create, select, insert, update, delete, alter, lock tables on my_wiki.* to 'wikiuser'@'localhost' identified by 'password';


Finish up by implementing the changes and quitting MySQL:

FLUSH PRIVILEGES;
exit;


The next part of the installation needs to be completed through the onscreen instructions located at [domain]/mediawiki/index.php

Set Up MediaWiki


Go through and fill out the Media Wiki Site configuration in the browser. 

When you reach the “MySQL settings” section of the setup page, leave the Database Host as localhost and then enter in the MySQL database name and username and password, if you configured them in the previous step.

If you did not set up a new user for MediaWiki, write in root for the username and the root password in the password field.

You can leave the “MySQL specific options” section as is.

Once you have finished walking through the the configuration, keep pressing continue until you reach the page that says, “Complete!” 

Complete!

The LocalSettings.php file should start downloading automatically. Make sure that you have downloaded the file before closing out of the page. LocalSettings.php is auto-generated—if you do not have the Local Settings file, you will have to generate it once again by going through all of the MediaWiki setup steps.

Once the file downloads, you should upload it to /etc/mediawiki, or whichever directory contains MediaWiki’s “index.php” file on your virtual server. 

You can copy the LocalSettings.php file from your computer to the server, with SCP (Secure Copy), an SSH-based way of transferring files:

cp /path/to/LocalSettings.php /etc/mediawiki


After the file is uploaded, you will be able to access your personal wiki at[domain]/mediawiki.


	

How To Create a SSL Certificate on Apache for Ubuntu 12.04

About SSL Certificates

A SSL certificate is a way to encrypt a site’s information and create a more secure connection. Additionally, the certificate can show the virtual private server’s identification information to site visitors. Certificate Authorities can issue SSL certificates that verify the server’s details while a self-signed certificate has no 3rd party corroboration.

Set Up

The steps in this tutorial require the user to have root privileges on the VPS. You can see how to set that up here in steps 3 and 4.

Additionally, you need to have apache already installed and running on your virtual server.
If this is not the case, you can download it with this command:

sudo apt-get install apache2


Step One—Activate the SSL Module


The next step is to enable SSL on the droplet.
sudo a2enmod ssl
Follow up by restarting Apache.
sudo service apache2 restart
Step Two—Create a New Directory
We need to create a new directory where we will store the server key and certificate
sudo mkdir /etc/apache2/ssl

Step Three—Create a Self Signed SSL Certificate


When we request a new certificate, we can specify how long the certificate should remain valid by changing the 365 to the number of days we prefer. As it stands this certificate will expire after one year.
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt
With this command, we will be both creating the self-signed SSL certificate and the server key that protects it, and placing both of them into the new directory.

This command will prompt terminal to display a lists of fields that need to be filled in.

The most important line is "Common Name". Enter your official domain name here or, if you don't have one yet, your site's IP address.
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]:New York
Locality Name (eg, city) []:NYC
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Awesome Inc
Organizational Unit Name (eg, section) []:Dept of Merriment
Common Name (e.g. server FQDN or YOUR name) []:example.com                  
Email Address []:webmaster@awesomeinc.com

Step Four—Set Up the Certificate


Now we have all of the required components of the finished certificate.The next thing to do is to set up the virtual hosts to display the new certificate. 

Open up the SSL config file:
 nano /etc/apache2/sites-available/default-ssl
Within the section that begins with <VirtualHost _default_:443>, quickly make the following changes.

Add a line with your server name right below the Server Admin email:
 ServerName example.com:443
Replace example.com with your DNS approved domain name or server IP address (it should be the same as the common name on the certificate).

Find the following three lines, and make sure that they match the extensions below:
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/apache.crt
SSLCertificateKeyFile /etc/apache2/ssl/apache.key
Save and Exit out of the file.

Step Five—Activate the New Virtual Host


Before the website that will come on the 443 port can be activated, we need to enable that Virtual Host:
sudo a2ensite default-ssl
You are all set. Restarting your Apache server will reload it with all of your changes in place.
sudo service apache2 reload
In your browser, type https://youraddress, and you will be able to see the new certificate.

Robots.txt

Web site owners use the /robots.txt file to give instructions about their site to web robots; this is called The Robots Exclusion Protocol.

It works likes this: a robot wants to vists a Web site URL, say http://www.example.com/welcome.html. Before it does so, it firsts checks for http://www.example.com/robots.txt, and finds:

User-agent: *
Disallow: /

The “User-agent: *” means this section applies to all robots. The “Disallow: /” tells the robot that it should not visit any pages on the site.

There are two important considerations when using /robots.txt:

  • robots can ignore your /robots.txt. Especially malware robots that scan the web for security vulnerabilities, and email address harvesters used by spammers will pay no attention.
  • the /robots.txt file is a publicly available file. Anyone can see what sections of your server you don’t want robots to use.

So don’t try to use /robots.txt to hide information.

How to create a /robots.txt file

Where to put it

The short answer: in the top-level directory of your web server.

The longer answer:

When a robot looks for the “/robots.txt” file for URL, it strips the path component from the URL (everything from the first single slash), and puts “/robots.txt” in its place.

For example, for “http://www.example.com/shop/index.html, it will remove the “/shop/index.html“, and replace it with “/robots.txt“, and will end up with “http://www.example.com/robots.txt”.

So, as a web site owner you need to put it in the right place on your web server for that resulting URL to work. Usually that is the same place where you put your web site’s main “index.html” welcome page. Where exactly that is, and how to put the file there, depends on your web server software.

Remember to use all lower case for the filename: “robots.txt“, not “Robots.TXT.

What to put in it

The “/robots.txt” file is a text file, with one or more records. Usually contains a single record looking like this:

To exclude all robots from the entire server
User-agent: *
Disallow: /
To allow all robots complete access
User-agent: *
Disallow:

(or just create an empty “/robots.txt” file, or don’t use one at all)

To exclude all robots from part of the server
User-agent: *
Disallow: /cgi-bin/
Disallow: /tmp/
Disallow: /junk/
To exclude a single robot
User-agent: BadBot
Disallow: /
To allow a single robot
User-agent: Google
Disallow:

User-agent: *
Disallow: /
To exclude all files except one

This is currently a bit awkward, as there is no “Allow” field. The easy way is to put all files to be disallowed into a separate directory, say “stuff”, and leave the one file in the level above this directory:

User-agent: *
Disallow: /~joy/stuff/

Alternatively you can explicitly disallow all disallowed pages:

User-agent: *
Disallow: /~joy/junk.html
Disallow: /~joy/foo.html
Disallow: /~joy/bar.html.

How to protect file with HTAccess

Protection with htaccess is an important way to protect your files from unwanted access. Most people  protect an entire directory or website, you can also protect files with password.

Here’s How:

Example:

I want to protect file “password.ini”

path = /var/www/html/mydomain.com/password.ini

Go to cd /var/www/html/mydomain.com

Open a new text file called .htaccess

  1. .htaccess
  2. Add the following to the file:

<Files “password.ini”>
deny from all
</Files>

save it.

now go to your browser and type, http://mydomain.com/password.ini

Forbidden ,You don’t have permission to access /password.ini  on this server.!!!

How to Install LAMP on Ubuntu

The following tutorial presumes you understand what a LAMP Server is, how to work a website from the back end and how to install software using either the Software Centre or the Terminal. It also expect experience running other Basic Terminal Commands.

There is also an excellent article on Digital Ocean that may be of more relevance if you are working on a remote or public facing server.

Install Apache

To install Apache you must install the Metapackageapache2. This can be done by searching for and installing in the Software Centre, or by running the following command.

Install MySQL

To install MySQL you must install the Metapackagemysql-server. This can be done by searching for and installing in the Software Centre, or by running the following command.

Install PHP

To install PHP you must install the Metapackagesphp5 and libapache2-mod-php5. This can be done by searching for and installing in the Software Centre, or by running the following command.

Check Apache

Open a web browser and navigate to http://localhost/. You should see a message saying It works!

Restart Server

Your server should restart Apache automatically after the installation of both MySQL and PHP. If it doesn’t, execute this command.

Check PHP

You can check your PHP by executing any PHP file from within /var/www/. Alternatively you can execute the following command, which will make PHP run the code without the need for creating a file .

Congratulations, you have just Installed a Ubuntu LAMP Server!

WordPress Installation on Ubuntu Linux with Apache and MySQL

This article describes an installation of WordPress on a Ubuntu Linux system using Apache web-server and MySQL database. WordPress is an CMS ( Content Management System ), mostly used as a blog publishing web application. WordPress is written in PHP language and uses MySQL database to store data.

Preliminary notes about the system used for this WordPress installation:

  • Ubuntu Linux 10.04 – Worpress 3.1 ( Reinhardt )
  • Kernel 2.6.32-21-generic #32-Ubuntu SMP
  • mysql Ver 14.14 Distrib 5.1.41
  • Apache/2.2.14 (Ubuntu)
  • PHP 5

1. Step 1: Prerequisites installation

# apt-get install php5-mysql mysql-server

2. Step 2: Download and Decompress WordPress

We start by downloading a latest version of WordPress and decompressing it into /var/www/wordpress .

# cd /var/www
# wget http://wordpress.org/latest.zip
# unzip latest.zip

At this point all files should be within /var/www/wordpress directory.

3. Step 3: Apache Configuration

In this step we will create a new apache site called wordpress, enable it and disable default apache website.

# cd /etc/apache2/sites-available
# sed 's/www/www\/wordpress/g' default > wordpress
# a2ensite wordpress
# a2dissite default

Restart apache to apply all changes:

# /etc/init.d/apache2 restart

4. Step 4: Creating WordPress database

Next, we will need to create a MySQL database to be used with our WordPress installation:

  • Username: wordpress
  • Password: wordpress
  • Database name: wordpress
# mysql -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 39
Server version: 5.1.41-3ubuntu12.10 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE DATABASE wordpress;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE USER 'wordpress'@'localhost' IDENTIFIED BY 'wordpress';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON wordpress.* to wordpress@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

5. Step 5: Creating wp-config.php

To continue a WordPress installation we need to create a wp-config.php file to accommodate all WordPress configuration needs. Use following lines to insert database name, database user and password into wp-config.php file.

# cd /var/www/wordpress/
# cp wp-config-sample.php wp-config.php
# sed -i 's/database_name_here/wordpress/' wp-config.php
# sed -i 's/username_here/wordpress/' wp-config.php
# sed -i 's/password_here/wordpress/' wp-config.php
# chmod 600 wp-config.php

OR use text editor of your choice to supply wp-config.php with correct information:

// ** 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', 'wordpress');

/** MySQL database password */
define('DB_PASSWORD', 'wordpress');

/** MySQL hostname */
define('DB_HOST', 'localhost');

Ensure that apache have access to wordpress installation files. If this is your local system make sure that www-data owns of all files:

# chown -R www-data.www-data /var/www/wordpress

NOTE: Although localhost as a MySQL hostname within wp-config.php file is a most common option, you may need to change this value according to your webhost needs.

Here are some possible MySQL host values:

Hosting Company MySQL hostname
1and1 db12345678
AN Hosting localhost
A Small Orange localhost
BlueHost localhost
DreamHost mysql.example.com
GoDaddy h41mysql52.secureserver.net
HostGator localhost
HostICan localhost
ICDSoft localhost:/tmp/mysql5.sock
LaughingSquid localhost
MediaTemple GridServer internal-db.s44441.gridserver.com
NearlyFreeSpeech.Net username.db
one.com localhost
pair Networks dbnnnx.pair.com
Rackspace Cloud mysql50-01.wc1.dfw1.stabletransit.com
Yahoo mysql
Hosts with cPanel localhost
Hosts with Plesk localhost
Hosts with DirectAdmin localhost
Tophost.it sql.your-domain-name.it

Optional ( recommended ) step:

Create slathash for wordpress. Visit https://api.wordpress.org/secret-key/1.1/salt/ and copy all lines into your wp-config.php file replacing current non-set values.

6. Step 6: Install WordPress

At this stage everything should be ready to install WordPress on your ubuntu Linux system. Open your browser and point it to your webserver’s IP / hostname . Follow WordPress installer instructions to finish WordPress installation.

7. Troubleshooting

WordPress blank page problem:

If you have created wp-config.php file and navigated to your WordPress installation page using your browser you may see a blank page. This is mostly a result of wp-config.php misconfiguration. Check all data you have filled in to your wp-config.php as well as check actual permissions of this file.

If worst comes to worst you may attempt to continue WordPress installation without wp-config.php where WordPress installer will offer you creation of new wp-config.php file.

Installing Apache and PHP from source

I’ve just been through the process of installing the latest versions (as of this writing) of Apache 2.4.2 and PHP 5.4.5 from source on a new Debian 6 virtual machine for the first time.

I had some small issues along the way and thought I’d do a short post on the process I went through to get it working and resolve various errors which may help someone out. First we will install Apache, followed by PHP.

Prerequisites

Before starting you will need make, gcc and g++ installed.

apt-get install make gcc g++
I also had the latest version of MySQL installed (5.5.25a), as I wanted to use this later on. The latest version was downloaded from the Dotdeb repository

Installing Apache 2.4.2 from source

Apache was then downloaded from http://httpd.apache.org/download using wget on one of the mirrors in my country and then extracted.

wget http://apache.mirror.uber.com.au//httpd/httpd-2.4.2.tar.gz
tar xf httpd-2.4.2.tar.gz
cd httpd-2.4.2/
./configure –enable-so

Configuring Apache Portable Runtime library …
checking for APR… no
configure: error: APR not found. Please read the documentation.
–enable-so is needed later on for PHP.

At this point I downloaded APR 1.4.6 from apr.apache.org/download using wget and then extracted it.

cd ..
wget http://apache.mirror.uber.com.au//apr/apr-1.4.6.tar.gz
tar xf apr-1.4.6.tar.gz
cd apr-1.4.6/
./configure
make
make install
Apache will also need APR-util installed, which can be downloaded from the apr.apache.org/download page too. When you run configure you will have to specify where the apr config is, you should see this location in the output from make install after finishing apr above.

cd ..
wget http://apache.mirror.uber.com.au//apr/apr-util-1.4.1.tar.gz
tar xf apr-util-1.4.1.tar.gz
cd apr-util-1.4.1/
./configure –with-apr=/usr/local/apr/bin/apr-1-config
make
make install
Now that’s complete we can try Apache again.

cd ../httpd-2.4.2/
./configure –enable-so

checking for pcre-config… false
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/
pcre-8.30.tar.gz was then downloaded from http://pcre.org and for something different, I uploaded it to the server with SFTP to the root directory.

cd ..
tar xf pcre-8.30.tar.gz
cd pcre-8.30/
./configure
make
make install
Now to try Apache again…

cd ../httpd-2.4.2/
./configure –enable-so
make
make install
That’s it, Apache should now be ready to go, let’s move into the default installation and start it up.

cd /usr/local/apache2/bin/
./apachectl start
/usr/local/apache2/bin/httpd: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
ldconfig
./apachectl start
./apachectl -v
Server version: Apache/2.4.2 (Unix)
Server built: Jul 26 2012 20:51:23
That error relating to the shared libraries was resolved by running ldconfig, see the man page on that for more information.

Browsing to the IP address in my web browser now brings up the Apache default page. As ServerSignature is enabled by default, using Firebug for FireFox I can see “Apache/2.4.2 (Unix)” as the Server header in the response headers, confirming the new install of Apache has served my page.

Installing PHP 5.4.5 from source

PHP was downloaded from http://au.php.net/get/php-5.4.5.tar.gz/from/a/mirror and then uploaded to the root directory on the server using SFTP.

I’m configuring PHP with apxs, this wasn’t working for me unless Apache was configured with –enable-so. I was after MySQL support, feel free to leave –with-mysql off.

cd /root
tar xf php-5.4.5.tar.gz
cd php-5.4.5/
./configure –with-apxs2=/usr/local/apache2/bin/apxs –with-mysql=mysqlnd

configure: error: xml2-config not found. Please check your libxml2 installation.
apt-get install libxml2-dev
./configure –with-apxs2=/usr/local/apache2/bin/apxs –with-mysql=mysqlnd
make
make install
php -v
PHP 5.4.5 (cli) (built: Jul 26 2012 21:18:58)
Configuration

Once you have both Apache and PHP installed and running there is a bit of configuration to do so that Apache knows how to use PHP. I created a phpinfo file here /usr/local/apache2/htdocs/phpinfo.php and it will not display in a browser prior to configuration.

First off, let’s copy the library file over to Apache.

cp /root/php-5.4.5/libs/libphp5.so /usr/local/apache2/modules/
Next you will want to edit the Apache configuration file in /usr/local/apache2/conf/httpd.conf and add the following lines into it.

AddType text/html .php
AddHandler php5-script .php
This line should already be in there, referring to the .so file that was previously copied.

LoadModule php5_module modules/libphp5.so
You probably want to edit DirectoryIndex index.html and include index.php after this.

Once complete, restart Apache.

cd /usr/local/apache2/bin/
./apachectl restart

Configure apache to use virtual hosts on ubuntu server

This tutorial assumes you have Apache setup correctly with a default config.
In this tutorial we will be using just one domain name (domain.com) replace this with your domain anytime you see it. at the end will be instruction on how to add your second, third, fourth, etc…

Making A Uniform Directory Structure

First we need to create our directory structure, you can do this however you like but I recommend following some sort of naming standard to avoid confusion.
cd /var/www
mkdir -p vhosts/domain.com/htdocs (this is where you will put your html.)
mkdir -p vhosts/domain.com/log (this will be for your log files)
chmod -R 755 vhosts/domain.com
[edit]Making A Default Page

The content of the public folder is, naturally, up to you but for this example I am going to use a very simple html file so we can check the virtual hosts work.
So for the domain create an index.html file:
cd vhosts/domain.com/htdocs
nano index.html
add the following into the file:



domain.com


OK. Now we have the basic structure needed for our domains we can look at defining one virtual host.
[edit]Configuring NameVirtualHost

With virtual hosts, one thing that often catches people out is the NameVirtualHost setting.
For each port that Apache listens to, we need to define a NameVirtualHost. The issue that can catch people lies in the fact that you can only define it once per port.
In itself, that’s no problem but the default vhost has already defined a generic NameVirtualHost – adding another one will cause warnings and errors.
The easiest thing to do is to remove the generic NameVirtualHost from the default vhost file and then add some specific ones to the main apache config file.
That way, if we ever delete or change the default vhost we don’t have to worry about where the NameVirtualHost setting is defined and so on.
OK. Let’s move into the apache directory and then open the default vhost:
cd /etc/apache2/

sudo nano sites-available/default
Delete the ‘NameVirtualHost *’ line and change the next ‘VirtualHost’ line so the file begins like this:

ServerAdmin webmaster@localhost

DocumentRoot /var/www/

Once that is done, we can open the main apache2.conf:
sudo nano apache2.conf
Make sure the following line is in the file and not commented out:
Include /etc/apache2/sites-enabled/
Also make sure the following is in the file:
NameVirtualHost *:80


NameVirtualHost *:443

If the above code is not in apache2.conf, I found that it would cause errors when the second vhost was enabled.
Let’s reload Apache now:
sudo /etc/init.d/apache2 reload
[edit]Making A Custom Virtual Host

We’ve set up the basics and now we’re ready to add our own virtual hosts so we can start to serve our domains.
Let’s go ahead and create the vhost file for domain.com:
nano /etc/apache2/sites-available/domain.com
The contents need to look like this:
# Place any notes or comments you have here
# It will make any customisation easier to understand in the weeks to come

# domain: domain1.com
# public: /var/www/vhosts/domain.com/domain.com/

# Admin email, Server Name (domain name) and any aliases
ServerAdmin webmaster@domain.com
ServerName domain.com
ServerAlias www.domain.com

# Index file and Document Root (where the public files are located)
DirectoryIndex index.html
DocumentRoot /var/www/vhosts/domain.com/htdocs

# Custom log file locations
LogLevel warn
ErrorLog /var/www/vhosts/domain.com/log/error.log
CustomLog /var/www/vhosts/domain.com/log/access.log combined


[edit]Using a2ensite

Now we have the site available, we need to enable it:
sudo a2ensite domain.com
The result should be:
Site domain.com installed; run /etc/init.d/apache2 reload to enable.
This does seem like good advice so:
/etc/init.d/apache2 reload
[edit]Test And Navigate

Now navigate to your site:
http://domain.com
[edit]Rinse and Repeat

To create and enable domain2.com simply go through the process again:
nano /etc/apache2/sites-available/domain2.com

Enter the details for domain2.com as per the example shown above
Then enable the site and restart Apache:
sudo a2ensite domain2.com

sudo /etc/init.d/apache2 reload
Finally navigate to your second domain!