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.
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.
The Ubuntu system comes with PHP package. You can check whether it is installed by running the following command:
$ php -v
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
Start the Apache server:
$ sudo systemctl start apache2
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
Open a text editor and create a file named "test.php":
$ sudo vi test.php
Enter the following content in the file:
<?php phpinfo(); ?>
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.
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
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
Create a user:
mysql> CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
Give permissions to the user:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';
Now you have succeeded The MySQL database is set up and you can already connect and read data using username and password.
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); ?>
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.
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!