How To Install Php Server
Introduction
A "LAMP" stack is a group of open-source software that is typically installed together to enable a server to host dynamic websites and spider web apps. This term is actually an acronym which represents the Linux operating arrangement, with the Apache web server. The site information is stored in a MySQL database, and dynamic content is processed by PHP.
In this guide, we'll install a LAMP stack on an Ubuntu eighteen.04 server.
Prerequisites
In lodge to complete this tutorial, you'll need to have an Ubuntu 18.04 server with a non-root sudo
-enabled user account and a basic firewall configured. To set this up, you lot tin follow our initial server setup guide for Ubuntu 18.04.
Stride 1 — Installing Apache and Updating the Firewall
The Apache web server is a popular open up source spider web server that tin can be used along with PHP to host dynamic websites. Information technology'southward well-documented and has been in wide employ for much of the history of the spider web.
First, make certain your apt
enshroud is updated with:
- sudo apt update
If this is your first time using sudo
in this session, you'll be prompted to provide your regular user's password to validate your permissions.
Once the cache has been updated, you can install Apache with:
- sudo apt install apache2
After entering this command, apt
volition tell you which packages it plans to install and how much actress deejay space they'll take upwardly. Press Y
and hit ENTER
to confirm, and the installation will proceed.
Adjust the Firewall to Let Web Traffic
Next, bold that you have followed the initial server setup instructions and enabled the UFW firewall, make sure that your firewall allows HTTP and HTTPS traffic. You can check that UFW has an application profile for Apache like so:
- sudo ufw app list
Output
Available applications: Apache Apache Total Apache Secure OpenSSH
If you lot look at the Apache Full
profile details, y'all'll come across that it enables traffic to ports fourscore
and 443
:
- sudo ufw app info "Apache Full"
Output
Contour: Apache Full Title: Web Server (HTTP,HTTPS) Description: Apache v2 is the adjacent generation of the omnipresent Apache web server. Ports: eighty,443/tcp
To let incoming HTTP and HTTPS traffic for this server, run:
- sudo ufw allow "Apache Full"
You lot tin can practice a spot cheque right away to verify that everything went as planned by visiting your server's public IP address in your spider web browser (run into the note under the next heading to find out what your public IP address is if you practise non have this information already):
http://your_server_ip
You will run into the default Ubuntu 18.04 Apache web page, which is there for informational and testing purposes. Information technology should wait something like this:
If you see this page, so your spider web server is now correctly installed and accessible through your firewall.
How To Discover your Server'southward Public IP Address
If you do non know what your server's public IP address is, there are a number of ways you can find it. Usually, this is the address you use to connect to your server through SSH.
There are a few unlike ways to do this from the command line. First, you could use the iproute2
tools to get your IP address past typing this:
- ip addr testify eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
This will requite you lot 2 or 3 lines dorsum. They are all right addresses, simply your reckoner may merely be able to use ane of them, so feel gratuitous to endeavor each one.
An culling method is to employ the curl
utility to contact an outside political party to tell you lot how it sees your server. This is done by asking a specific server what your IP address is:
- sudo apt install curl
- coil http://icanhazip.com
Regardless of the method you use to get your IP address, blazon information technology into your web browser's accost bar to view the default Apache folio.
Step 2 — Installing MySQL
At present that you have your spider web server upwardly and running, it is time to install MySQL. MySQL is a database management system. Basically, it will organize and provide access to databases where your site can store information.
Over again, use apt
to larn and install this software:
- sudo apt install mysql-server
Annotation: In this example, y'all practise not have to run sudo apt update
prior to the command. This is because you recently ran it in the commands above to install Apache. The packet index on your computer should already exist up-to-engagement.
This command, too, will prove y'all a list of the packages that volition be installed, along with the amount of deejay space they'll accept up. Enter Y
to continue.
When the installation is complete, run a simple security script that comes pre-installed with MySQL which will remove some dangerous defaults and lock down access to your database system. Start the interactive script by running:
- sudo mysql_secure_installation
This will inquire if you lot want to configure the VALIDATE Password PLUGIN
.
Notation: Enabling this feature is something of a judgment telephone call. If enabled, passwords which don't match the specified criteria will be rejected by MySQL with an mistake. This will crusade issues if you use a weak countersign in conjunction with software which automatically configures MySQL user credentials, such every bit the Ubuntu packages for phpMyAdmin. Information technology is safe to get out validation disabled, but you should ever use strong, unique passwords for database credentials.
Answer Y
for aye, or annihilation else to continue without enabling.
VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to gear up simply those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin? Press y|Y for Yes, any other key for No:
If you respond "yes", you'll be asked to select a level of password validation. Keep in listen that if y'all enter 2
for the strongest level, y'all will receive errors when attempting to set any password which does non contain numbers, upper and lowercase letters, and special characters, or which is based on common lexicon words.
There are 3 levels of password validation policy: Low Length >= 8 MEDIUM Length >= viii, numeric, mixed case, and special characters Stiff Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = Depression, one = MEDIUM and 2 = STRONG: 1
Regardless of whether you chose to ready up the VALIDATE PASSWORD PLUGIN
, your server will next ask you lot to select and ostend a password for the MySQL root user. This is not to be confused with the organization root. The database root user is an authoritative user with full privileges over the database organization. Fifty-fifty though the default authentication method for the MySQL root user dispenses the employ of a password, even when one is set, y'all should define a strong password here as an additional prophylactic measure out. We'll talk near this in a moment.
If you've enabled password validation, you'll be shown the password strength for the root countersign you only entered and your server will ask if y'all want to change that countersign. If yous are happy with your electric current countersign, enter N
for "no" at the prompt:
Using existing password for root. Estimated strength of the password: 100 Modify the password for root ? ((Press y|Y for Yes, any other cardinal for No) : n
For the rest of the questions, press Y
and hit the ENTER
key at each prompt. This volition remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MySQL immediately respects the changes you have fabricated.
When you're finished, test if you lot're able to log in to the MySQL console past typing:
- sudo mysql
This will connect to the MySQL server as the administrative database user root, which is inferred by the utilise of sudo
when running this command. You should see output like this:
Output
Welcome to the MySQL monitor. Commands stop with ; or \g. Your MySQL connectedness id is 5 Server version: five.7.34-0ubuntu0.eighteen.04.one (Ubuntu) Copyright (c) 2000, 2022, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
To exit the MySQL console, type:
- exit
Detect that you didn't need to provide a password to connect equally the root user, even though you have defined 1 when running the mysql_secure_installation
script. That is because the default authentication method for the authoritative MySQL user is unix_socket
instead of password
. Even though this might look like a security business organization at first, it makes the database server more secure because the simply users allowed to log in as the root MySQL user are the system users with sudo privileges connecting from the console or through an application running with the aforementioned privileges. In practical terms, that means you won't exist able to use the authoritative database root user to connect from your PHP application. Setting a password for the root MySQL account works every bit a safeguard, in case the default hallmark method is inverse from unix_socket
to countersign
.
For increased security, it'south best to have dedicated user accounts with less expansive privileges gear up for every database, especially if you programme on having multiple databases hosted on your server. Please refer to our guide on How To Create a New User and Grant Permissions on MySQL for detailed instructions on how to create MySQL users and configure database admission rights.
Your MySQL server is now installed and secured. Next, we'll install PHP, the last component in the LAMP stack.
Step 3 — Installing PHP
PHP is the component of your setup that volition process code to brandish dynamic content. It can run scripts, connect to your MySQL databases to become information, and mitt the processed content over to your web server and then that information technology can display the results to your visitors.
Once again, leverage the apt
system to install PHP. In addition to the php
package, you lot'll also need libapache2-mod-php
to integrate PHP into Apache, and the php-mysql
parcel to let PHP to connect to MySQL databases. Run the following command to install all iii packages and their dependencies:
- sudo apt install php libapache2-modernistic-php php-mysql
This should install PHP without any bug. We'll test this in a moment.
Irresolute Apache'due south Directory Index (Optional)
In some cases, you'll want to modify the way that Apache serves files when a directory is requested. Currently, if a user requests a directory from the server, Apache will first look for a file called index.html
. Nosotros want to tell the web server to prefer PHP files over others, to make Apache look for an alphabetize.php
file first. If you don't do that, an index.html
file placed in the document root of the application will always take precedence over an index.php
file.
To make this change, open up the dir.conf
configuration file in a text editor of your choice. Here, we'll apply nano
:
- sudo nano /etc/apache2/mods-enabled/dir.conf
It will look similar this:
/etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c> DirectoryIndex alphabetize.html index.cgi index.pl index.php index.xhtml index.htm </IfModule>
Move the PHP index file (highlighted above) to the beginning position after the DirectoryIndex
specification, like this:
/etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c> DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm </IfModule>
When you are finished, salvage and close the file by pressing CTRL+X
. Confirm the salvage by typing Y
and and then hit ENTER
to verify the file salvage location.
Afterwards this, restart the Apache web server in order for your changes to exist recognized. You tin can do that with the following control:
- sudo systemctl restart apache2
You can too bank check on the condition of the apache2
service using systemctl
:
- sudo systemctl status apache2
Sample Output
● apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Drop-In: /lib/systemd/system/apache2.service.d └─apache2-systemd.conf Active: active (running) since Thu 2022-07-15 09:22:59 UTC; 1h 3min agone Chief PID: 3719 (apache2) Tasks: 55 (limit: 2361) CGroup: /organization.slice/apache2.service ├─3719 /usr/sbin/apache2 -yard showtime ├─3721 /usr/sbin/apache2 -k start └─3722 /usr/sbin/apache2 -m start Jul xv 09:22:59 ubuntu1804 systemd[i]: Starting The Apache HTTP Server... Jul xv 09:22:59 ubuntu1804 apachectl[3694]: AH00558: apache2: Could not reliably determine the server'south fully qualified domain name, using 127.0.1.i. Gear up the 'ServerName' di Jul xv 09:22:59 ubuntu1804 systemd[1]: Started The Apache HTTP Server.
Press Q
to leave this status output.
Installing PHP Extensions (Optional)
To extend the functionality of PHP, you have the option to install some boosted modules. To encounter the available options for PHP modules and libraries, pipage the results of apt search
into less
, a pager which lets you ringlet through the output of other commands:
- apt search php- | less
Apply the arrow keys to curlicue up and down, and press Q
to quit.
The results are all optional components that you tin can install. Information technology volition give you a short description for each:
bandwidthd-pgsql/bionic 2.0.1+cvs20090917-10ubuntu1 amd64 Tracks usage of TCP/IP and builds html files with graphs bluefish/bionic ii.2.ten-1 amd64 advanced Gtk+ text editor for web and software development cacti/bionic 1.ane.38+ds1-1 all web interface for graphing of monitoring systems ganglia-webfrontend/bionic iii.6.i-three all cluster monitoring toolkit - web front-stop golang-github-unknwon-cae-dev/bionic 0.0~git20160715.0.c6aac99-4 all PHP-like Compression and Archive Extensions in Go haserl/bionic 0.9.35-2 amd64 CGI scripting program for embedded environments kdevelop-php-docs/bionic 5.2.i-1ubuntu2 all transitional package for kdevelop-php kdevelop-php-docs-l10n/bionic v.2.i-1ubuntu2 all transitional bundle for kdevelop-php-l10n … :
To learn more about what each module does, you could search the internet for more information almost them. Alternatively, wait at the long clarification of the package by typing:
- apt bear witness package_name
There will be a lot of output, with ane field called Description
which will have a longer explanation of the functionality that the module provides.
For example, to discover out what the php-cli
module does, you could type this:
- apt evidence php-cli
Along with a large amount of other data, you'll find something that looks like this:
Output
… Description: control-line interpreter for the PHP scripting language (default) This packet provides the /usr/bin/php control interpreter, useful for testing PHP scripts from a beat out or performing general shell scripting tasks. . PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source full general-purpose scripting language that is especially suited for web development and can be embedded into HTML. . This parcel is a dependency package, which depends on Ubuntu's default PHP version (currently vii.2). …
If, later researching, you lot decide you lot would like to install a bundle, you can do so by using the apt install
command similar you lot have been doing for the other software.
If you decided that php-cli
is something that you need, you could type:
- sudo apt install php-cli
If you desire to install more 1 module, you can practice that by listing each one, separated by a space, post-obit the apt install
command, similar this:
- sudo apt install package1 package2 ...
At this point, your LAMP stack is installed and configured. Earlier y'all do annihilation else, we recommend that you prepare an Apache virtual host where y'all tin can store your server's configuration details.
Step 4 — Setting Up a Virtual Host (Recommended)
When using the Apache web server, yous can use virtual hosts (similar to server blocks in Nginx) to encapsulate configuration details and host more than i domain from a single server. Nosotros will gear up a domain called your_domain, but yous should replace this with your own domain proper name. To learn more about setting up a domain name with DigitalOcean, come across our Introduction to DigitalOcean DNS.
Apache on Ubuntu 18.04 has one server block enabled by default that is configured to serve documents from the /var/www/html
directory. While this works well for a single site, information technology can go unwieldy if you are hosting multiple sites. Instead of modifying /var/www/html
, let's create a directory structure within /var/world wide web
for your_domain site, leaving /var/www/html
in identify as the default directory to be served if a customer asking doesn't match whatsoever other sites.
Create the directory for your_domain as follows:
- sudo mkdir /var/world wide web/your_domain
Next, assign ownership of the directory with the $USER
environment variable, which references the electric current logged user:
- sudo chown -R $USER : $USER /var/world wide web/your_domain
The permissions of your spider web root directory should be correct if you lot oasis't modified its umask value, just you lot tin can make sure past typing:
- sudo chmod -R 755 /var/www/your_domain
Side by side, create a sample index.html
page using nano
or your favorite editor:
- nano /var/www/your_domain/alphabetize.html
Inside, add the following sample HTML:
/var/www/your_domain/index.html
<html> <caput> <title>Welcome to Your_domain!</championship> </head> <trunk> <h1>Success! The your_domain server block is working!</h1> </body> </html>
Relieve and close the file when you are finished.
In order for Apache to serve this content, it's necessary to create a virtual host file with the right directives. Instead of modifying the default configuration file located at /etc/apache2/sites-bachelor/000-default.conf
direct, let'southward make a new one at /etc/apache2/sites-bachelor/your_domain.conf
:
- sudo nano /etc/apache2/sites-available/your_domain.conf
Paste in the following configuration cake, which is similar to the default, merely updated for our new directory and domain name:
/etc/apache2/sites-available/your_domain.conf
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName your_domain ServerAlias world wide web.your_domain DocumentRoot /var/world wide web/your_domain ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Observe that nosotros've updated the DocumentRoot
to our new directory and ServerAdmin
to an email that the your_domain site administrator can access. We've also added two directives: ServerName
, which establishes the base domain that should friction match for this virtual host definition, and ServerAlias
, which defines farther names that should match as if they were the base of operations name.
Salvage and close the file when you are finished.
Let'southward enable the file with the a2ensite
tool:
- sudo a2ensite your_domain.conf
Disable the default site defined in 000-default.conf
:
- sudo a2dissite 000-default.conf
Side by side, let'due south test for configuration errors:
- sudo apache2ctl configtest
You lot should see the following output:
Output
Syntax OK
Restart Apache to implement your changes:
- sudo systemctl restart apache2
Apache should at present be serving your domain proper name. Y'all can test this past navigating to http://your_domain
, where you should run across something like this:
With that, your virtual host is fully gear up. Before making any more changes or deploying an application, though, information technology would be helpful to proactively test out your PHP configuration in example there are any issues that should be addressed.
Step 5 — Testing PHP Processing on your Spider web Server
In guild to test that your arrangement is properly configured for PHP, create a PHP script called info.php
. In guild for Apache to observe this file and serve it correctly, it must be saved to your web root directory.
Create the file at the web root yous created in the previous pace by running:
- sudo nano /var/www/your_domain/info.php
This will open a blank file. Add the following text, which is valid PHP code, inside the file:
/var/www/your_domain/info.php
<?php phpinfo();
When y'all are finished, save and shut the file.
Now you can exam whether your web server is able to correctly display content generated by this PHP script. To try this out, visit this page in your web browser. You'll demand your server'south public IP accost or domain name again.
The address you volition desire to visit is:
http://your_domain/info.php
The folio that you come to should expect something like this:
This folio provides some basic information about your server from the perspective of PHP. It is useful for debugging and to ensure that your settings are existence applied correctly.
If yous can see this page in your browser, then your PHP is working equally expected.
Y'all probably want to remove this file after this examination considering information technology could actually give information almost your server to unauthorized users. To do this, run the following control:
- sudo rm /var/www/your_domain/info.php
You can ever recreate this page if y'all need to access the information once more afterward.
Decision
Now that you have a LAMP stack installed, yous have many choices for what to do adjacent. You've installed a platform that will allow you to install almost kinds of websites and web software on your server.
As an firsthand next pace, you should ensure that connections to your web server are secured, by serving them via HTTPS. Follow our guide on how to secure Apache with Let'southward Encrypt to secure your site with a free TLS/SSL certificate.
Another popular options are:
- Install Wordpress the most pop content management system on the internet.
- Fix PHPMyAdmin to aid manage your MySQL databases from a web browser.
- Learn how to use SFTP to transfer files to and from your server.
Source: https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-ubuntu-18-04
Posted by: archuletainuitch.blogspot.com
0 Response to "How To Install Php Server"
Post a Comment