国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Key Takeaways
Booting the VM
Prepping the Environment
Ubuntu
Fedora
RHEL
Suse
Installing Extensions
Installing a bundled extension
Installing a third party extension
Enabling and Testing
Removing Extensions
Conclusion
Frequently Asked Questions (FAQs) about Installing PHP Extensions from Source
What are the prerequisites for installing PHP extensions from source?
How can I verify if a PHP extension is installed correctly?
Can I install PHP extensions on a Windows system?
What should I do if I encounter errors during the installation process?
How can I update a PHP extension?
Can I install multiple PHP extensions at once?
How can I uninstall a PHP extension?
What is the PECL repository?
Can I install PHP extensions without root access?
What are some common PHP extensions and what do they do?
Home Backend Development PHP Tutorial How to Install PHP Extensions from Source

How to Install PHP Extensions from Source

Feb 20, 2025 am 09:38 AM

How to Install PHP Extensions from Source

Sometimes it’s hard to know which PHP extensions you’ll need before you install PHP. In cases where you need to add extensions later on, you might get lucky and the extension could be in the repository of the OS you’re using. It might just be a simple sudo apt-get install php5-intl away. In other cases, however, you might need to install it from source – Phalcon is one such case, but it makes the procedure extremely simple by introducing vendor support, shortcuts and pre-written instructions for your OS to carry out. What if there’s no such thing for other extensions, though?

In this tutorial, we’ll go through installing some custom extensions on Linux systems (and OS X – the process is nearly identical). The procedure is very similar to what we already did on Nitrous.io, but adapted for local environments – more specifically, Laravel Homestead. You can easily derive installation instructions from this tutorial and apply them to other distros.

Key Takeaways

  • Installing PHP extensions from source requires PHP development tools and a compiler installed on your machine, as well as the PHP source code and the extension’s source code.
  • The process of installing PHP extensions from source involves several steps: downloading the source code, preparing the extension’s folder for compilation with phpize, configuring the environment for compilation, compiling the sources into a .so file, and moving this file into the current PHP installation’s extensions folder.
  • After installation, the PHP extensions need to be enabled by adding them to the php.ini file or creating a separate ini file for each extension in a folder that gets auto-included after php.ini is loaded.
  • To remove extensions, you can use the php5dismod tool, remove the symlinks manually, or remove the enabling lines from the php.ini files.

Booting the VM

If you haven’t already, read the Homestead post linked above and get it up and running. Immediately after running a new Homestead box, you should be able to do this:

How to Install PHP Extensions from Source

That’s perfectly fine, this happens because the folder that’s mounted by default actually doesn’t contain any files yet. Now vagrant ssh into the VM, and execute the following commands:

<span>cd Code
</span><span>git clone https://github.com/Swader/publicinfo</span>

This creates a valid PHP Info file in the path that Homestead is set to by default. Refreshing the URL will now produce a PHPInfo screen:

How to Install PHP Extensions from Source

Prepping the Environment

To build extensions from source, we need the PHP dev tools installed on our machine, as well as a compiler that can produce the extension file. Here’s how you install these prerequisites on various operating systems:

Ubuntu

<span>cd Code
</span><span>git clone https://github.com/Swader/publicinfo</span>

Fedora

<span>sudo apt-get install php5-dev php5-mysql gcc libpcre3-dev</span>

RHEL

<span>sudo yum install php-devel php-mysqlnd gcc libtool</span>

Suse

<span>sudo yum install php-devel php-mysql gcc libtool</span>

If you’re using the most recent Homestead, all these tools will already be installed for you. With everything prepared, let’s start installing extensions.

Installing Extensions

There are two types of extensions you can install: bundled with PHP but not installed by default, and third party extensions. Third party extensions like Phalcon usually make the installation process much easier by providing shortcuts, as they don’t have to comply to certain traditions bundled PHP extensions are bound by.

First, let’s move into the home folder on the vm: cd ~. In there, make a downloads folder, and cd into it. When installing a bundled extension, you’ll need the source code of PHP on your machine, preferably one matching your current version. The version Homestead uses is 5.5.12, so I’ll be downloading that one:

<span>yast2 -i php5-pear php5-devel php5-mysql gcc</span>

I’m using the Belgian mirror above, feel free to use that one or any other from the download archives.

To see the sources of all bundled extensions, go into the ext folder inside the unarchived PHP source code folder and do a list with ls.

How to Install PHP Extensions from Source

Installing a bundled extension

First, we’ll install the PHP-intl extension if you don’t have it installed already. If you do, that’s fine – the installation procedure you’ll see below is identical for every bundled PHP extension. The intl extension is for internationalization – read more here if you’re interested.

Seeing as the intl extension needs the ICU library as a prerequisite (as stated in the requirements), let’s install that first.

<span>wget http://be2.php.net/distributions/php-5.5.12.tar.bz2
</span><span>tar xvjf php-5.5.12.tar.bz2
</span><span>cd php-5.5.12</span>

Under other distributions, the installation instructions may vary. It’s best if you refer to the ICU site or your individual distribution’s docs for this step.

Once ICU is installed, do the following while still in the ext folder:

<span>sudo apt-get install icu-devtools icu-doc libicu-dev libicu52 libicu52-dbg</span>

Let’s explain what’s going on.

  • phpize prepares the extension’s folder for compliation. It allows you to do the subsequent commands by creating a configure file, and basically making the extension’s folder “think” it’s PHP itself. The procedure after phpize is, in fact, identical to what you would do when installing PHP from source – only in this case, just a fragment of PHP is compiled and prepared for use with the already compiled and installed PHP.
  • ./configure --enable-intl configures the environment for compilation. It prepares everything the compiler will need to craft the intl.so file which we’ll be using. The enable-intl flag is necessary even though we’re in the intl folder because the folder, effectively, thinks it is PHP, and we need to help it live that illusion. This command tells it: “Ok, you’re PHP’s source code. Now compile and install with the intl extension.”, when in fact, it’s the only part that can be installed from this folder.
  • make will compile the sources into intl.so, placing the file into the very folder you’re currently in, under the modules subfolder.
  • sudo make install will move this file into the current PHP installation’s extensions folder.

All we need to do now is enable the extension by having php.ini consume it. We’ll do that later, let’s compile a third party extension first.

Installing a third party extension

We’ll be installing Mongo as a third party extension. There are binary distributions available for Mongo which make installation simpler, but let’s do it manually for the sake of education. We’ll assume you already have the actual Mongo installed, and are thus focusing only on the PHP extension. If you don’t have Mongo installed, refer to their installation docs.

<span>cd Code
</span><span>git clone https://github.com/Swader/publicinfo</span>

How to Install PHP Extensions from Source

This has built our mongo.so file, and placed it into the extensions folder of our PHP installation. We’ll enable it in the next section.

Enabling and Testing

To see if the compiled .so files are indeed in our PHP extensions folder, list out its contents:

How to Install PHP Extensions from Source

As you can see, there they are, highlighted in bright green.

To enable them, we need to tell php.ini about them. There are several ways to do this:

  1. You can put the lines extension=mongo.so and extension=intl.so directly into php.ini. This works, and is a perfectly valid approach in most cases.
  2. You can create a separate ini file for each of these, put them in a folder that gets auto-included after php.ini is loaded, and maintain separation and isolation of php.ini that way. This approach is healthier, though harder to accomplish.
  3. You can combine 2) and the default tools for enabling PHP mods. Homestead uses this approach, and so will we.
<span>cd Code
</span><span>git clone https://github.com/Swader/publicinfo</span>

This folder is the repository of all such individual ini files. Create two new files here:

<span>sudo apt-get install php5-dev php5-mysql gcc libpcre3-dev</span>

These commands created two new ini files, each for one of the extensions we’ve previously built. As they are now in the mods-available folder, we can use the already available php5enmod (short for PHP enable mod) command line tool.

<span>sudo yum install php-devel php-mysqlnd gcc libtool</span>

Note: If you don’t have the php5enmod tool, symlinking the ini files into the conf.d folder of various php runtimes will do the trick:

<span>sudo yum install php-devel php-mysql gcc libtool</span>

The reason why there’s four entries is that we have the command line version of PHP and the FPM version of PHP. Each uses its own php.ini file, and each loads its own conf.d folder for extensions – hence, we need to add both extensions to both PHP versions if we want the extensions available all-around. Use this approach only if you don’t have the php5enmod tool.

Finally, let’s restart nginx and php-fpm to load these changes.

<span>yast2 -i php5-pear php5-devel php5-mysql gcc</span>

To see if we’ve got them installed, refresh the PHPinfo screen from before and search for mongo and intl respectively.

How to Install PHP Extensions from Source

Success!

Removing Extensions

To remove extensions, there’s no need to delete any actual files unless you’re really low on space. You can do it in three ways:

  1. Run php5dismod if you have the tool available. It’s the opposite of the php5enmod tool mentioned above. The .so files will stay in place, and the ini files will remain in mods-available, they just won’t load because their symlinks will be removed from the fpm and cli conf.d folders.
  2. Remove the symlinks manually. E.g. sudo rm /etc/php5/cli/conf.d/mongo.ini
  3. If you enabled the extensions by putting them directly into the php.ini files, remove those lines from the php.ini files, or better yet, comment them so that they remain accessible for further use should you ever change your mind.

Conclusion

As you can see, installing extensions from source is extremely simple, even if there are no precise instructions and even if the extension isn’t supported by an OS’s official repository. The next time you need to add an extension into your PHP installation on a *nix system (this tutorial applies to OS X as well), refer back to this post for a refresher.

Please leave your feedback below, and let me know if you’re confused by a specific extension and would like help with installing it.

Frequently Asked Questions (FAQs) about Installing PHP Extensions from Source

What are the prerequisites for installing PHP extensions from source?

Before you start installing PHP extensions from source, you need to have a few things in place. First, you need to have PHP installed on your system. You also need to have the PHP development environment set up, which includes tools like a compiler and make. Additionally, you need to have the PHP source code available, as you’ll be building the extension directly from this code. Lastly, you need to have the extension’s source code. This can usually be downloaded from the extension’s official website or from a repository like PECL.

How can I verify if a PHP extension is installed correctly?

After installing a PHP extension, you can verify its installation by using the phpinfo() function. This function provides a lot of information about your PHP installation, including the list of installed extensions. To use it, create a new PHP file in your web server’s document root, add a call to phpinfo(), and then view this file in your web browser. The installed extensions are listed in the ‘PHP Core’ section.

Can I install PHP extensions on a Windows system?

Yes, you can install PHP extensions on a Windows system. However, the process is slightly different compared to a Unix-like system. Instead of compiling the extension from source, you would typically download a precompiled DLL file and add it to your PHP installation. The PHP.net website provides detailed instructions on how to do this.

What should I do if I encounter errors during the installation process?

If you encounter errors during the installation process, the first step is to carefully read the error message. It often contains clues about what went wrong. Common issues include missing dependencies, incorrect configuration options, and problems with the PHP source code. If you can’t resolve the issue on your own, consider seeking help from the PHP community. There are many forums and mailing lists where you can ask for assistance.

How can I update a PHP extension?

Updating a PHP extension typically involves downloading the latest version of the extension’s source code and then repeating the installation process. However, the exact steps can vary depending on the extension. It’s a good idea to check the extension’s official documentation for specific update instructions.

Can I install multiple PHP extensions at once?

Yes, it’s possible to install multiple PHP extensions at once. This can be done by specifying multiple extensions in the configure command. However, keep in mind that each extension may have its own set of dependencies and configuration options.

How can I uninstall a PHP extension?

Uninstalling a PHP extension involves removing the extension’s configuration from your PHP.ini file and then restarting your web server. If the extension was installed as a shared module, you may also need to delete the module’s .so or .dll file.

What is the PECL repository?

The PECL repository is a collection of PHP extensions that are distributed as source code. These extensions can be installed using the PECL command, which automates the process of downloading, compiling, and installing extensions.

Can I install PHP extensions without root access?

Yes, it’s possible to install PHP extensions without root access. This can be done by installing PHP in your home directory and then installing the extensions there. However, this approach requires more technical knowledge and may not be suitable for all users.

What are some common PHP extensions and what do they do?

There are many PHP extensions available, each providing additional functionality to the PHP language. Some common extensions include MySQLi for interacting with MySQL databases, GD for creating and manipulating image files, and cURL for making HTTP requests.

The above is the detailed content of How to Install PHP Extensions from Source. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are some best practices for versioning a PHP-based API? What are some best practices for versioning a PHP-based API? Jun 14, 2025 am 12:27 AM

ToversionaPHP-basedAPIeffectively,useURL-basedversioningforclarityandeaseofrouting,separateversionedcodetoavoidconflicts,deprecateoldversionswithclearcommunication,andconsidercustomheadersonlywhennecessary.StartbyplacingtheversionintheURL(e.g.,/api/v

How do I implement authentication and authorization in PHP? How do I implement authentication and authorization in PHP? Jun 20, 2025 am 01:03 AM

TosecurelyhandleauthenticationandauthorizationinPHP,followthesesteps:1.Alwayshashpasswordswithpassword_hash()andverifyusingpassword_verify(),usepreparedstatementstopreventSQLinjection,andstoreuserdatain$_SESSIONafterlogin.2.Implementrole-basedaccessc

What are the differences between procedural and object-oriented programming paradigms in PHP? What are the differences between procedural and object-oriented programming paradigms in PHP? Jun 14, 2025 am 12:25 AM

Proceduralandobject-orientedprogramming(OOP)inPHPdiffersignificantlyinstructure,reusability,anddatahandling.1.Proceduralprogrammingusesfunctionsorganizedsequentially,suitableforsmallscripts.2.OOPorganizescodeintoclassesandobjects,modelingreal-worlden

What are weak references (WeakMap) in PHP, and when might they be useful? What are weak references (WeakMap) in PHP, and when might they be useful? Jun 14, 2025 am 12:25 AM

PHPdoesnothaveabuilt-inWeakMapbutoffersWeakReferenceforsimilarfunctionality.1.WeakReferenceallowsholdingreferenceswithoutpreventinggarbagecollection.2.Itisusefulforcaching,eventlisteners,andmetadatawithoutaffectingobjectlifecycles.3.YoucansimulateaWe

How can you handle file uploads securely in PHP? How can you handle file uploads securely in PHP? Jun 19, 2025 am 01:05 AM

To safely handle file uploads in PHP, the core is to verify file types, rename files, and restrict permissions. 1. Use finfo_file() to check the real MIME type, and only specific types such as image/jpeg are allowed; 2. Use uniqid() to generate random file names and store them in non-Web root directory; 3. Limit file size through php.ini and HTML forms, and set directory permissions to 0755; 4. Use ClamAV to scan malware to enhance security. These steps effectively prevent security vulnerabilities and ensure that the file upload process is safe and reliable.

How can you interact with NoSQL databases (e.g., MongoDB, Redis) from PHP? How can you interact with NoSQL databases (e.g., MongoDB, Redis) from PHP? Jun 19, 2025 am 01:07 AM

Yes, PHP can interact with NoSQL databases like MongoDB and Redis through specific extensions or libraries. First, use the MongoDBPHP driver (installed through PECL or Composer) to create client instances and operate databases and collections, supporting insertion, query, aggregation and other operations; second, use the Predis library or phpredis extension to connect to Redis, perform key-value settings and acquisitions, and recommend phpredis for high-performance scenarios, while Predis is convenient for rapid deployment; both are suitable for production environments and are well-documented.

What are the differences between == (loose comparison) and === (strict comparison) in PHP? What are the differences between == (loose comparison) and === (strict comparison) in PHP? Jun 19, 2025 am 01:07 AM

In PHP, the main difference between == and == is the strictness of type checking. ==Type conversion will be performed before comparison, for example, 5=="5" returns true, and ===Request that the value and type are the same before true will be returned, for example, 5==="5" returns false. In usage scenarios, === is more secure and should be used first, and == is only used when type conversion is required.

How do I perform arithmetic operations in PHP ( , -, *, /, %)? How do I perform arithmetic operations in PHP ( , -, *, /, %)? Jun 19, 2025 pm 05:13 PM

The methods of using basic mathematical operations in PHP are as follows: 1. Addition signs support integers and floating-point numbers, and can also be used for variables. String numbers will be automatically converted but not recommended to dependencies; 2. Subtraction signs use - signs, variables are the same, and type conversion is also applicable; 3. Multiplication signs use * signs, which are suitable for numbers and similar strings; 4. Division uses / signs, which need to avoid dividing by zero, and note that the result may be floating-point numbers; 5. Taking the modulus signs can be used to judge odd and even numbers, and when processing negative numbers, the remainder signs are consistent with the dividend. The key to using these operators correctly is to ensure that the data types are clear and the boundary situation is handled well.

See all articles