一、搭建前的准备
- 更新系统软件yum:
yum update
- 查看是否已安装wget:
rpm -qa wget
否则(结果显示空白)安装:yum install wget
- 查看是否已安装编译器:
rpm -qa gcc
否则(结果显示空白)安装:yum install gcc gcc-c++
二、安装Nginx
- 安装nginx依赖包
nginx的Rewrite模块和HTTP核心模块会使用到PCRE正则表达式语法:yum -y install pcre pcre-devel
nginx的各种模块中需要使用gzip压缩:yum -y install zlib zlib-devel
安全套接字层密码库:yum -y install openssl openssl-devel
- 下载nginx包并解压
这里我们新建目录/root/software,并进入:
mkdir /root/software
cd /root/software
在该文件夹下载nginx压缩包:wget http://nginx.org/download/nginx-1.14.0.tar.gz
解压:tar -zxvf nginx-1.14.0.tar.gz
- 编译安装
这里我们编译安装到新建目录nginx
mkdir /root/software/nginx
cd nginx-1.14.0
./configure --prefix=/root/software/nginx
make
make install
- 创建并设置nginx运行账号
groupadd nginx
useradd -M -g nginx -s /sbin/nologin nginx
cd /root/software/nginx/conf
修改文件nginx.conf,设置user参数如下:user nginx;
- 设置nginx为系统服务
在文件夹/lib/systemd/system/新建文件nginx.service,文件内容:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/root/software/nginx/sbin/nginx
ExecReload=/root/software/nginx/sbin/nginx -s reload
ExecStop=/root/software/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
- 设置nginx开机自启动
systemctl enable nginx.service
- 开启nginx服务
systemctl start nginx.service
查看nginx是否启动成功:systemctl status nginx
或者ps aux | grep nginx
- 防火墙开放80端口
nginx默认使用80端口,可在nginx.conf中配置,若无需进行远程访问则不需要开放端口
永久开放80端口:firewall-cmd --zone=public --add-port=80/tcp --permanent
重启防火墙:firewall-cmd --reload
查看防火墙开启状态:systemctl status firewalld
查看80端口是否开放成功:firewall-cmd --zone=public --query-port=80/tcp
可在windows宿主主机浏览器直接访问虚拟机ip测试是否可以成功访问。
三、安装MySql
- 查看是否已安装mysql:
rpm -qa mysql
,有则卸载。 - 安装编译mysql需要的依赖包:
yum install libevent* libtool* autoconf* libstd* ncurse* bison* openssl*
- 安装cmake(mysql5.5之后需要用cmake支持编译安装)
查看是否已安装cmake:rpm -qa cmake
没有则下载编译安装:
cd /root/software
wget https://cmake.org/files/v3.12/cmake-3.12.1.tar.gz
tar -xf cmake-3.12.1.tar.gz
cd cmake-3.12.1
./configure
make
make install
检查cmake是否安装成功:cmake --version
- 下载mysql包并解压
cd /root/software
wget http://cdn.mysql.com/Downloads/MySQL-8.0/mysql-8.0.12.tar.gz
tar -zxvf mysql-8.0.12.tar.gz
- 编译安装
mkdir /root/software/mysql
mkdir /root/software/mysql/data
cd mysql-8.0.12
cmake . -DCMAKE_INSTALL_PREFIX=/root/software/mysql -DMYSQL_DATADIR=/root/software/mysql/data -DSYSCONFDIR=/etc -DMYSQL_TCP_PORT=3306 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/root/software/mysql-8.0.12/boost
make
make install
- 创建用户
groupadd mysql
useradd -M -g mysql -s /sbin/nologin mysql
chown -R mysql:mysql /root/software/mysql
- 配置/etc/my.cnf文件
[mysqld]
server-id=1
port=3306
basedir=/root/software/mysql
datadir=/root/software/mysql/data
socket=/root/software/mysql/mysql.sock
[client]
default-character-set=utf8
socket=/root/software/mysql/mysql.sock
[mysql]
default-character-set=utf8
socket=/root/software/mysql/mysql.sock
- 修改目录权限
chown -R mysql:mysql /root/software/mysql
chmod 755 /root/software/mysql -R
- 初始化配置
bin/mysqld --initialize --user=mysql --datadir=/root/software/mysql/data
bin/mysql_ssl_rsa_setup
若报错:OS errno 13 - Permission denied
则将mysql安装目录的所有上级目录更新权限为755
- 设置mysql为系统服务
在文件夹/lib/systemd/system/新建文件mysql.service,文件内容:
[Unit]
Description=mysqld
After=network.target
[Service]
Type=forking
ExecStart=/root/software/mysql/support-files/mysql.server start
ExecStop=/root/software/mysql/support-files/mysql.server stop
ExecRestart=/root/software/mysql/support-files/mysql.server restart
ExecReload=/root/software/mysql/support-files/mysql.server reload
PrivateTmp=true
[Install]
WantedBy=multi-user.target
- 设置mysql开机启动
systemctl enable mysqld
- 启动mysql服务
systemctl start mysqld
查看mysql是否启动成功:systemctl status mysqld
或者ps aux | grep mysqld
- 修改账号密码
为了加强安全性,MySQL5.7以后为root用户随机生成了一个密码,在/var/log/mysqld.log中;
若/var/log/mysqld.log找不到,先在/etc/my.cnf中添加skip-grant-tables
以无密码形式登录mysql,重启mysqld;
切换到mysql数据库use mysql;
查看user表,会发现:
user:当前数据库的用户名;
authentication_string: 用户密码;在mysql 5.7.9以后废弃了password字段和password()函数;
plugin: 密码加密方式;
<1> 如果当前root用户authentication_string字段下有内容,先将其设置为空;
update user set authentication_string='' where user='root';
<2> 退出mysql, 删除/etc/my.cnf文件最后的 skip-grant-tables 重启mysql服务。
<3> 在mysql/bin目录下登录mysql
./mysql -u root -p
passwrod:直接回车
<4> 使用ALTER修改root用户密码(WITH mysql_native_password类型,用于native连接,这里设置的空密码):
ALTER USER root@localhost IDENTIFIED WITH mysql_native_password BY '';
<5> 成功,进行连接
四、安装PHP
- 安装php7.2所需的一些依赖扩展库
yum install -y libxml2* openssl* libcurl* libjpeg* libpng* freetype* libmcrypt*
- 下载解压官网php7.2源码包
wget -c http://cn2.php.net/distributions/php-7.2.9.tar.gz
tar -xzvf php-7.2.9.tar.gz
- 安装php模块,编译安装
cd php-7.2.9
./configure --prefix=/root/software/php --with-mysqli --with-pdo-mysql --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-simplexml --enable-xml --disable-rpath --enable-bcmath --enable-soap --enable-zip --with-curl --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mbstring --enable-sockets --with-gd --with-openssl --with-mhash --enable-opcache --enable-fileinfo --with-iconv=/usr/local/libiconv
make && make install
# 编译安装前可指定php.ini文件 --with-config-file-path=/root/software/php/php.ini
- 添加配置文件
cd /root/software/php/etc
cp php-fpm.conf.default /mnt/php7/etc/php-fpm.conf
cd php-fpm.d
mkdir /mnt/php7/etc/php-fpm.d
cp www.conf.default www.conf
- 修改/mnt/php7/etc/php-fpm.conf
error_log = /root/software/php/var/log/php-fpm.log
- 创建用户
groupadd www
useradd -M -g www -s /sbin/nologin www
chown -R www:www /root/software/php
chown -R www:www /mnt/php7
- 启动php-fpm
/root/software/php/sbin/php-fpm
五、配置nginx并解析php文件
- nginx目录下的conf/nginx.conf
server{
listen 80;
server_name localhost;
root /root/www/web;
location / {
index index.html index.htm index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
- 重新加载nginx
systemctl reload nginx
- /root/www/web新建index.php
-
成功