LNMP环境搭建

文章目录

  • LNMP环境搭建
    • LNMP部署之Nginx
      • 编译安装nginx
    • LNMP部署之MySQL
      • RPM安装
    • LNMP部署之PHP
      • yum安装
      • php连接数据库测试
  • lnmp环境搭建wordpress
    • 环境准备
    • 安装Wordpress网站系统

LNMP环境搭建

LNMP是一种web服务器架构,与LAMP类似。它由Linux、Nginx、MySQL和PHP组成,这四个组合再一起提供了一个完整的Web服务器环境。

LNMP部署之Nginx

编译安装nginx

nginx源码包下载地址:http://nginx.org/download/

第一步:安装依赖环境

[root@localhost ~]# hostnamectl set-hostname lnmp-server
[root@localhost ~]# bash
[root@lnmp-server ~]# 

[root@lnmp-server ~]#  yum install -y make gcc-c++ pcre pcre-devel zlib-devel openssl openssl-devel

第二步:下载nginx源码包

这里使用的是nginx 1.16.0版本

[root@lnmp-server ~]#  mkdir /opt/mytools/
[root@lnmp-server ~]#  wget -O /opt/mytools/nginx-1.16.0.tar.gz http://nginx.org/download/nginx-1.16.0.tar.gz

第三步:解包

