Installing a web server
The first instinct may be to install Apache. However, nginx performs better on lower-end hardware. It should be noted that Apache can work just as well if the unnecessary plugins are removed and Apache is configured appropriately. For something that works fairly well out of the box, nginx is a great start.
- First, install nginx and php with an accelerator and a MySQL server with the following command:
# apt-get install nginx php5-fpm php5-mysql php-apc mysql-server
Note
You will be prompted to set a password for MySQL; please make sure you remember it, as you will need it later.
- Then, start the
nginx
service:# service nginx start
- In order to ensure nginx is working, open up a browser and connect to the Raspberry Pi's IP. You should be greeted with a Welcome to nginx! message.
Note
The configuration file for nginx is located in
/etc/nginx/nginx.conf
. The file then imports additional configuration files from/etc/nginx/conf.d/*.conf
and/etc/nginx/sites-enabled/*
. The idea is that you store the configuration for the individual sites you want to run in the/etc/nginx/sites-available/
directory and then symlink the ones you wish to enable into the/etc/nginx/sites-enabled/
directory. - Next, create a configuration for our site based on the default and then enable it by running the following commands:
# cp /etc/nginx/sites-available/default /etc/nginx/sites- available/pisite # unlink /etc/nginx/sites-enabled/default # ln -s /etc/nginx/sites-available/pisite /etc/nginx/sites- enabled/default
- After this, change the content of
/etc/nginx/sites-available/pisite
as follows:server { root /srv/www; index index.html index.htm; }
- At this point, this is not a valid entry, as
/srv/www
does not exist. Create the directory and add a simpleindex.html
by executing the following commands:# mkdir /srv/www # echo "Hello world" > /srv/www/index.html
- Finally, reload the
nginx
configuration so that our changes take effect by running this command:# service nginx reload
Refresh the web page to ensure the new site is properly configured.
Adding PHP support
Now that the web server is working, it is a good idea to configure it to use PHP, using the following steps:
- First, start the
php5-fpm
service, which nginx will communicate with to provide PHP support using FastCGI. This can be done by running the following command:# service php5-fpm start
- Then, modify the
pisite
configuration as follows:server { root /srv/www; index index.php index.html index.htm; location ~ \.php$ { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } }
- Next, remove the old index file by running this command:
# rm /srv/www/index.html
- Create
/srv/www/index.php
with the following content:<?php phpinfo(); ?>
The
<?php … ?>
tags indicate that the contents is to be interpreted as PHP. Thephpinfo()
function is used to return configuration information. - Finally, reload the configuration:
# service nginx reload
Refreshing the page should bring up information about your PHP configuration, indicating that PHP is working.
Now you have installed popular LAMP server on your Raspberry Pi. Web application development using the LAMP stack is the preferred option for developers to create a stable, reliable, and highly efficient application. Moreover, developing the application using LAMP stack and deploying code on LAMP stack is comparatively easy. Having installed all these stacks, you might want to host your own personal blog/website using Raspberry Pi powered by popular WordPress.
Installing WordPress
Once the server is installed, it can be used to host a personal website powered by WordPress:
- First, download and extract WordPress to the
www
directory by running the following commands:# wget http://wordpress.org/latest.zip # unzip latest.zip -d /srv/www/
- Then, launch the MySQL command-line tool. When prompted for a password, enter the password setup when installing MySQL:
# mysql -u root -p
- Next, create a new database called
piwordpress
by entering the following command:> CREATE DATABASE piwordpress;
- After this, create a username
wordpress
with thepassword
password and give the user all privileges to thepiwordpress
database by entering this command:> GRANT ALL PRIVILEGES ON piwordpress.* TO "wordpress"@"localhost" IDENTIFIED BY "password";
- Please replace
password
in the preceding code with something more secure. Then, reload the privileges and exit the MySQL shell with the following commands:> FLUSH PRIVILEGES; > EXIT
- Next, give full write access to the
wordpress
directory for the setup process by running the following command:# chmod a +w /srv/www/wordpress
- Open the IP of the Raspberry Pi in the browser followed by
/wordpress/wp-admin/install.php
. - Go through the setup process using the parameters outlined in the fourth step. The tables prefix can be anything.
- Finally, adjust the ownership and permissions by running the following commands:
# find /srv/www/wordpress -type d -exec chmod 755 {} \; # find /srv//www/wordpress -type f -exec chmod 644 {} \; # chown www-data:www-data /srv/www/wordpress/* -R # chmod 400 /srv/www/wordpress/wp-config.php
The WordPress installation can now be accessed through http://IP/wordpress
in your browser.
Note
WordPress and PHP have historically many security holes, and simple mistakes by a plugin or theme coder can make your Raspberry Pi vulnerable to attack. Take extra care by not storing sensitive information on the Raspberry Pi, installing a security plugin, configuring fail2ban, and monitoring your logs. Always search for more ways to secure your server and data.