Home > Backend Development > PHP Tutorial > Getting Started with PHP: Linux Server

Getting Started with PHP: Linux Server

PHPz
Release: 2023-05-20 08:56:01
Original
1045 people have browsed it

As a developer, it is essential to understand the use of different programming languages ​​and corresponding tools. As a mainstream programming language, PHP is widely used in Web application development, and its application scope covers the entire Internet. Here, we will introduce how to learn and use the PHP language on a Linux server.

  1. Preparation

Before you start learning PHP, you need an environment to run PHP. Usually, choosing to build a Linux server is a good choice. In order to avoid installation and configuration trouble, we will use Ubuntu system as the server system.

When installing the server, you need to set a username and password. You also need to install an SSH client so that you can log in to your server from a remote location and operate the configuration.

  1. Install PHP

The Ubuntu system comes with PHP package. You can check whether it is installed by running the following command:

$ php -v
Copy after login

If it is already installed, it PHP version information will be displayed. If it does not show up, you need to execute the following command:

$ sudo apt-get install php7.2
Copy after login

Start the Apache server:

$ sudo systemctl start apache2
Copy after login

Now you can create a simple PHP file and access it in your web browser. First, go to the Apache website root directory:

$ cd /var/www/html
Copy after login

Open a text editor and create a file named "test.php":

$ sudo vi test.php
Copy after login

Enter the following content in the file:

<?php
phpinfo();
?>
Copy after login

Save the file and close the editor. Now, you can access it in your browser: http://[server IP address]/test.php. You can see a page with information about installed PHP version.

  1. Install database

PHP is mainly used to interact with databases and generate dynamic web applications. On Linux servers, the most popular and widely used is the MySQL database. You can install it using the following command:

$ sudo apt-get install mysql-server
Copy after login

During the installation process, you will be asked to set the MySQL administrator password. After your installation is complete, you can test whether MySQL has been successfully installed by the following method:

$ mysql -u root -p
Copy after login

Create a user:

mysql> CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
Copy after login

Give permissions to the user:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';
Copy after login

Now you have succeeded The MySQL database is set up and you can already connect and read data using username and password.

  1. Writing a PHP Application

Here we will enter a simple PHP application to test our previously installed PHP and MySQL:

<?php
// Connect to MySQL
$connection = mysqli_connect("localhost", "newuser", "password", "testdb");

// Check connection
if (!$connection) {
    echo "Connection failed: " . mysqli_connect_error();
    exit;
}

// Query database for the average population of cities with population > 100,000
$query = "SELECT AVG(CityPopulation) as AvgPopulation FROM Cities WHERE CityPopulation > 100000";
$result = mysqli_query($connection, $query);

// Display result
$row = mysqli_fetch_assoc($result);
echo "Average population of cities with population over 100,000: " . $row["AvgPopulation"];

// Close connection
mysqli_close($connection);
?>
Copy after login

On your Ubuntu server, create a file called "test.php" and copy the above code into its contents. Make sure you change the username and password to your own MySQL username and password. Now, enter the following address in your browser to run the application:

http://[server IP address]/test.php

You should see the output: Specified population exceeds The average population of a city is 100,000.

  1. Conclusion

Learning and using PHP on a Linux server can help you understand how to create and deploy web applications. By studying the content introduced in this article, you will not only be able to install PHP and MySQL on the Ubuntu server, but also understand how to write and run simple PHP applications. Based on this, you can start to explore advanced PHP development, apply these skills to actual projects, and enjoy the fun and charm of PHP programming.

The above is the detailed content of Getting Started with PHP: Linux Server. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template