lnmp-env-in-ubuntu



OS:  Ubuntu 5.4.0-132-generic

安装: 
[test@host1:~]$ sudo apt install mariadb-server mariadb-client php-fpm nginx


nginx:
[test@host1:~]$ nginx -v
nginx version: nginx/1.18.0 (Ubuntu)


mysql:
[test@host1:~]$ mysql --version
mysql  Ver 8.0.32-0ubuntu0.20.04.2 for Linux on x86_64 ((Ubuntu))



php-fpm: 
[test@host1:~]$ php-fpm7.4 -v
PHP 7.4.3-4ubuntu2.17 (fpm-fcgi) (built: Jan 10 2023 15:37:44)
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3-4ubuntu2.17, Copyright (c), by Zend Technologies

php: 
[test@host1:~]$ php -v
PHP 7.4.3-4ubuntu2.17 (cli) (built: Jan 10 2023 15:37:44) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3-4ubuntu2.17, Copyright (c), by Zend Technologies




2 配置mysql: 
据说mysql修改root密码需要这么做:
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password by 'yourpassword';

mysql: 
[test@host1:~]$ sudo mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.32-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

#修改root密码为your_new_password
mysql> alter user 'root'@'localhost' identified with mysql_native_password by 'your_new_password';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

mysql> exit
Bye

然后重新登录:
[test@host1:~]$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.32-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.03 sec)

mysql> \q
Bye



[test@host1:~]$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:   ####输入上一步骤中的密码

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2    #选择了strong密码
Using existing password for root.

Estimated strength of the password: 25
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password:

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : n

New password:

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y  #移除匿名用户
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n  #允许远程root登录

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y #移除test数据库
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y #重新加载权限表
Success.

All done!

以上结束后,设置了一个25位以上的密码,重新使用该密码登录mysql: 
[test@host1:~]$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.32-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql>

3、配置nginx
nginx默认配置文件为/etc/nginx/nginx.conf,里面有一句:
include /etc/nginx/sites-enabled/*;
所以我们需要修改/etc/nginx/sites-enabled/下的某个文件,sites-enabled下的文件是sites-available下的软链接
我们就修改sites-enabled下的配置文件,目前有一个default文件
1)、在server{}内,找到index开头的配置行,在该行中添加index.php。
2)、在server{}内找到location ~ \.php$ {},去除以下配置行的注释符号。
location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
3)、重启nginx服务: 
sudo nginx -s reload 


4、配置php
运行以下命令,在Nginx网站根目录中,新建phpinfo.php文件。
sudo vi <网站根目录>/phpinfo.php

<网站根目录>为变量,可通过Nginx配置文件查看。此时Nginx配置文件为默认文件/etc/nginx/sites-enabled/default,您可以运行cat /etc/nginx/sites-enabled/default命令查看文件内容,其中root指定的/var/www/html部分即为网站根目录

因此,对应的运行命令为:
sudo vi /var/www/html/phpinfo.php
增加如下内容:
<?php
  phpinfo();
?>

我们找到php-fpm指定的配置文件:
[test@host1:~]$ ps -ef | grep php-fpm
root       40898       1  0 Feb18 ?        00:00:03 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
www-data   48345   40898  0 03:22 ?        00:00:00 php-fpm: pool www
www-data   48346   40898  0 03:22 ?        00:00:00 php-fpm: pool www

得知php-fpm的配置文件为: /etc/php/7.4/fpm/php-fpm.conf
从php-fpm.conf中得知pid文件为:
pid = /run/php/php7.4-fpm.pid

我们需要重启php-fpm,重启命令如下:
kill -USR2 `cat /run/php/php7.4-fpm.pid`

关闭php-fpm:
kill -INT `cat /run/php/php7.4-fpm.pid`

测试访问phpinfo.php

在浏览器的地址栏输入http://<公网IP地址>/phpinfo.php进行访问


后续步骤
成功搭建LNMP环境后,建议您删除phpinfo.php测试文件,消除数据泄露风险。
rm -rf <网站根目录>/phpinfo.php
本教程中网站根目录为/var/www/html,则需要运行以下命令删除测试文件。
rm -rf /var/www/html/phpinfo.php

5、下载wordpress
下载最新版wordpress: 
wget https://wordpress.org/latest.zip && unzip lastest.zip

将wordpress复制到/var/www/并重命名为wp 

删除/etc/nginx/sites-available/default文件,创建/etc/nginx/sites-available/wp文件
将/etc/nginx/sites-available/wp文件的root改为/var/www/wp
为sites-available/wp文件创建软链接:sudo ln -s /etc/nginx/sites-available/wp /etc/nginx/sites-enabled/wp

重启nginx:sudo nginx -s reload

然后直接访问http://vps_ip,可能会看到php不支持mysql的提示信息,此时安装php-mysql扩展即可: sudo apt install php-mysql.
重新访问http://vps_ip,立即可以看到跳转到wordpress配置页面:
http://vps_ip/wp-admin/setup-config.php

Welcome to WordPress. Before getting started, you will need to know the following items.

  1. Database name
  2. Database username
  3. Database password
  4. Database host
  5. Table prefix (if you want to run more than one WordPress in a single database)

This information is being used to create a wp-config.php file. If for any reason this automatic file creation does not work, do not worry. All this does is fill in the database information to a configuration file. You may also simply open wp-config-sample.php in a text editor, fill in your information, and save it as wp-config.phpNeed more help? Read the support article on wp-config.php.

In all likelihood, these items were supplied to you by your web host. If you do not have this information, then you will need to contact them before you can continue. If you are ready…