准备工作:
- 服务器一台
- 手一双(如果能力较强,单手也可以)
- 眼睛一对(如果能力较强,睁一只眼闭一只眼也可以)准备好这些我们就可以开始搭建今天的lnmp环境了。
1、首先我们检查一下服务器是否有wget
rpm -qa wget
如果有我们就进行下一步,没有就自己下一个
yum install wget //代码附上
查看是否已安装编译器
rpm -qa gcc
如果没有安装yum install gcc gcc-c++
2、开始安装nginx
1、安装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
2、下载安装nginx并解压(下载到usr/local)
cd /usr/local
wget http://nginx.org/download/nginx-1.1.10.tar.gz
tar -zxvf nginx-1.1.10.tar.gz
3、编译安装
cd nginx-1.1.10
./configure --prefix=/usr/local/nginx
make
make install
4、创建并设置nginx运行账号:
groupadd nginx
useradd -M -g nginx -s /sbin/nologin nginx
cd /usr/local/nginx/conf
vim nginx.conf,设置user参数如下:
user nginx nginx
/usr/local/nginx/sbin/nginx -t
5、运行nginx
/usr/local/nginx/sbin/nginx
3、安装mysql
1、查看是否已安装mysql
rpm -qa mysql
有就卸掉我们重新搞起
rpm -e mysql // 普通删除模式
rpm -e --nodeps mysql // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除
使用 yum 命令安装 MySQL,安装前我们需要先去官网下载 Yum 资源包
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum update
yum install mysql-server
权限设置:
chown mysql:mysql -R /var/lib/mysql
初始化 MySQL:
mysqld --initialize
启动 MySQL:
service mysqld start
查看 MySQL 运行状态:
service mysqld status
验证版本号
mysqladmin --version
进入mysql查看是否可以连接
mysql -u **** -p
4、安装php
1、安装php依赖包
yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel
2、下载
wget https://www.php.net/distributions/php-7.2.17.tar.gz
tar -zxvf php-7.2.17.tar.gz
3、编译安装
cd php-7.2.0
./configure --prefix=/usr/local/php --disable-fileinfo --enable-fpm --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-openssl --with-zlib --with-curl --enable-ftp --with-gd --with-xmlrpc --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --enable-mbstring --with-mcrypt=/usr/local/libmcrypt --enable-zip --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock=/var/lib/mysql/mysql.sock --without-pear --enable-bcmath
(注意:–with-mcrypt参数指定的是libmcrypt的安装目录。Php7不再使用mysql的库来支持mysql的连接,而是启用了mysqlnd来支持,所以php7的编译已经不再使用–with-mysql参数指定mysql的安装位置了,若想支持mysql,需要设置–enable-mysqlnd、–with-mysqli和–with-pdo-mysql=mysqlnd参数,–with-mysql-sock指定的是编译mysql时-DMYSQL_UNIX_ADDR参数指定的文件)
make
make install
4、将php包解压目录中的配置文件放置到正确位置(configure命令中的–with-config-file-path设置的位置)
cp php.ini-development /etc/php.ini
创建并设置php-fpm运行账号
groupadd www-data
useradd -M -g www-data -s /sbin/nologin www-data
cd /usr/local/php/etc
cp php-fpm.conf.default php-fpm.conf
vim php-fpm.conf
发现搜索不到“user”(设置运行账号的位置),但发现文件的最后一行:
LNMP环境搭建(linux+Nginx + Mysql + PHP)
所以:
cd php-fpm.d
cp www.conf.default www.conf(否则include匹配不到文件)
vim www.conf
搜索“user”设置运行账号:
user=www-data
group=www-data
配置nginx支持php
vim /usr/local/nginx/conf/nginx.conf
添加如下代码
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;
}
修改完之后重启nginx
service nginx start
如果发现以下错误修改方法如下:
在 /etc/init.d/ 下创建nginx文件
vim /etc/init.d/nginx
插入以下代码
#!/bin/sh
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
cd /etc/init.d
chmod 755 /etc/init.d/nginx //给文件755权限
chkconfig --add nginx
然后重新执行nginx
service nginx start
设置php-fpm为系统服务
vim /etc/systemd/system/php-fpm.service
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
PrivateTmp=True
[Install]
WantedBy=multi-user.target
设置php开机自启动
systemctl enable php-fpm.service
启动php-fpm
systemctl start php-fpm.service
查看是否启动
ps aux | grep php-fpm
然后写一个php脚本测试nginx是否已支持php,php是否已支持mysql。