[Turn] build lamp service on Raspberry Pi

Source: Internet
Author: User
Tags raspi

This article describes how to configure the lamp service on the Raspberry Pi in detail before introducing the LNMP environment of the Raspberry Pi.

For lamp, one of the most popular server configurations, the meaning of lamp is:

    • Linux-Operating system
    • Apache-Web server (HTTP) software
    • Mysql-Database service
    • PHP or Perl-programming languages

This configuration may be too heavyweight for most Raspberry Pi users, but most users can learn the server configuration process, which is a good way to learn how to configure a server. I might write an article on the configuration of a lightweight server later.

All configurations are done under the command line. This may be more difficult than a little mouse, but there will be many advantages, such as remote management, configuration services, CPU time can be used to provide Web services, rather than to render a graphical interface, after all, Web server is the sole purpose of Web services.

Why do you use Raspberry Pi?

In addition to the geek answer, "because I can do this," I think there are some other good reasons.
The main purpose of learning Linux on this site is to teach Linux tricks. The best way to learn Linux is to do something with Linux. Building a server is a good place to start.
Learning Network Programming Tree The aim of the Plum Foundation is to teach children to program. Network programming is a very useful skill. Although the first to learn desktop programming good, or to learn network programming good, has been controversial, but there is no doubt that network programming is very practical.
As an interface The Raspberry Pi is suitable for collecting data from various sensors and can build a server to access this data.
Private network devices can be made into a private network device used in a home, such as streaming media devices.
When developing a Web application as a test or development server, it is better to have a dedicated test server. Ideally, the test server hardware and software should be consistent with the production environment, but if not realistic, can be sent to replace with raspberry, the overhead is very low.
Server as a production environment at first I would say that it would be foolish to think of Raspberry Pi as a production environment server. But I recalled that the server configuration of my initial personal blog was not much stronger than the Raspberry Pi, which was before 2007. The server has a bit more memory than the Raspberry Pi, and can run a full WordPress site, along with some other CGI scripts. Now the site dynamic content more, the site of the file is also larger, but if it is a personal site, with the Raspberry Pi is enough.

Debian Linux

The configuration of the server is based on the Debian Raspberry Pi image on the Raspberry Pi download page.

This article requires the Raspberry Pi to be able to connect to the extranet, and the following assumes that the Raspberry Pi is connected to the home router using a network cable.

Security

The first thing to do is to make the Raspberry Pi more secure. The system in this image has a default user name and password, and after the Sisu, anyone can log in and use the Raspberry Pi.

To change your password, enter it after logging in

1 passwd

Then enter the new password after the prompt that appears.

You may want to add an account. The account name I used below is User1, usually the account name should be a personal name. If you don't want to add an account, you can skip this section and go directly to the performance/network step.
The following command can add a new account and set a password.

12 sudouseradd -m user1sudo passwduser1

This is the first time in this article with the sudo command, which will be used many times in the rest of the section. With the sudo command, the user can execute a command as an administrator, as in the example above. If you do not use sudo, the command fails because a normal user cannot create another account. This is a security feature that prevents the system from being compromised, and it also prevents users from misusing some of the commands to disrupt the system (although adding sudo above does not prevent misuse).

The new account must be joined to a specific user group in order to have some permissions for the PI account.

You can add user groups with the Usermod command, or you can change the configuration file directly. I will change the configuration file directly below, so that you can look at the configuration file (in general, if you want to make a few changes, directly change the configuration file will be faster). Please note that if an error occurs while editing the configuration file, it is possible that the account will not be logged in.

There are two commonly used command-line text editors, and the Nano is easier for new users (so I use the nano below) because it is useful and is preinstalled on all Linux. If you can use VI, then use the Nano in the following places, you can use VI.

1 sudonano /etc/group

Browse through the file, find the line where Pi is located, and then add the User1 to the end of the line.

For example

1 adm:x:4:pi,user1

After editing is complete, Ctrl-o saves the file and Ctrl-x exits.

The important thing is that if an account is not on the admin line, the account cannot use sudo, and therefore cannot administer any system. Of course, if you want to add an account and then do not give administrative permissions, then you do not have to change the/etc/group file.

Input

1 exit

Log out, and then log in with your new account to check if it's set up correctly.

The default shell for the new account is the Bourne shell. Bash is an improved version of the Bourne shell that allows users to automatically complete the command line using the arrow keys.

To set the default shell for a new account, enter:

1 chsh-s /bin/bash

If you do not use PI This account later, you can delete it.

1 userdel pi

This is just the first step in raising the PI's security level. Linux security also includes other aspects, such as installing patches after security patches are released (for example, with Apt-get update).

Regulating system performance

In general, tuning server performance is the next thing to do. But for the Raspberry Pi, making an adjustment can improve the performance of the system. We set it here, save the reboot to take effect, then we can save a reboot.
The Raspberry Pi has 256MB (later version is 512MB) memory. But this memory is shared between the graphics and the main system. By default, 64MB is assigned to the graphics system. 64MB of memory is too much for devices that do not run a graphical interface, or rarely run graphical interfaces, such as servers. To reduce the memory of the graphics system to 32MB, enter the following command.

1 sudocp/boot/arm224_start.elf /boot/start.elf

Or, use sudo raspi-config to change the configuration directory. (You can use sudo cp/boot/arm192_start.elf/boot/start.elf to restore the previous settings)

This change takes effect after reboot, we restart later, so we don't have to reboot now.

Set up a network

