HOW TO INSTALL PHP 7, HOW TO INSTALL PHP (8

The tác giả selected mở cửa Sourcing Mental Illness Ltd to lớn receive a donation as part of the Write for DOnations program.

Bạn đang xem: How to install php 7, how to install php (8

Introduction

PHP is a popular vps scripting language known for creating dynamic and interactive website pages. Getting up and running with your language of choice is the first step in learning khổng lồ program.

This tutorial will guide you through installing PHP 7.4 on Ubuntu và setting up a local programming environment via the command line. You will also install a dependency manager, Composer, and test your installation by running a script.

Prerequisites

To complete this tutorial, you will need a local or virtual machine with Ubuntu 18.04 installed và have administrative access and an mạng internet connection to that machine. You can download this operating system via the Ubuntu releases page.

Step 1 — Setting Up PHP 7.4

You’ll be completing your installation and cài đặt on the command line, which is a non-graphical way khổng lồ interact with your computer. That is, instead of clicking on buttons, you’ll be typing in text và receiving feedback from your computer through text as well.

The command line, also known as a shell or terminal, can help you modify and automate many of the tasks you bởi on a computer every day and is an essential tool for software developers. There are many terminal commands lớn learn that can enable you to vì more powerful things. The article An Introduction to lớn the Linux Terminal can get you better oriented with the terminal.

On Ubuntu, you can find the Terminal application by clicking on the Ubuntu icon in the upper-left-hand corner of your screen & typing terminal into the tìm kiếm bar. Click on the Terminal application icon to mở cửa it. Alternatively, you can hit the CTRL, ALT, and T keys on your keyboard at the same time to xuất hiện the Terminal application automatically.

*

You will want to lớn avoid relying on the default version of PHP because that mặc định version could change depending on where you are running your code. You may also wish khổng lồ install a different version lớn match an application you are using or to lớn upgrade to a newer version, such as PHP 8.

Run the following command lớn update apt-get itself, which ensures that you have access to the latest versions of anything you want lớn install:

sudo apt-get updateNext, install software-properties-common, which adds management for additional software sources:

sudo apt -y install software-properties-commonThe -y flag will automatically agree to lớn the installation. Without that, you would receive a prompt in your terminal window for each installation.

Next, install the repository ppa:ondrej/php, which will give you all your versions of PHP:

sudo add-apt-repository ppa:ondrej/phpFinally, you update apt-get again so your package manager can see the newly listed packages:

sudo apt-get updateNow you’re ready khổng lồ install PHP 7.4 using the following command:

sudo apt -y install php7.4Check the version installed:

php -vYou will receive something similar khổng lồ the following:


sudo apt-get install php7.4-PACKAGE_NAMEYou can also install more than one package at a time. Here are a few suggestions of the most common modules you will most likely want khổng lồ install:

sudo apt-get install -y php7.4-cli php7.4-json php7.4-common php7.4-mysql php7.4-zip php7.4-gd php7.4-mbstring php7.4-curl php7.4-xml php7.4-bcmathThis command will install the following modules:

php7.4-cli - command interpreter, useful for testing PHP scripts from a shell or performing general shell scripting tasksphp7.4-json - for working with JSON dataphp7.4-common - documentation, examples, & common modules for PHPphp7.4-mysql - for working with MySQL databasesphp7.4-zip - for working with compressed filesphp7.4-gd - for working with imagesphp7.4-mbstring - used khổng lồ manage non-ASCII stringsphp7.4-curl - lets you make HTTP requests in PHPphp7.4-xml - for working with XML dataphp7.4-bcmath - used when working with precision floats

PHP configurations related khổng lồ Apache are stored in /etc/php/7.4/apache2/php.ini. You can menu all loaded PHP modules with the following command:

php -mYou have installed PHP và verified the version you have running. You also installed any required PHP modules and were able to các mục the modules that you have loaded.

You could start using PHP right now, but you will likely want khổng lồ use various libraries khổng lồ build PHP applications quickly. Before you kiểm tra your PHP environment, first mix up a dependency manager for your projects.

Step 2 — Setting Up Composer for Dependency Management (Optional)

Libraries are a collection of code that can help you solve common problems without needing to lớn write everything yourself. Since there are many libraries available, using a dependency manager will help you manage multiple libraries as you become more experienced in writing PHP.

Xem thêm: New Phần Mềm Pc-Nvr - Hướng Dẫn Sử Dụng Pc

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and will manage installing & updating these packages.

Although similar, Composer is not a package manager in the same sense as yum or apt. It deals with “packages” or libraries, but it manages them on a per-project basis, installing them in a directory (e.g. Vendor) inside your project. By default, it does not install anything globally. Thus, it is a dependency manager. It does, however, tư vấn a global project for convenience via the global command.

This idea is not new, and Composer is strongly inspired by Node’s npm & Ruby’s bundler.

Suppose:

You have a project that depends on several libraries.Some of those libraries depend on other libraries.

Composer:

Enables you khổng lồ declare the libraries you depend on.Finds out which versions of which packages can and need to lớn be installed & installs them by downloading them into your project.Enables you lớn update all your dependencies in one command.Enables you khổng lồ see the Basic Usage chapter for more details on declaring dependencies.

There are, in short, two ways to lớn install Composer: locally as part of your project or globally as a system-wide executable. Either way, you will start with the local install.

Locally

To quickly install Composer in the current directory, run this script in your terminal:

php -r "copy("https://getcomposer.org/installer", "composer-setup.php");"php -r "if (hash_file("sha384", "composer-setup.php") === "756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3") echo "Installer verified"; else echo "Installer corrupt"; unlink("composer-setup.php"); echo PHP_EOL;"php composer-setup.phpphp -r "unlink("composer-setup.php");"This installer script will kiểm tra some php.ini settings, warn you if they are mix incorrectly, & then download the latest composer.phar in the current directory. The four lines will, in order:

Download the installer to lớn the current directoryRun the installerRemove the installer

The installer will kiểm tra a few PHP settings and then download composer.phar lớn your working directory. This tệp tin is the Composer binary. It is a PHAR (PHP archive), which is an archive format for PHP that can be run on the command line, amongst other things.

In order khổng lồ run Composer, you use php composer.phar. As an example, run this command khổng lồ see the version of Composer you have installed:

php composer.phar --versionTo use Composer locally, you will want your composer.phar tệp tin to be in your project’s root directory. You can start in your project directory before installing Composer. You can also move the tệp tin after installation. You can also install Composer to lớn a specific directory by using the --install-dir option và additionally (re)name it using the --filename option.

Since Composer is something used across projects, it’s recommended that you continue to lớn the next portion và set Composer to run globally.

Globally

You can place the Composer PHAR anywhere you wish. If you put it in a directory that is part of your $PATH, you can access it globally. You can even make it executable on Ubuntu (and other Unix systems) & invoke it without directly using the PHP interpreter.

After installing locally, run this command to lớn move composer.phar lớn a directory that is in your path:

sudo mv composer.phar /usr/local/bin/composerIf you’d lượt thích to install it only for your user và avoid requiring root permissions, you can use ~/.local/bin instead, which is available by mặc định on some Linux distributions:

mv composer.phar ~/.local/bin/composerNow to lớn run Composer, use composer instead of php composer.phar. To check for your Composer version, run:

composer --versionAs a final step, you may optionally initialize your project with composer init. This will create the composer.json tệp tin that will manage your project dependencies. Initializing the project will also let you define project details such as Author và License, and use Composer’s autoload functionality. You can define dependencies now or địa chỉ cửa hàng them later.

Run this command to initialize a project:

composer initRunning this command will start the thiết lập wizard. Details that you enter in the wizard can be updated later, so feel free to leave the defaults và just press ENTER. If you aren’t ready to install any dependencies, you can choose no. Enter in your details at each prompt: