yum 搭建lnmp 环境
2021-12-03 本文已影响0人
王宣成
环境
系统 CentOS 7.6 、php 7.2 、mysql8.0、swoole 4.4 扩展、redis 扩展
nginx安装
# 下载nginx
wget http://nginx.org/download/nginx-1.20.2.tar.gz
#解压
tar -zxvf nginx-1.20.2.tar.gz
cd nginx-1.20.2
# 取消debug编译模式安装更快
vim auto/cc/gcc
找到这行
# debug
CFLAGS="$CFLAGS -g"
注释掉
# debug
#CFLAGS="$CFLAGS -g"
安装依赖并创建nginx启动用户
yum install wget gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel
useradd -s /bin/false -M www
编译
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-pcre --with-http_gzip_static_module --with-http_dav_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module
make && make install
设置软连接
ln -sv /usr/local/nginx/sbin/nginx /usr/local/sbin/
设置开机启动
vim /usr/lib/systemd/system/nginx.service
# nginx.service内容
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl start nginx
systemctl enable nginx
打开浏览器输入服务器公网ip访问 , 出现 Welcome to nginx! 安装成功
# 查看版本
nginx -v
mysql安装
cd /usr/local
mkdir mysql
cd mysql
wget https://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm
# 安装 yum repo文件并更新 yum 缓存
rpm -ivh mysql80-community-release-el7-1.noarch.rpm
# 更新yum命令
yum clean all
yum makecache
# 查看mysql yum仓库中mysql版本
yum repolist all | grep mysql
# 使用 yum-config-manager 命令修改相应的版本为启用状态最新版本为禁用状态,根据需要安装的版本修改,当前默认是8.0不用处理
# yum-config-manager --disable mysql80-community #关闭8.0版本
# yum-config-manager --enable mysql57-community #开启5.7版本
# 安装
yum install mysql-community-server
# 设置开机启动
systemctl enable mysqld.service
systemctl start mysqld.service
# 查看初始密码
cat /var/log/mysqld.log | grep password
# 重置密码,注意位数和种类至少大+写+小写+符号+数字
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
忘记密码重置
vim /etc/my.cnf
# 在【mysqld】模块添加
skip-grant-tables
# 重启mysql服务:
service mysqld restart
# 将旧密码置空 提示输入密码时直接敲回车。
mysql -u root -p
# 选择数据库
use mysql
# 将密码置空
update user set authentication_string = '' where user = 'root';
# 退出
quit
# 删掉步骤1的语句 skip-grant-tables
# 重启服务
service mysqld restart
# 提示密码直接回车
mysql -u root -p
# 修改密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '密码';
常用命令
# 登录mysql
mysql -u username -p
# 退出mysql
quit
#启动mysql
systemctl start mysqld.service
#结束
systemctl stop mysqld.service
#重启
systemctl restart mysqld.service
#开机自启
systemctl enable mysqld.service
#查看mysql版本
select version();
#立即生效
flush privileges;
phpmyadmin登录问题 mysqli::real_connect(): The server requested authentication method unknown to the client [caching_sha2_password]
vim /etc/my.cnf
# 修改
default_authentication_plugin=mysql_native_password
# 连接mysql
use mysql
# 修改密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
# 刷新
FLUSH PRIVILEGES;
php安装
# 安装源
# 安装 EPEL 软件包:
sudo yum install epel-release
# 安装 remi 源:
sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
# 安装 yum 扩展包:
sudo yum install yum-utils
# 启用 remi 仓库:
sudo yum-config-manager --enable remi-php72 $ sudo yum update
# 安装 PHP7,2
sudo yum install php72
# 查看安装结果
php72 -v
# 安装 php-fpm 和一些其他模块
sudo yum install php72-php-fpm php72-php-gd php72-php-json php72-php-mbstring php72-php-mysqlnd php72-php-xml php72-php-xmlrpc php72-php-opcache php72-php-devel php72-php-pear
# 设置开机自启
sudo systemctl enable php72-php-fpm.service
# 开启服务
sudo systemctl start php72-php-fpm.service
# 停止服务
sudo systemctl stop php72-php-fpm.service
# 重启服务
sudo systemctl restart php72-php-fpm.service
# 查看状态
sudo systemctl status php72-php-fpm.service
cd /usr/local/nginx/html
vim phpinfo.php
# phpinfo.php
<?php
echo phpinfo();
修改nginx配置
cd /usr/local/nginx/conf
vim nginx.conf
# nginx.conf 找到修改如下
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# 重启nginx
systemctl restart nginx
#访问 http://服务器ip地址/phpinfo.php
安装swoole扩展(手动)
# 下载 http://pecl.php.net/package/redis
wget http://pecl.php.net/get/swoole-4.4.26.tgz
# 解压
tar zxvf swoole-4.4.26.tgz
cd swoole-4.4.26
# 用phpize生成
/opt/remi/php72/root/usr/bin/phpize
# 进行配置
./configure --with-php-config=/opt/remi/php72/root/usr/bin/php-config
# 编译安装
make && make install
#报错 error: '__builtin_ssubl_overflow' was not declared in this scope。 先升级gcc版本
sudo yum install centos-release-scl
sudo yum install devtoolset-7
scl enable devtoolset-7 bash
#需要注意的是scl命令启用只是临时的,退出shell或重启就会恢复原系统gcc版本。
#如果要长期使用的话:
echo "source /opt/rh/devtoolset-7/enable" >>/etc/profile
#这样退出shell重新打开就是新版的gcc了
#其他版本同理,修改devtoolset版本号即可
# 再执行 make && make install
# 在PHP配置文件php.ini里面加载swoole扩展
cd /etc/opt/remi/php72/php.d/
vim 40-swoole.ini
# 40-swoole.ini
; Enable swoole extension module
extension=swoole.so
; Configuration
;swoole.enable_coroutine = On
;swoole.enable_library = On
;swoole.enable_preemptive_scheduler = Off
;swoole.display_errors = On
:swoole.use_shortname = On
;swoole.unixsock_buffer_size = 8388608
# 重启 sudo systemctl stop php72-php-fpm.service
安装redis扩展
# 搜索扩展
yum search php72 | grep redis
# /etc/opt/remi/php72/php.d/50-redis.ini
# 安装后重启服务 phpinfo查看是否安装成功
sudo yum install php72-php-pecl-redis
#redis.so 扩展存放在 /opt/remi/php72/root/usr/lib64/php/modules/
# 安装swoole4版本扩展
# sudo yum install php72-php-pecl-swoole4
redis安装
wget http://download.redis.io/releases/redis-6.0.8.tar.gz
tar xzf redis-6.0.8.tar.gz
cd redis-6.0.8
make
#启动
cd src
./redis-server
# 使用指定配置文件启动
./redis-server ../redis.conf
redis配置修改
vim redis.conf
#设置密码
requirepass 123456
# 守护进程方式启动
daemonize yes
#允许远程连接
bind 0.0.0.0
redis自启
vim /etc/init.d/redis
chmod +x /etc/init.d/redis
# 启动
/etc/init.d/redis start | stop
# 加入开机自启服务
chkconfig --add redis
# 开启服务自启动
chkconfig redis on
# 开机启动项列表
chkconfig --list
/etc/init.d/redis
#!/bin/sh
# chkconfig: 2345 90 10
# description: Start and Stop redis
PATH=/usr/local/bin:/sbin:/usr/bin:/bin #找到本机安装redis后,存放redis命令的目录
REDISPORT=6379 #redis的默认端口, 要和下文中的redis.conf中一致
EXEC=/www/redis-6.0.8/src/redis-server #redis服务端的命令
REDIS_CLI=/www/redis-6.0.8/src/redis-cli #redis客户端的命令 这两个一般都在 PATH目录下
PIDFILE=/var/run/redis_6379.pid #reids的进程文件生成的位置
CONF="/www/redis-6.0.8/redis.conf" #redis的配置文件所在的目录
PASS=123456 #redis密码
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed."
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE exists, process is not running."
else
PID=$(cat $PIDFILE)
echo "Stopping..."
$REDIS_CLI -p $REDISPORT -a $PASS SHUTDOWN
sleep 2
while [ -x $PIDFILE ]
do
echo "Waiting for Redis to shutdown..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac