Set PHP Environment Variables Ubuntu

Set PHP Environment Variables Ubuntu

To set PHP environment variables in Ubuntu, you can configure your PHP application with specific settings, such as database credentials, debugging options, or any other configuration parameters. Environment variables make it easier to manage these settings without modifying your PHP code directly.

In this tutorial, you will learn how to set PHP environment variables in an Ubuntu environment.

How to Set PHP Environment Variables in Ubuntu

To set PHP environment variables in Ubuntu, you can use the following steps:

  • Step 1: Update System Packages
  • Step 2: Choose Environment Variables
  • Step 3: Edit the .bashrc or .bash_profile File
  • Step 4: Set/Add Environment Variables
  • Step 5: Reload Your Bash Environment
  • Step 6: Verify the Environment Variable

Step 1: Update System Packages

Firstly, open your terminal and execute the following command into it to update system packages:

sudo apt update
sudo apt install php

Step 2: Choose Your Environment Variables

Next, you need to decide which environment variables you want to set. These variables can include database connection details, API keys, or any configuration-specific values your PHP application requires.

Step 3: Edit the .bashrc or .bash_profile File

You can set environment variables in the system-wide .bashrc file or your user’s .bashrc or .bash_profile file. The latter is usually preferred, as it allows you to define environment variables specifically for your user.

Open your preferred terminal and use a text editor (e.g., Nano, Vim, or Gedit) to edit the .bashrc or .bash_profile file:

For the current user:

nano ~/.bashrc

For a different user (replace username with the target username):

sudo nano /home/username/.bashrc

Step 4: Set/Add Environment Variables

Add your environment variables in the format export VARIABLE_NAME="variable_value". For example, to set a database connection variable:

export DB_HOST="localhost"
export DB_USERNAME="myuser"
export DB_PASSWORD="mypassword"

Save the file and exit your text editor.

Step 5: Reload Your Bash Environment

To make the changes take effect, you can either restart your terminal or execute the following command:

source ~/.bashrc

Step 6: Verify the Environment Variable

You can access these environment variables in your PHP scripts using the getenv() function or the $_ENV superglobal. For example:

$dbHost = getenv('DB_HOST');
$dbUsername = getenv('DB_USERNAME');
$dbPassword = getenv('DB_PASSWORD');

Alternatively, you can use $_ENV:

$dbHost = $_ENV['DB_HOST'];
$dbUsername = $_ENV['DB_USERNAME'];
$dbPassword = $_ENV['DB_PASSWORD'];

Create a simple PHP script to verify that your environment variables are accessible:

<?php
$dbHost = getenv('DB_HOST');
$dbUsername = getenv('DB_USERNAME');
$dbPassword = getenv('DB_PASSWORD');

echo "Database Host: " . $dbHost . "<br>";
echo "Database Username: " . $dbUsername . "<br>";
echo "Database Password: " . $dbPassword . "<br>";
?>

Conclusion

That’s it! You’ve successfully set and accessed PHP environment variables in Ubuntu. This can help you keep sensitive information secure and manage configuration settings for your PHP applications.

Recommended Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *