LNMP搭建
一、编译安装Nginx
1.1、安装准备
bash
#关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
bash
#安装依赖
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
#创建用户
useradd -M -s /sbin/nologin nginx
#获取源码包
wget https://nginx.org/download/nginx-1.24.0.tar.gz
关闭防火墙
安装依赖包
创建用户来配置nginx
bash
cd /opt
tar xf nginx-1.24.0.tar.gz#解压
bash
cd nginx-1.24.0/
#编译
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
1.2、编译安装
bash
cd nginx-1.24.0/
#编译
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
bash
#安装
make -j2 && make install
bash
#让系统识别nginx操作命令,可以补全
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
1.3、添加到系统服务(systemd启动)
二、编译安装Mysql服务
2.1、安装准备
bash
#依赖环境
yum -y install \
ncurses \
ncurses-devel \
bison \
cmake
#添加用户
useradd -s /sbin/nologin mysql
bash
将MySQL源代码包放在 /opt目录下
tar xf mysql-boost-5.7.20.tar.gz #解压
cd mysql-5.7.20
2.2、编译安装
bash
#编译
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 -j2 && make install
2.3、编辑配置文件
bash
#进入配置文件,把内容全删掉,然后加入以下内容
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
2.4、更改mysql安装目录和配置文件的属主属组
bash
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf
2.5、设置路径环境变量
bash
echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
source /etc/profile
2.6、数据库初始化
bash
cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
2.7、添加mysqld系统服务
bash
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
#能用systemd启动
bash
systemctl daemon-reload 刷新识别
systemctl start mysqld.service 开启服务
systemctl enable mysqld 开机自启动
ss -natp | grep 3306 查看端口
2.8、修改用户mycql的登录密码
bash
mysqladmin -u root -p password "123abc"
mysql -u root -p #授权远程登录
三、编译安装PHP
3.1、配置准备
bash
#安装依赖环境
#安装GD库和GD库关联程序,用来处理和生成图片
yum -y install \
libjpeg \
libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 \
libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel
bash
将PHP源代码包放在 /opt目录下
tar -jxvf php-7.1.10.tar.bz2 #解压
cd /opt/php-7.1.10/
3.2、编译安装
bash
#编译环境检测 功能模块添加
./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
bash
#安装
make -j2 && make install
3.3、路径优化
bash
ln -s /usr/local/php/bin/* /usr/local/bin/
ln -s /usr/local/php/sbin/* /usr/local/sbin/
3.4、调整PHP配置文件
bash
php有三个配置文件
php.ini 主配置文件
php-fpm.conf 进程服务配置文件
www.conf 扩展配置文件
调整主配置文件
bash
cp /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini #模板
vim /usr/local/php/lib/php.ini #修改主配置文件
调整进程服务配置文件
bash
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
vim php-fpm.conf
调整配置文件
bash
cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf
3.5、启动php-fpm
bash
/usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
ss -napt | grep 9000
bash
cd /opt/php-7.1.10/sapi/fpm
cp php-fpm.service /usr/lib/systemd/system/php-fpm.service
systemctl restart php-fpm.service
3.6、配置nginx支持PHP解析
bash
vim /usr/local/nginx/conf/nginx.conf
3.7、验证PHP测试页
bash
#创建网页文件
vi /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
#重启nginx服务
systemctl restart nginx
#在网页测试
http://192.168.2.102/index.php