[root@lnmp-server ~]#  cd /opt/mytools/
[root@lnmp-server mytools]# tar -zxf nginx-1.16.0.tar.gz 
[root@lnmp-server mytools]# ls
nginx-1.16.0  nginx-1.16.0.tar.gz
[root@lnmp-server mytools]# cd nginx-1.16.0
[root@lnmp-server nginx-1.16.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

第四步:创建nginx用户

[root@lnmp-server nginx-1.16.0]# useradd nginx -u 1160 -s /sbin/nologin -M
[root@lnmp-server nginx-1.16.0]# cat /etc/passwd |grep nginx
nginx:x:1160:1160::/home/nginx:/sbin/nologin

第五步:编译安装nginx

[root@lnmp-server nginx-1.16.0]# ./configure  --user=nginx --group=nginx  --prefix=/usr/local/nginx-1.16.0/ --with-http_stub_status_module --with-http_ssl_module

[root@lnmp-server nginx-1.16.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src
# 生成了Makefile
[root@nginx nginx-1.16.0]# make && make install

第六步:配置环境变量

[root@lnmp-server nginx-1.16.0]# echo "export  PATH=${PATH}:/usr/local/nginx-1.16.0/sbin/" >> /etc/profile
[root@lnmp-server nginx-1.16.0]# source /etc/profile

第七步:开启Nginx防火墙开放端口访问测试

[root@lnmp-server nginx-1.16.0]# nginx
[root@lnmp-server nginx-1.16.0]# netstat -tlnp |grep 80
tcp      0     0 0.0.0.0:80        0.0.0.0:*    LISTEN      55819/nginx: master 
[root@lnmp-server nginx-1.16.0]# firewall-cmd --add-port=80/tcp --permanent
success
[root@lnmp-server nginx-1.16.0]# firewall-cmd --reload
success

LNMP环境搭建_第1张图片

LNMP部署之MySQL

RPM安装

第一步:下载安装

[root@lnmp-server ~]# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

[root@lnmp-server ~]# rpm -ivh mysql-community-release-el7-5.noarch.rpm
[root@lnmp-server ~]# yum install -y mysql-server

第二步:启动服务

[root@lnmp-server ~]# systemctl start mysqld && systemctl enable mysqld
[root@lnmp-server ~]# netstat -tlnp |grep 3306
tcp6       0      0 :::3306           :::*         LISTEN      12019/mysqld   

LNMP环境搭建_第2张图片

第三步:设置密码

[root@lnmp-server ~]# mysqladmin -uroot password "000000"

第四步:测试登录

[root@lnmp-server ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.

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> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '000000';flush privileges;
Query OK, 0 rows affected (0.00 sec)
# root 用户授予所有数据库的所有权限,并设置密码为 '000000'
mysql> exit;

第五步:防火墙放行3306端口

[root@lnmp-server ~]# firewall-cmd --add-port=3306/tcp --permanent
success
[root@lnmp-server ~]# firewall-cmd --reload
success

第六步:远程测试访问

LNMP环境搭建_第3张图片

LNMP环境搭建_第4张图片

LNMP部署之PHP

yum安装

安装php7.4

安装remi扩展源

[root@lnmp-server ~]# yum install  -y epel-release 
[root@lnmp-server ~]# yum install -y  http://rpms.remirepo.net/enterprise/remi-release-7.rpm

安装yum管理工具

[root@lnmp-server ~]# yum install -y  yum-utils

查找是否有php74

[root@lnmp-server ~]# yum search php74

安装php

yum install -y php74-php-cli php74-php-common php74-php-devel php74-php-embedded php74-php-fpm php74-php-gd php74-php-mbstring php74-php-mysqlnd php74-php-pdo php74-php-opcache php74-php-xml php74-php-soap

建立软连接

[root@lnmp-server ~]# ln -s /opt/remi/php74/root/bin/php  /usr/local/bin/php

查看版本

[root@lnmp-server ~]# php -v
PHP 7.4.33 (cli) (built: Jun  6 2023 15:55:08) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies

启动fpm并设置开机自启

[root@lnmp-server ~]# systemctl start php74-php-fpm  && systemctl enable php74-php-fpm

配置nginx.conf文件

[root@lnmp-server ~]# vim /usr/local/nginx-1.16.0/conf/nginx.conf

       location / {
            root   html/myphp/;
            index   index.php index.html index.htm;
        }

        location ~ \.php$ {
            root html/myphp;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi.conf;
        }

生效配置文件

[root@lnmp-server ~]# nginx -s reload

创建php的首页脚本文件

[root@lnmp-server ~]# mkdir -p /usr/local/nginx-1.16.0/html/myphp/
[root@lnmp-server ~]# echo "" > /usr/local/nginx-1.16.0/html/myphp/index.php

浏览器访问

LNMP环境搭建_第5张图片

LNMP环境搭建完毕

php连接数据库测试

[root@lnmp-server ~]# cd /usr/local/nginx-1.16.0/html/myphp/
[root@lnmp-server myphp]# ls
index.php

[root@lnmp-server myphp]# vim php-mysql.php
<?php
header("content-type:text/html;charset=utf-8");
$servername = "localhost";
$username = "root";
$password = "000000";

$conn = new mysqli($servername,$username,$password);

if ($conn->connect_error) {
   die("连接失败:". $conn->connect_error);
}
echo "连接成功";
?>

访问测试

LNMP环境搭建_第6张图片

lnmp环境搭建wordpress

环境准备

启动MySQL数据库,创建用于wordpress博客数据库

[root@lnmp-server ~]# mysql -u root -p

创建wordpress数据库

mysql> create database wordpress;
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| WORDPRESS          |
| mysql              |
| performance_schema |
+--------------------+

创建用于wordpress专用的数据库用户

mysql> create user wordpress;

给该wordpress用户授权,运行在localhost本地登录mysql,且有增删改查的权限,密码000000

mysql> grant all on wordpress.* to 'wordpress'@'localhost' identified by '000000';

刷新授权表

mysql> flush privileges;

查询刚刚创建的用户信息

mysql> select user,authentication_string,host from mysql.user;

退出数据库

mysql> exit;

安装Wordpress网站系统

下载地址:https://cn.wordpress.org/

[root@lnmp-server ~]# ls
anaconda-ks.cfg  wordpress-6.2.2-zh_CN.zip

解压到站点目录

[root@lnmp-server ~]# unzip wordpress-6.2.2-zh_CN.zip -d /usr/local/nginx-1.16.0/html/

赋权

[root@lnmp-server ~]# cd /usr/local/nginx-1.16.0/html/
[root@lnmp-server html]# chmod 755  -R wordpress
[root@lnmp-server html]# chown nginx:nginx -R wordpress

修改nginx.conf配置文件

[root@lnmp-server html]# vim ../conf/nginx.conf
        location / {
            root   html/wordpress;
            index   index.php index.html index.htm;
        }
        location ~ \.php$ {
            root html/wordpress;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi.conf;
        }

重新读取nginx配置文件

[root@lnmp-server html]# nginx -s reload

浏览器访问

LNMP环境搭建_第7张图片

省略一系列安装步骤(按照自己设置的来就行了)

LNMP环境搭建_第8张图片

你可能感兴趣的:(Linux服务器架设学习,lnmp)