The next step is to assign a static IP address to the Raspberry Pi. This step is a little more complicated and depends on your own configuration and router type.
By default, the Raspberry Pi requests a dynamic IP to the router. This IP may change and not be easy to connect to your server. Our approach is to give it an immutable address, such as 192.168.1.4
Note that this address can only be used on LAN and cannot be used outside the network-after that we will describe how to configure your router and firewall so that the extranet users can access your server.

First use Ifconfig to see the address assigned to the Raspberry Pi by DHCP--below is the part of the output

1234 eth0      Link encap:Ethernet  HWaddr b8:27:eb:8a:71:a0inet addr:192.168.1.110  Bcast:192.168.1.255  Mask:255.255.255.0

This is saying, Ethernet port 0, address is 192.168.1.110

You also need to know the address of the router, using the route command

12345 $ Route kernel IP routing table destination     Gateway          genmask         Flags Metric ref    use Iface default          192.168.1.1     0.0.0.0         UG     0      0        0 eth0 192.168.1.0     *                255.255.255.0   u     0      0         0 eth0

This is to say that the router's IP address (that is, the gateway) is 192.168.1.1, all the data will be received and sent through this router.

Now, look at the scope of the router IP pool. It depends on your router. The following example uses a Belkin wireless router, which enters 192.168.1.1 In the browser and can see the management interface of the router.

The LAN settings look like this:

In this example, the LAN valid IP range is from 192.168.1.1 to 192.168.1.254. The address of the router is 192.168.1.1, DHCP assigns addresses from 192.168.1.100 to 192.168.1.150 (you can change this range if necessary). The address I assigned to the server is 192.168.1.4.

Configuring static IP Addresses

12 cd/etc/networksudonano interfaces

iface eth0 inet dhcpChange toiface eth0 inet static

123 address 192.168.1.4netmask 255.255.255.0gateway 192.168.1.1

You can take a look at the configuration in/etc/resolv.conf, which has a record (most likely pointing to your default gateway)

1 nameserver 192.168.1.1

You can also point this directly to the DNS server address of the network service provider. Although it is possible to reconnect the network interface without rebooting, I recommend restarting this step to ensure that the configuration is correct.

1 sudoreboot

After logging in, use ifconfig to confirm that the server has a static IP address

1234 eth0      Link encap:Ethernet  HWaddr b8:27:eb:8a:71:a0inet addr:192.168.1.4  Bcast:192.168.1.255  Mask:255.255.255.0
Open SSH

SSH (Secure Shell) is a network protocol that allows you to remotely log in and control your computer using the command line. As the name says, this remote control is secure because the data is encrypted when it is transmitted over the network (so no one else can see your password). It also provides great features such as tunnels, and we don't go into the discussion here.

The SSH service is available on the default mirror, which is also enabled by default. You can use Raspi-config to turn the SSH service on or off.

Now you can access the Raspberry Pi (under the same network) remotely with SSH. If you have another computer on the same network with Linux installed, you can run it at the terminal

1 ssh192.168.1.4

It will then log in with the same user name. If you want to use an account with a different user name, enter the user name in front of the IP and separate it with @. For example

1 ssh[email protected]

There is also ssh on your Mac, you can log in with the same command.

If you use Windows, then there are several options, I recommend open source software putty.

Allow the extranet to access the server

Then, let's set up the router so that SSH and network data can be connected to the Raspberry Pi through the firewall.

Did you get rid of the default password for pi account? If you don't get rid of your default password, anyone can log on to your Raspberry Pi.

The IP address in the home network is the address of the LAN, the LAN is a private network segment, the external network is inaccessible. Your network service provider provides a dynamic IP, which is used by routers. In order for the extranet to have access to your Raspberry Pi, you must convert the address of the Raspberry Pi to an extranet address. This process is called Network address translation (NAT).

The port number that needs to be opened is (HTTP default port), and if you want to log on to an external network, you also need to open port 22 (SSH default).

This step requires a reference to the router's instructions. In this example, my Belkin router is in Firewall > virtual server settings (see), but Netgear routers are in Advanced > Secure >IP forwarding.

Dns

The final step is to bind a domain name to your router's IP. In my personal case, the broadband service provider is Virgin Media. Although they provide a dynamic IP, the address is usually not changed. I registered a static domain name with a domain Name service provider. This domain name needs to be updated only once a year, or when Virgin Media is doing large-scale network maintenance.

If your IP is dynamic and constantly changing, you will have to register for a dynamic Domain name service.

Installing Apache

The Apache server can be downloaded from the Debian source. can be downloaded with apt.

The first thing to do is to update the APT software list. If you do not run sudo apt-get updata, the APT software will not know if there is a new package or version update.

Apache can use the following command to install

1 sudoapt-get installapache2
Install MySQL

MySQL is also available in the source of Debian and can be installed with this command

1 sudoapt-get installmysql-server

During the installation, a prompt will appear to let you enter a password.

This password is the password of the MySQL root user.

Install PHP

Perl is preinstalled on the system, so I'm only writing PHP installation here.

Enter the following command to install PHP 5 and the libraries that PHP needs to access the MySQL database.

12 sudoapt-get install php5sudo apt-get installphp5-mysql
Configuration complete

Once the installation is complete, you can enter your router's IP or domain name in the browser to access your website.

You should be able to see a page that shows "It works", but no other content.

Delete/var/www/index.html and create a/var/www/index.php to test whether the server and PHP are working properly.

This article comes from: Raspberry Pi Lab
Link Address: http://shumeipai.nxez.com/2013/10/13/install-and-config-lamp.html

[Turn] build lamp service on Raspberry Pi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.