Linux-LNMP编译安装
一、安装Nginx
1. 关闭防火墙并安装依赖包
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
2. 创建运行用户、编译安装Nginx
useradd -M -s /sbin/nologin nginx
拖入软件包
cd /opt
tar zxvf nginx-1.12.0.tar.gz //解压软件包
cd nginx-1.12.0/
./configure \
--prefix=/usr/local/nginx \ //指定nginx的安装路径
--user=nginx \ //指定用户名
--group=nginx \ //指定组名
--with-http_stub_status_module //启用 http_stub_status_module 状态统计模块
make && make install
3. 优化路径并添加Nginx系统服务
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ //让系统识别nginx的操作命令
关闭服务方法:
nginx //开启服务
cat /usr/local/nginx/logs/nginx.pid //查看服务进程号
kill -3 进程号 //关闭服务
4. 验证服务是否开启
二、安装MySQL
1. 安装环境依赖包
yum -y install \
ncurses \
ncurses-devel \
bison \
cmake
yum -y install ncurses ncurses-devel bison cmake
2. 创建运行用户
useradd -M -s /sbin/nologin mysql
3. 编译安装
cd /opt
tar zxvf mysql-boost-5.7.20.tar.gz
cd /opt/mysql-5.7.20/
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
make && make instal //编译安装
4. 对mysql的数据目录进行权限调整
chown -R mysql:mysql /usr/local/mysql
vim /etc/my.cnf
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
5. 设置环境变量,并输出全局变量,刷新文件
echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
echo 'export PATH' >> /etc/profile
source /etc/profile
6. 初始化数据文件
cd /usr/local/mysql/bin
mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
7. systemctl服务控制
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /lib/systemd/system/
systemctl daemon-reload #刷新识别
8. 开启mysql服务验证
systemctl start mysqld.service #开启服务
systemctl enable mysqld #开机自启动
netstat -anpt | grep 3306 #查看端口
9. 设置mysql数据库密码并登录
mysqladmin -u root -p password "123"
mysql -u root -p
grant all privileges on *.* to 'root'@'%' identified by '123';
#授予root用户可以在所有终端远程登录,使用的密码是abc123,并对所有数据库和所有表有操作权限
show databases; #查看当前已有的数据库
三、安装PHP
1. 编译安装
1.1 安装关系依赖包
yum -y install \
gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel
1.2 解压软件包
tar jxvf php-7.1.10.tar.bz2
1.3 配置模块并安装
cd php-7.1.10
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
make && make install
2. 修改安装文件
2.1 路径优化
ln -s /usr/local/php/bin/* /usr/local/bin/
ln -s /usr/local/php/sbin/* /usr/local/sbin/
2.2 调整PHP配置文件
php.ini 主配置文件
php-fpm.conf 进程服务配置文件
www.conf 扩展展配置文件
//调整主配置文件
cp /opt/php-7.1.10/php.ini-development /usr/local/php7/php.ini
//在测试环境时使用php.ini-development文件,而在生产环境时使用php.ini-production文件
vim /usr/local/php7/php.ini
--1170行--修改
mysqli.default_socket = /usr/local/mysql/mysql.sock
--939行--取消注释,修改
date.timezone = Asia/Shanghai
php -m //验证安装的模块
//调整进程服务配置文件:
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
vim php-fpm.conf
——17行——去掉“;”注释
pid = run/php-fpm.pid
//调整扩展配置匹配文件
cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf
2.3 启动php-fpm
/usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
netstat -natp | grep 9000
2.4 配置Nginx支持PHP解析
vim /usr/local/nginx/conf/nginx.conf
2.5 创建首页,重启nginx
vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
systemctl restart nginx
2.6 验证
浏览器访问:http://192.168.117.60/index.php