tutorial-cloud/blog/2019-05-29-blog-9.md
2024-10-25 10:05:08 +05:30

235 lines
15 KiB
Markdown

---
slug: Setting Up a Web Server with Linux Apache MySQL PHP
title: Setting Up a Web Server with Linux, Apache, MySQL, PHP (LAMP) on Ubuntu 14.04
authors: [slorber, yangshun]
tags: [hola, docusaurus]
---
<!-- truncate -->
import CodeBlock from '@site/src/components/CodeBloack';
import ubuntu from '@site/static/img/small_apache_default.png';
<div className="head">Introduction</div><br/>
<div className="text">A "LAMP" stack is like a set of tools for building websites and web apps. It's made up of different free software that work together on a computer server. Let's look at what each part does:</div>
<div className="text"><ol><li>Operating System: This is like the main software that runs everything on the computer. It's the foundation of the stack.</li><li>Web Server (Apache): Apache acts like a traffic controller for the web. It takes requests from people's browsers and sends back the right web pages.</li><li>Database (MySQL): This is where all the website's data is stored, like usernames, passwords, and other information. It's like a digital filing cabinet.</li><li>Server-side Language (PHP): PHP is a language that helps create dynamic web pages. It's what makes things like logins, forms, and shopping carts work smoothly.</li></ol></div>
<div className="text">Now, let's learn how to set up this LAMP stack on a computer running Ubuntu 14.04. Ubuntu is just another type of operating system and it's perfect for this task.</div><br/>
<div className="note"><strong>Note</strong>: Installing a LAMP stack on your Droplet can be made super easy! Just include this special script when you're setting up your Droplet. It's like giving your computer a set of instructions to follow right from the start. If you're not sure how to do this, check out the tutorial on Droplet User Data. It'll walk you through the process step by step.</div> <br/>
<div className="head">Getting Started</div>
<div className="text">Before you start following this guide, make sure you have a special user account set up on your server. This account shouldn't be the main "root" one, but it should have similar privileges. If you're not sure how to set up this user account, you can check out steps 1-4 in the beginner's guide for setting up your server with Ubuntu 14.04. It'll walk you through the process in easy-to-follow steps.</div><br/>
<div className="head">Let's Get Your Website Running: Installing Apache</div>
<div className="text">Apache is like the engine that powers many websites on the internet. It's super popular and reliable, making it a great choice for hosting your site.To get Apache, we'll use something called Ubuntu's "package manager." It's like an app store for your server!</div><br/>
<div className="text">Here's how we do it: We just tell the package manager what we want, and it goes and gets it for us. Easy, right? If you want to learn more about how it works, you can check out this guide on using the package manager.</div>
<CodeBlock code={`sudo apt-get update
sudo apt-get install apache2
`} /><br/>
<div className="text">When we use a command with "sudo," it means we're doing something important with extra powers. It might ask for your regular password just to make sure you're sure about what you're doing.</div><br/>
<div className="text">Once we're done with that, our web server is good to go!</div><br/>
<div className="text">To make sure everything worked like it should, you can quickly check it by going to your server's public IP address in your web browser. If you're not sure what that is, don't worry, we'll show you how to find it in the next part.</div>
<CodeBlock code={`<pre> http://<span class=“highlight”>your_server_IP_address</span> </pre>`} /><br/>
<div className="text">You'll be greeted with a special webpage made by Apache on Ubuntu 14.04. It's there to give you some basic info and to help you check if everything is working fine. It might look a bit like this:</div>
<img src={ubuntu} alt="small_apache_default" />
<div className="text">How to See Your Server's Public IP Address:
If you're not sure what your server's public IP address is, there are a few simple ways to find it. This is the address you use to connect to your server using SSH.</div> <br/>
<div className="text">One way to find it is by using commands on the command line. You can do this by typing a specific command, like this:</div>
<CodeBlock code={`ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'`} /><br/>
<div className="text">After running the command, you'll see one or two lines with numbers on them. Both lines show the correct addresses, but your computer might only be able to use one of them. So, you can try using each one to see which works.</div><br/>
<div className="text">Another way is to ask a different computer outside of your network to tell you what it sees as your server's IP address. You can do this by reaching out to a specific server and asking it for the information.</div>
<CodeBlock code={`curl http://icanhazip.com`} /><br/>
<div className="text">No matter how you find your IP address, you can simply type it into your web browser's address bar to reach your server.</div>
<div className="head">Setting Up MySQL Database Management System</div>
<div className="text">Let's set up MySQL now that our web server is running. MySQL is like a storage system for our website. It helps organize and manage databases where our site can keep information.</div><br/>
<div className="text">We'll use a tool called "apt" to get and install MySQL, just like before. This time, we'll also install some extra packages that help different parts of our setup talk to each other:</div>
<CodeBlock code={`sudo apt-get install mysql-server php5-mysql`} /><br/>
<div className="note"><strong>Note</strong>: You don't need to run "sudo apt-get update" again before installing MySQL because we already did that when installing Apache. Your computer already has the latest information about available packages.</div> <br/>
<div className="text">While installing MySQL, your server will ask you to pick and confirm a password for the MySQL "root" user. This is like an administrator account in MySQL, with special powers. It's similar to the main account for the server itself, but just for MySQL.</div><br/>
<div className="text">After MySQL is installed, we have to do a few more things to make sure it's set up safely.<br/><br/>
First, we need to tell MySQL to create the place where it will keep all its information. You can do this by typing:</div>
<CodeBlock code={`sudo mysql_install_db`} /><br/>
<div className="text">After that, we'll run a simple security script to make sure our database system is safer. To begin this script, just run:</div>
<CodeBlock code={`sudo mysql_secure_installation`} /><br/>
<div className="text">You'll be prompted to enter the password you set for the MySQL root account. Then, it will ask if you want to change that password. If you're happy with it, just type "n" for "no".</div><br/>
<div className="text">For the other questions, just press "ENTER" to accept the default values. This will remove some sample users and databases, disable remote root logins, and apply these new rules immediately to MySQL.<br/>
Now your database system is all set up and we can move forward.</div>
<div className="head">Installing PHP for Dynamic Website Content</div>
<div className="text">Let's install PHP, which is a crucial part of our setup. PHP is responsible for processing code to show dynamic content on our website. It can run scripts, communicate with our MySQL databases to fetch information, and then deliver the processed content to our web server for display.</div><br/>
<div className="text">We'll use the apt system again to install PHP along with some additional helper packages.</div>
<CodeBlock code={`sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt`} /><br/>
<div className="text">Installing PHP should be smooth without encountering any issues. We'll verify this shortly.</div><br/>
<div className="text">Usually, we'll want to adjust how Apache handles files when someone requests a directory from our server. Right now, if a user asks for a directory, Apache checks if there's a file called "index.html" inside. However, we prefer to use PHP files, so we'll tell Apache to look for an "index.php" file first.</div><br/>
<div className="text">To make this change, follow these steps - Open the "dir.conf" file in a text editor with special permissions. You can do this by typing:</div>
<CodeBlock code={`sudo nano /etc/apache2/mods-enabled/dir.conf`} /><br/>
<div className="text">Here's what the file will look like:</div>
<CodeBlock code={`<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl <span class=“highlight”>index.php</span> index.xhtml index.htm
</IfModule>
`} /><br/>
<div className="text">We want to move the highlighted PHP index file to the first position after the DirectoryIndex specification, like this:</div>
<CodeBlock code={`<IfModule mod_dir.c>
DirectoryIndex <span class=“highlight”>index.php</span> index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
`} /><br/>
<div className="text">Once you've made this change, save and close the file by pressing "CTRL-X". Confirm the save by typing "Y" and then hit "ENTER" to confirm the file save location.</div><br/>
<div className="text">After that, we need to restart the Apache web server to make our changes take effect. You can do this by typing:</div>
<CodeBlock code={`sudo service apache2 restart`} /><br/>
<div className="text" style={{ fontSize:'28px' }}>Enhancing PHP Functionality with Additional Modules</div>
<div clssName="text">We can make PHP even more powerful by adding extra modules. These modules provide additional features and tools that we can use in our PHP code.</div><br/>
<div className="text">To check out the available options for PHP modules and libraries, you can do this:</div>
<CodeBlock code={`apt-cache search php5-`} /><br/>
<div className="text">The list shows extra parts you can add if you want. Each one comes with a quick explanation:</div>
<CodeBlock code={`php5-cgi - server-side, HTML-embedded scripting language (CGI binary)
php5-cli - command-line interpreter for the php5 scripting language
php5-common - Common files for packages built from the php5 source
php5-curl - CURL module for php5
php5-dbg - Debug symbols for PHP5
php5-dev - Files for PHP5 module development
php5-gd - GD module for php5
. . .
`} /><br/>
<div className="text">To learn more about what each module does, you have two options:</div>
<div className="text"><ol><li>Search online.</li><li>Check the detailed description in the package itself. You can do this by typing:</li></ol></div>
<CodeBlock code={`apt-cache show <span class=“highlight”>package_name</span>`} /><br/>
<div className="text">This will show a lot of information, but focus on the "Description-en" field, which gives a longer explanation of what the module does.</div><br/>
<div className="text">For instance, to find out about the php5-cli module, you can type:</div>
<CodeBlock code={`apt-cache show php5-cli`} /><br/>
<div className="text">In addition to many other details, you'll see something like this:</div>
<CodeBlock code={`. . .
SHA256: 91cfdbda65df65c9a4a5bd3478d6e7d3e92c53efcddf3436bbe9bbe27eca409d
Description-en: command-line interpreter for the php5 scripting language
This package provides the /usr/bin/php5 command interpreter, useful for
testing PHP scripts from a shell or performing general shell scripting tasks.
.
The following extensions are built in: bcmath bz2 calendar Core ctype date
dba dom ereg exif fileinfo filter ftp gettext hash iconv libxml mbstring
mhash openssl pcntl pcre Phar posix Reflection session shmop SimpleXML soap
sockets SPL standard sysvmsg sysvsem sysvshm tokenizer wddx xml xmlreader
xmlwriter zip zlib.
.
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used
open source general-purpose scripting language that is especially suited
for web development and can be embedded into HTML.
Description-md5: f8450d3b28653dcf1a4615f3b1d4e347
Homepage: http://www.php.net/
. . .
`} /><br/>
<div className="text">If, after checking, you decide you want to add a package, you can use the "apt-get install" command just like we did for our other software.</div><br/>
<div className="text">For example, if you've decided you need "php5-cli", you would type:</div>
<CodeBlock code={`sudo apt-get install php5-cli`} /><br/>
<div className="text">If you want to install multiple modules, you can do that by listing each one, separated by a space, after the "apt-get install" command, like this:</div>
<CodeBlock code={`sudo apt-get install <span class=“highlight”>package1</span> <span class=“highlight”>package2</span> <span class=“highlight”></span>`} /><br/>
<div className="text">Now, your LAMP stack is all set up and ready to go. However, we should still check if PHP is working correctly.</div>
<div className="head">Checking PHP Functionality on Your Web Server</div>
<div className="text">To make sure our system is set up correctly for PHP, let's create a simple PHP script.<br/><br/>
We'll name this script "info.php". For Apache to find and display the file properly, it needs to be saved in a specific folder called the "web root".<br/><br/>
In Ubuntu 14.04, this folder is located at "/var/www/html/". You can create the file in that location by typing:</div>
<CodeBlock code={`sudo nano /var/www/html/info.php`} /><br/>
<div className="text">This will open an empty file. Now, we'll add some simple PHP code inside it. Just copy and paste the following text into the file:</div>
<CodeBlock code={`<?php
phpinfo();
?>
`} /><br/>
<div className="text">Once you're done, save and close the file.</div><br/>
<div className="text">Now, let's check if our web server can show content generated by a PHP script. To do this, simply open your web browser and visit the following page. You'll need your server's public IP address again.</div><br/>
<div className="text">The address you want to visit is:</div>
<CodeBlock code={`http://<span class=“highlight”>your_server_IP_address</span>/info.php`} /><br/>
<div className="text">When you go to this page, you should see something similar to this:</div>
<!-- <needs to add image here> -->
<div className="text">This page shows you server information from PHP's perspective, useful for checking your setup. If everything looks good, your PHP is working as it should.</div> <br/>
<div className="text">If everything went well and you see the expected information, it means your PHP is working correctly.</div>
<div className="text">However, you should consider removing this file after the test because it could potentially expose information about your server to unauthorized users. To delete it, you can do the following:</div>
<CodeBlock code={`sudo rm /var/www/html/info.php`} /><br/>
<div className="text">If you ever need to check that information again, you can always recreate this page.</div><br/>
<div className="con">Conclusion:</div>
<div className="text">Now that you've set up your LAMP stack, you're ready to do a lot of cool stuff! Basically, you've got everything you need to create different types of websites and web software on your server.There are so many possibilities! Just choose what interests you the most and start exploring.</div>