Home  >  Article  >  Backend Development  >  How to deploy php project to linux system

How to deploy php project to linux system

PHPz
PHPzOriginal
2023-03-23 16:53:172466browse

In the modern software development process, project deployment has become an indispensable link. Among them, deploying PHP projects to the Linux operating system is a relatively common way. This article will focus on the steps and considerations on how to deploy a PHP project to the Linux operating system.

Step One: Install the Necessary Software

Before deploying a PHP project, you need to ensure that the required software is installed on your Linux operating system. These software typically include: Apache, MySQL or MariaDB and PHP.

  1. Installing Apache

Apache is a very common web server on Linux operating systems. If Apache is not installed on your Linux operating system, you can use the following command to install it:

sudo apt-get update
sudo apt-get install apache2

After the installation is complete, you can use the following command to start the Apache service:

sudo systemctl start apache2
  1. Install MySQL Or MariaDB

MySQL and MariaDB are both commonly used open source relational database management systems. You can choose one of them to install according to your needs. Taking Ubuntu as an example, you can use the following command to install:

sudo apt-get install mysql-server

After the installation is complete, you can use the following command to start the MySQL service:

sudo systemctl start mysql
  1. Install PHP

PHP is a very commonly used server-side scripting language. You can use the following command to install PHP on the Linux operating system:

sudo apt-get install php libapache2-mod-php php-mysql

After the installation is complete, you can use the following command to restart the Apache service:

sudo systemctl restart apache2

Step 2: Upload the code

Upload your PHP project code to the Linux operating system server. You can use an SFTP tool such as FileZilla to upload your code to the server. Typically, you would place your code in the /var/www/html directory.

Step 3: Configure Apache

Apache is a commonly used web server on Linux operating systems. You need to make some configurations to ensure that your PHP project can run correctly.

  1. Create a virtual host

If you need to run multiple PHP projects on the same Linux server, you need to use the virtual host function. To create a virtual host, you create a configuration file in the /etc/apache2/sites-available directory.

sudo nano /etc/apache2/sites-available/myproject.conf

The following is an example configuration file:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/myproject
    ServerName myproject.local
    ServerAlias www.myproject.local
    <Directory /var/www/html/myproject>
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/myproject_error.log
    CustomLog ${APACHE_LOG_DIR}/myproject_access.log combined
</VirtualHost>

In the configuration file, you need to set DocumentRoot to your project directory. Please note that the directory here should be the real project directory.

  1. Activate virtual host

In order to activate the virtual host configuration file, you need to create a symbolic link on Apache2:

sudo ln -s /etc/apache2/sites-available/myproject.conf /etc/apache2/sites-enabled/

Restart the Apache service:

sudo systemctl restart apache2

Step 4: Open the firewall port

On the Linux operating system, the firewall will be enabled by default. Therefore, you need to make sure your firewall has the relevant ports open. You can use the following command to open the relevant port:

sudo ufw allow 80/tcp
sudo ufw allow 3306/tcp

Step 5: Set up the database

If your PHP project needs to use a database, you need to use a MySQL or MariaDB database Create a database and user and grant appropriate permissions. On the Linux operating system, you can use the following command to achieve this:

mysql -u root -p

After logging in, enter the following command to create a database named myproject:

CREATE DATABASE myproject;

Create a user named username, And set the password:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

Grant the user access to the myproject database:

GRANT ALL PRIVILEGES ON myproject.* TO 'username'@'localhost' IDENTIFIED BY 'password';

Refresh the database permission settings:

FLUSH PRIVILEGES;

Note: In the above operation, username and password It should be the specific username and password you set.

Step 6: Test the project

After completing the above steps, you need to test whether your PHP project can run successfully on the Linux operating system.

  1. Open your browser

Open your browser and enter your server IP address or domain name, for example: http://myproject.local.

  1. Test

If you see the homepage of the project, congratulations, your PHP project has been successfully deployed to the Linux operating system.

Summary

Deploying a PHP project to a Linux operating system is not a complicated task, but it still requires you to pay attention to some tips and details. Through the introduction of this article, you should have mastered all the steps and precautions on how to deploy a PHP project to a server with a Linux operating system.

The above is the detailed content of How to deploy php project to linux system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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