环境说明:
系统 | IP | 所需服务 |
---|---|---|
CentOS7 | 192.168.159.144 | nginx、mysql、php |
实验前请关闭防火墙和SELINUX,并配置好网络源
1. 安装nginx
//安装依赖包
[root@longnian ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget
[root@longnian ~]# yum -y groups mark install 'Development Tools'
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
Marked install: Development Tools
//创建系统用户nginx
[root@longnian ~]# useradd -r -M -s /sbin/nologin nginx
[root@longnian ~]# id nginx
uid=996(nginx) gid=994(nginx) groups=994(nginx)
//创建日志存放目录
[root@longnian ~]# mkdir -p /var/log/nginx
[root@longnian ~]# chown -R nginx.nginx /var/log/nginx
//下载nginx并安装
[root@longnian ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@longnian src]# tar xf nginx-1.18.0.tar.gz
[root@longnian src]# cd nginx-1.18.0
[root@longnian nginx-1.18.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@longnian nginx-1.18.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
//配置环境变量
[root@longnian ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@longnian ~]# source /etc/profile.d/nginx.sh
[root@longnian ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::* :::*
端口起来后,用浏览器访问验证
2. 安装mysql
#安装依赖包
[root@longnian ~]# yum -y install libaio ncurses-devel openssl-devel openssl cmake mariadb-devel
#创建用户和组
[root@longnian ~]# useradd -r -M -s /sbin/nologin mysql
//创建mysql用户和组
[root@longnian ~]# useradd -r -M -s /sbin/nologin -u 306 mysql
[root@longnian ~]# id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)
//下载二进制格式的mysql软件包
[root@longnian ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
[root@longnian ~]# tar xf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz -C /usr/local
//创建软连接,修改属主和环境变量
[root@longnian ~]# cd /usr/local/
[root@longnian local]# ln -s mysql-5.7.30-linux-glibc2.12-x86_64/ mysql
[root@longnian local]# chown -R mysql.mysql mysql*
[root@longnian local]# ll -d mysql
lrwxrwxrwx. 1 mysql mysql 36 Jul 6 09:39 mysql -> mysql-5.7.30-linux-glibc2.12-x86_64/
[root@longnian ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@longnian ~]# source /etc/profile.d/mysql.sh
//创建数据存放目录
[root@longnian ~]# mkdir /opt/data
[root@longnian ~]# chown -R mysql.mysql /opt/data
[root@longnian ~]# ll -d /opt/data/
drwxr-xr-x 6 mysql mysql 4096 Jul 31 18:50 /opt/data/
初始化数据库,并配置
[root@longnian ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data
//这里加入了-insecure选项,可以不要密码
[root@longnian ~]# ln -s /usr/local/mysql/include /usr/local/include/mysql
[root@longnian ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@longnian ~]# ldconfig
//生成配置文件
[root@longnian ~]# cat > /etc/my.cnf <<EOF
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
EOF
//配置服务启动脚本
[root@longnian ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@longnian ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@longnian ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
//启动mysql,并修改密码
[root@longnian ~]# chkconfig mysqld on //设置为开机自动启动
[root@longnian ~]# service mysqld start
[root@longnian ~]# ss -antl |grep 3306
LISTEN 0 80 :::3306 :::*
[root@longnian ~]# mysql -e "set password = password('longnian123.')"
[root@longnian ~]# mysql -uroot -plongnian123.
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 5.7.24 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> quit
Bye
3. 安装PHP
//配置yum源
[root@longnian ~]# wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
[root@longnian ~]# yum -y install remi-release-7.rpm
[root@longnian ~]# yum clean all
[root@longnian ~]# yum makecache --enablerepo=remi-php74
//安装依赖包
[root@longnian ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php74-php-mysqlnd
//下载并安装php
[root@longnian ~]# wget https://www.php.net/distributions/php-7.4.7.tar.gz
[root@longnian ~]# tar xf php-7.4.7.tar.gz
[root@longnian ~]# cd php-7.4.7
[root@longnian ~]# ./configure --prefix=/usr/local/php7 \
--with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-png \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-json \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
这里如果报错需要安装两个依赖包,安装好后再次执行上面的命令
[root@longnian php-7.4.7]# yum -y install libsqlite3x-devel oniguruma-devel
[root@longnian php-7.4.7]# make -j $(cat /proc/cpuinfo |grep processor|wc -l)
[root@longnian php-7.4.7]# make install
//配置环境变量
[root@longnian ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@longnian ~]# source /etc/profile.d/php7.sh
[root@longnian ~]# php -v
PHP 7.4.7 (cli) (built: Jul 31 2020 18:00:53) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
//配置php-fpm
[root@longnian ~]# cp php-7.4.7/php.ini-production /etc/php.ini
[root@longnian ~]# cp php-7.4.7/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@longnian ~]# chmod +x /etc/rc.d/init.d/php-fpm
[root@longnian ~]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@longnian ~]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
[root@longnian ~]# vim /usr/local/php7/etc/php-fpm.conf
//添加以下四行
pm.max_children = 50 //最多同时提供50个进程提供50个并发服务
pm.start_servers = 5 //启动时启动5个进程
pm.min_spare_servers = 2 //最小空闲进程数
pm.max_spare_servers = 8 //最大空闲进程数
//启动php-fpm
[root@longnian ~]# service php-fpm start
Starting php-fpm done
[root@node2 ~]# chkconfig php-fpm on
[root@longnian ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:9000 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 80 :::3306 :::*
LISTEN 0 100 :::8080 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8005 :::*
//配置监听所有IP
[root@longnian ~]# vim /usr/local/php7/etc/php-fpm.d/www.conf
listen = 127.0.0.1:9000 `//将这里的127.0.0.1改为0.0.0.0`
[root@node2 ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@longnian ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:9000 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 80 :::3306 :::*
LISTEN 0 100 :::8080 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8005
4. 配置nginx
//创建php测试页面
[root@longnian ~]# vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
//修改nginx主配置文件
[root@longnian ~]# vim /usr/local/nginx/conf/nginx.conf
...
location / {
root html;
index index.php index.html index.htm; //在index后面添加index.php,表示有限访问php页面
}
...
//将以下内容取消注释并修改
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
...
//重读配置文件
[root@node2 ~]# nginx -s reload
访问网页
5. 利用lnmp搭建WordPress
5.1 配置数据库
[root@longnian ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 5.7.24 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> CREATE DATABASE wordpress;
..........
mysql> CREATE USER 'user'@'localhost' IDENTIFIED BY '123456';
..........
mysql> GRANT ALL PRIVILEGES ON wordpress.* TO 'user'@'localhost' IDENTIFIED BY '123456';
..........
mysql> FLUSH PRIVILEGES;
mysql> quit
Bye
5.2 安装和配置 WordPress
下载WordPress
//进入/usr/local/nginx/html/目录,并下载与解压 WordPress
[root@longnian html]# wget https://cn.wordpress.org/wordpress-5.0.4-zh_CN.tar.gz
[root@longnian html]# tar zxvf wordpress-5.0.4-zh_CN.tar.gz
//修改 WordPress 配置文件
//找到文件中 MySQL 的部分,并将相关配置信息修改为 配置 WordPress 数据库 中的内容
[root@longnian html]# cd wordpress
[root@longnian wordpress]# cp wp-config-sample.php wp-config.php
[root@longnian wordpress]# vim wp-config.php
........
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'user');
/** MySQL database password */
define('DB_PASSWORD', '123456');
/** MySQL hostname */
define('DB_HOST', 'localhost');
验证 WordPress 安装
在浏览器上访问安装界面
安装过程填写信息
安装完成!
登录
成功登录 WordPress 博客