跳转至

5. lnmp搭建

1. 部署nginx

# centos7

yum install nginx -y
nginx
systemctl enable nginx

2. 部署php

yum instatll php php-fpm -y
systemctl enable php-fpm
systmectl start php-fpm

3. 配置nginx支持php

vim /etc/nginx/nginx.conf

location / {
    index index.php;
}
location ~\.php {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    # fastcgi_param  SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;  #指定路径
    include fastcgi_params;
}

4. 重启nginx

nginx -s reload

5. 编写页面测试nginx支持php

vim /usr/share/nginx/html/index.php
<?php
    phpinfo();
?>

6. 访问页面测试

# ip:80

7. 部署MySQL

#1.安装MySQL
yum install mariadb mariadb-server -y
systemctl start mariadb

#2.配置MySQL(除了输入密码:123456,其他一直回车!)
mysql_secure_installation

#3.登录测试
mysql -uroot -p123456

8. 安装php支持MySQL的工具

yum install php-mysql -y

9. 重启服务

systemctl restart nginx
systemctl restart php-fpm

10. 编写php页面测试连接MySQL

vim /usr/share/nginx/html/conn_mysql.php
#
<?php
    $conn=mysql_connect('localhost','root','123456');
    if($conn)
        echo "success";
    else
        echo "failure";
    mysql_close();
?>

11. 访问测试

# ip/conn_mysql.php

12. 部署wordpress

# 有了lnmp环境以后,就可以部署相关服务应用了,比如wordpress

1. 创建数据库及用户

1.创建wordpress数据库
mysql -uroot -p123456
create database wordpress;

2.创建专属用户
grant all on wordpress.* to wordpress@'%' identified by '123456';
flush privileges;

2. 确保nginx支持php解析

server{
  listen 80;
  server_name _;
  location / {
    root html/myphp;
    index index.html index.php;
  }
  location ~.*\.(php|php5)?$ {
    root html/myphp;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi.conf;
  }
}

3. 下载wordpress

# 官网:https://wordpress.org/
wget https://wordpress.org/latest.tar.gz
tar -zxvf latest.tar.gz

4. 修改配置文件

server{
  listen 80;
  server_name _;
  location / {
    root /var/www/html/wordpress;
    index index.html index.php;
  }
  location ~.*\.(php|php5)?$ {
    root /var/www/html/wordpress;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi.conf;
  }
}

5. 重启nginx

nginx -t
nginx -s reload

6. 浏览器访问测试

192.168.178.110

最后更新: 2022-02-16 13:02:16