Building PHP website with Ubuntu

Time:2021-7-22

The original is in my own blog, small partners can also pointRead the original textJump to see, there are good background music Oh~

Building PHP website with Ubuntu

I have written two articles about building PHP environment with Ubuntu system before

After many times of practice, I found that some things have not been mentioned. Here I will add the problems and methods encountered in the future as a complete summary of building lamp environment in Ubuntu.

One click installation lamp

I won’t do more introduction here. In my previous articlesOne click installation of lamp environment in UbuntuIt is also explained that a lamp can be set up with two simple commands;
(bloggers tend to use PHP when building PHP environmentOne click installationThis lazy way to build (unless the scene has to be that each software must be downloaded and installed separately, then it will be installed one by one). Compared with the installation of Apache, MySQL and PHP programs one by one, this way is more efficient and faster, the configuration is simple, and the unnecessary trouble can be avoided.)

Configuration database remote access

After installing the lamp environment, the next step is to configure our remote MySQL users. This is very useful because in daily database operations, compared withphpmyadminThis method, I prefer to use tools to operate the database. Recommend a user-friendly database toolnavicat for mysqlWe have been using this MySQL tool for database operation, which greatly improves the work efficiency;

Back to the point, in the one click installation program, when installing to MySQL, you will be prompted to set a root password. After the installation is completed, in the Ubuntu command line (hereinafter referred to ascommand line)Visit MySQLmysql -u root -p, and then type the password to enter MySQL and select the database:use mysql

Create user

Command:CREATE USER 'username'@'host' IDENTIFIED BY 'password';
example:

  1. CREATE USER 'test'@'localhost' IDENTIFIED BY 'test2017666';
  2. CREATE USER 'ycfw'@'%' IDENTIFIED BY 'ycfw2017666';
  3. CREATE USER 'zdip'@'118.119.120.110' IDENTIFIED BY 'zdip2017666';

Parameters:

  1. User name: MySQL user password
  2. Host: Specifies the host on which the user can access the database (localhost specifies that the database can only be accessed locally, and% refers to remote access, which can be accessed anywhere, or an IP access can be specified, example 3)
  3. Password access password

User authorization

Command:GRANT privileges ON databasename.tablename TO 'username'@'host';
example:

  1. GRANT all ON *.* TO 'test'@'test2017666';(with all database operation permissions)
  2. GRANT all ON test.* TO 'test'@'test2017666';(have all table operation permissions of test database)
  3. GRANT select ON test.test TO 'test'@'test2017666';(have all the query permissions of the test table in the test database)
  4. ……

Parameters:

  1. Privileges: the operation permissions on the database, which can be insert, select, update and delete; All gives all operations permission
  2. Databasename and tablename: database and database name. Here you can specify the database and data table that can be operated. You can specify a separate database or data table. All databases can be operated*.*
  3. ‘user name’ @’host ‘: set the authorized user and host

After setting, execute flush privileges to refresh the MySQL permission table

At this time, you can use the new remote user to try to connect to the database. If the connection is made, the configuration of the remote user of the database has been completed. If not, continue to look down and modify the configuration item of MySQL;

View authorization information

Command:show grants for [email protected]

Modify my.cnf

If you create a new remote user and know that the new remote user’s permission is OK through the view authorization information command, you can’t link it. Then you may need to do another step to modify thebind-addressParameters.

My.con is in the mysq directory/etc/mysql/After entering the directory, find the bind address in the configuration file of MySQLbind-address=127.0.0.1, amend to readbind-address=0.0.0.0

PS: 1. Due to different versions of Ubuntu, the installed versions of MySQL may also be different, so you just need to find the relevant MySQL configuration file to modify it (a related reference article will be posted at the end of the article later, which is described in more detail); 2. Bind address means the binding address. Originally 127.0.0.1 is bound to local access only. If it is changed to 0.0.0.0, any IP can access it

Through the above configuration, remote MySQL users should be able to access.

Domain name binding

Domain name configuration

Here, we will not introduce the operation of resolving domain name to IP from the domain name provider. Let’s talk about the domain name configuration in apache2 directly
About apache2 domain name binding my previous articleLinux server domain name binding specified directory (APACHE 2 binding multiple domain names under Ubuntu)But later, I found that the configuration file for configuring domain name doesn’t need to be that long, as long as the following paragraph:

<VirtualHost *:80>
        ServerName www.cxiansheng.cn
        ServerAdmin [email protected]
        DocumentRoot /var/www/cxsblog

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

The rest of the configuration soft connection configuration soft connection, the place to fill in the domain name to fill in the domain name, and the operation of that article is almost the same;
PS: when we implement multi domain name binding in practice, the domain name may always point to a directory. At this time, we can delete 000-default.conf in site available and try to solve this problem. Of course, the actual situation should prevail

Apache2 route rewriting

One click installation of apache2 generally does not open Rewrite Module by default, we need to open it manually
Command line input:a2enmod rewrite, which will automatically open the rewriting module of Apache/etc/apache2/mod-enabledGenerate a soft connection to rewrite.load in mod available. If we find that no soft connection is generated in the directory after executing this command, we can generate it manually;
If the command fails to execute, the soft connection can also be generated manually

PS: the reason why we have to open this is: generally, in our web projects, there will be some pseudo static (. Htaccess) files, and we will put the URL rewriting rules in this file. If rewrite is not opened, Apache will not recognize these rules, leading to 500 or other errors in our website

apache.conf

stay/etc/apache2Find the apache2.conf file in the directory of, open the file, modify the permission problem for the website path, and find the

<Directory />
        Options Indexes FollowSymLinks
        AllowOverride none
        Require all denied
</Directory>

modifyAllowOverrideWhen it is all, allowoverride means to operate on. Htaccess. When it is none, the. Htaccess file will be ignored, resulting in our pseudo static failure and problems in accessing web projects;

For the meaning of each value of configuration option, see the following figure:
Building PHP website with Ubuntu

After configuring the above, restart Apache and execute/etc/init.d/apache2 restartAt this time

summary

After the above three sections, the operations necessary for building a lamp environment in Ubuntu are basically here. Generally, after the above steps, the website can be built successfully. Of course, in daily practice, there may be some unexpected situations, when the time comes to test our ability to solve problems.

Reference article