Book Image

Learning Raspberry Pi

By : Samarth Shah, Serge Schneider
Book Image

Learning Raspberry Pi

By: Samarth Shah, Serge Schneider

Overview of this book

Table of Contents (14 chapters)
Learning Raspberry Pi
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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.

  1. 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.

  2. Then, start the nginx service:

    # service nginx start
    
  3. 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.

  4. 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
    
  5. After this, change the content of /etc/nginx/sites-available/pisite as follows:

    server {
            root /srv/www;
            index index.html index.htm;
    }
    
  6. At this point, this is not a valid entry, as /srv/www does not exist. Create the directory and add a simple index.html by executing the following commands:

    # mkdir /srv/www
    # echo "Hello world" > /srv/www/index.html
    
  7. 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:

  1. 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
    
  2. 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;
            }
    }
    
  3. Next, remove the old index file by running this command:

    # rm /srv/www/index.html
    
  4. Create /srv/www/index.php with the following content:

    <?php
            phpinfo();
    ?>
    

    The <?php … ?> tags indicate that the contents is to be interpreted as PHP. The phpinfo() function is used to return configuration information.

  5. 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:

  1. 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/
    
  2. Then, launch the MySQL command-line tool. When prompted for a password, enter the password setup when installing MySQL:

    # mysql -u root -p
    
  3. Next, create a new database called piwordpress by entering the following command:

    > CREATE DATABASE piwordpress;
    
  4. After this, create a username wordpress with the password password and give the user all privileges to the piwordpress database by entering this command:

    > GRANT ALL PRIVILEGES ON piwordpress.* TO "wordpress"@"localhost" IDENTIFIED BY "password";
    
  5. 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
    
  6. Next, give full write access to the wordpress directory for the setup process by running the following command:

    # chmod a +w /srv/www/wordpress
    
  7. Open the IP of the Raspberry Pi in the browser followed by /wordpress/wp-admin/install.php.

  8. Go through the setup process using the parameters outlined in the fourth step. The tables prefix can be anything.

  9. 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.