2020. 10. 31. 15:46ㆍRaspberry Pi
The Raspberry Pi Series: LAMP Server
안녕하세요 :)
바베입니다, 오늘은 라즈베리파이 시리즈 'LAMP 서버' 편으로 돌아왔는데요! 시작해볼까요?
Hello friends, Babae here.
If you are new to the series, I recommend you go visit the previous parts of the series if at any part you are lost to what each Linux command does.
So this week I will resume the series with building a LAMP server. Before we begin, as usual, I will explain what is a LAMP server and why you would want one.
Server is any computer that provides computational services for other devices. A web server, for example, provides data for your computer, the client, to display on your web browser.
LAMP is an acyronym, which stands for Linux, Apache, MySQL/MariaDB, PHP/Python. We are already a quarter of the way there as your Pi is already running Linux, specifically Raspberry Pi OS on the Debian branch. Next is to set up Apache to handle the incoming and outgoing HTTP(S) traffic. Over the last decade, web technologies have evolved and so have the preferences for them. PHP was a popular scripting language for web development but in recent years, Python has become a more popular jack-of-all-trade language for its readability. While PHP/Python provides the logic for the website, data is stored on databases. MariaDB has been gaining popularity over MySQL in recent years for the preferred open-source database due to performance advantages.
A popular blog tool you might all know is Wordpress, which has been written in PHP. Installing Wordpress will be the end goal for the guide today, but the possibilities are endless.
Let's begin by SSH-ing into your Pi and installing Apache with the following command:
sudo apt install apache2
That's it for the install!
Now, from any device on your network, you can type in the IP address of your Pi into the web browser's address bar. When your Pi receives the HTTP request, it will go into the default directory "/var/www/html/" and load the file named "index.html". Unless you configure the Apache server otherwise, it will always look for a file named index. The first extension it tries to look for is ".html" but will then look for ".php" if "index.html" does not exist.
A quick note on why the specifity on the version number: sometimes software version number does not matter and newer is always better. However, when we talk about web development, where the language evolves, it is important that previously written code to still work properly, especially in a production environment. When there are major changes in the language, often the software will get a new whole version number, oppose to rational increments, to signify that the changes are so huge that there will not be backward compatibility. Apache and Python are such examples.
I would love to go alphabetically through LAMP for the install, but it actually makes more sense to install the P before the M. And, while I said Python is the trending language right now, our goal in this tutorial is install Wordpress so we will be installing PHP. After installing Wordpress, you can build your own blog without knowing neither language anyways.
Installing PHP is as easy as installing Apache:
sudo apt install php
Your server can now serve up PHP pages. Again, we don't need to write any webpages in PHP as we will be using Wordpress.
Lastly, to complete your web server, you will install MariaDB:
sudo apt install mariadb-server php-mysql
Now, we set up a database for Wordpress:
sudo mysql_secure_installation
You can deviate from my following suggestion if you know what you are doing. Upon setup, you do not have a root password so just press 'enter' to proceed. Then 'Y' and 'enter' to set a new password; follow the prompt. I recommend you remove anonymous users, disallow root login remotely, remove test database and access to it, and reload privilege tables now. This is for security purposes.
Now we run MySQL:
sudo mysql -uroot -p
We will then create a database called wordpress:
create database wordpress;
Now we give privileges to root user:
GRANT ALL PRIVILEGES ON wordpress.* TO 'root'@'localhost' IDENTIFIED BY 'your-password-here';
The next command will reload the privileges to your database:
FLUSH PRIVILEGES;
Remember how your Apache will always look for "index.html" first to display? Well, we will be installing Wordpress next and we don't need any of the files from the installation. Let's change directory to where Apache will be looking for our website and remove the existing content:
cd /var/www/html/
sudo rm *
Now, we use a command wget to download the latest version of Wordpress from the Internet:
sudo wget http://wordpress.org/latest.tar.gz
I love when people appropriately name the latest version of their files as it makes tutorials like this timeless.
Another new command we will be using is tar, which is like zip. It is an archiving utility for compressing and extracting files. Enter the following command:
sudo tar xzf latest.tar.gz
Notice there are two arguments used in this command. The 'x' is for extract, 'z' tells tar that the file is gzipped, and 'f' tells tar that the next argument is the filename.
The files will be extracted into a directory called 'wordpress', which makes the whole site under the directory "/var/www/html/wordpress", which is not where Apache is looking.
So, we move all the files from the wordpress folder into the current directory ("/var/www/html" assuming you are following me through the tutorial):
sudo mv wordpress/* .
The '*' is a wildcard and basically all files in the wordpress folder will match it. The '.' means current directory.
We now remove the empty wordpress directory and the tar.gz file we downloaded:
sudo rm -rf wordpress latest.tar.gz
We then give Apache ownership of these files:
sudo chown -R www-data: .
I believe we have seen 'chown' command, change owner, before. The '-R' is similar to the '-r' for the 'rm' command, which means recursive - to include all directories and files within directories. 'www-data' is the owner, and there is no group assigned after the ':'. The directory for the ownership change is '.', which is, again, the current directory.
Remember the step where we put in the IP address of the Pi into the browser on another computer on the same network? When you do that now, you will find that the default Apache landing page is gone, and is replaced by a setup prompt for you to install Wordpress!
Select your language and let's go.
Your database name is 'wordpress' if you followed the tutorial exactly. User name is 'root'. Database host is 'localhost' because your MariaDB is installed on the same device, your Pi, as Apache. As explained by the installation, you may give your table a prefix if you want to use the same database for multiple Wordpress blogs.
At this point, I think it's safe for you to go through the rest of the installation on your own.
After all this, your Wordpress will be up and running on your network. That's no fun, and I want to see your blog too! So next week, I will teach you how to access your Wordpress blog from the Internet. Stay tuned!
오늘도 바베의 라즈베리파이 시리즈를 읽어주셔서 감사합니다.
오늘 준비한 LAMP 웹 서버 설치 어떠셨나요?
다음주에는 워드프레스 블로그 엑세스 방법에 대해서 알아볼게요 :)
궁금한 점이나 따라하시다 힘드시면 댓글 주시면 차근차근 알려드릴게요!
그럼 다음 포스팅에서 만나요
'Raspberry Pi' 카테고리의 다른 글
The Raspberry Pi Series: Kodi and OnDemandKorea (6) | 2020.11.22 |
---|---|
The Raspberry Pi Series: Taking Your LAMP Server Online (14) | 2020.11.16 |
The Raspberry Pi series: PiVPN (2) | 2020.10.09 |
The Raspberry Pi series: Homebridge 라즈베리파이 시리즈: 홈브리지 (8) | 2020.09.30 |
The Raspberry Pi series: Securing The Pi / 라즈베리파이 셋업 시리즈: 보안강화 편 (7) | 2020.09.21 |