1.准备
a.新建目录,/lnmp/php7、/lnmp/nginx:
mkdir -p /lnmp/php7
mkdir /lnmp/nginx
b.安装依赖
yum install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel libxml2-devel libpng-devel curl-devel numactl
2.安装nginx
wget http://nginx.org/download/nginx-1.18.0.tar.gz #下载最新的nginx
tar zxvf nginx-1.18.0.tar.gz #解压
cd nginx-1.18.0 #进入目录
./configure --prefix=/lnmp/nginx --with-http_ssl_module #安装到/lnmp/nginx
make && make install #编译&安装
cp /lnmp/nginx/sbin/nginx /usr/bin/
nginx #开启
nginx -s stop #关闭
nginx -s reload #平滑重启
注意:
安装后可能会遇到的问题
#nginx配置位置:/lnmp/nginx/conf/nginx.conf
#站点目录位置:/lnmp/nginx/html
#搭好后,本地无法访问
#关闭防火墙
sudo systemctl stop firewalld
#允许访问80端口
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
#此时就可以访问了
访问站点IP,出现下图则表示搭建成功了。
3.安装PHP
安装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
php7.4需要另外安装:
sqlite3:yum install sqlite-devel
oniguruma:yum install oniguruma oniguruma-devel
wget https://www.php.net/distributions/php-7.4.5.tar.gz #下载安装包
tar zxvf php-7.4.5.tar.gz
cd php-7.4.5
./configure --prefix=/lnmp/php7 --enable-fpm --with-mysqli --with-curl --with-pdo_mysql --with-pdo_sqlite --enable-mysqlnd --enable-mbstring --with-gd
make
make install
cp php.ini-development /lnmp/php7/lib/php.ini
重命名:
cd /lnmp/php7/etc
mv php-fpm.conf.default php-fpm.conf
mv php-fpm.d/www.conf.default php-fpm.d/www.conf
cp /lnmp/php7/sbin/php-fpm /usr/bin/
修改配置文件php.ini(vim /lnmp/php7/lib/php.ini)
cgi.fix_pathinfo=0
php-fpm #开启
killall php-fpm #关闭
配置nginx,支持php
vi /lnmp/nginx/conf/nginx.conf
将上面的location ~ .php$ {...} : fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
替换为: fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
查看nginx配置是否正确:nignx -t
#保存重启nginx
nginx -s reload
4.安装mysql
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz
tar -Jxf mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz
#移到lnmp目录,重命名mysql
mv mysql-8.0.18-linux-glibc2.12-x86_64 /lnmp/mysql
cd /lnmp/mysql
mkdir data #用于存放数据
#创建mysql用户组和mysql用户
groupadd mysql
useradd -g mysql mysql
#修改mysql目录权限
chown -R mysql.mysql /lnmp/mysql/
#初始化
bin/mysqld --initialize --user=mysql --basedir=/lnmp/mysql --datadir=/lnmp/mysql/data
#初始化后,出现一大串,最后面的为临时密码
xxx xxx xxx A temporary password is generated for root@localhost: mhr#qEJO+7?h
#修改配置文件
vim /etc/my.cnf
datadir=/lnmp/mysql
datadir=/lnmp/mysql/data
socket=/tmp/mysql/mysql.sock
character-set-server=utf8
port=3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[client]
socket
= /tmp/mysql/mysql.sock
default-character-set=utf8
#新建&修改目录权限
mkdir -p /tmp/mysql/
chmod -R 777 /tmp/mysql
chown -R mysql:mysql /tmp/mysql/
#配置mysql服务并设置成开机自启动
cp -a ./support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
#配置全局环境变量
vim /etc/profile
#profile文件中添加 ###
export PATH=$PATH:/lnmp/mysql/bin:/lnmp/mysql/lib
export PATH
###
#让环境变量设置立即生效
source /etc/profile
#启动mysql
service mysqld start
#登录mysql
mysql -uroot -pmhr#qEJO+7?h
#修改密码
alter user 'root'@'localhost' identified by 'root';
需要创建此路径并授权给mysql用户:
mkdir /var/log/mariadb
touch /var/log/mariadb/mariadb.log
chown -R mysql:mysql /var/log/mariadb
若登录出现以下错误:
则在/etc/my.cnf中注释掉: