CentOS7使用yum安装最新版本LNMP
介绍
yum是CentOS、RedHat系列linux系统的包管理工具,用其来进行软件包的安装,可以自动安装依赖,方便快捷。但是,系统默认的yum源里面的软件包往往版本较低,15年底php7已经发布了,但是现在,yum源里的版本还是5.4。 本篇文章就来介绍如何使用yum来安装最新版的LNMP。
安装nginx
第一步,把包含最新版nginx的yum源添加进去,下面是nginx官网提供的方法
运行命令
vim /etc/yum.repos.d/nginx.repo
新建一个yum源文件 ,里面添加内容
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1
要注意, 这里面的
centos
是操作系统名字,要替换程你的操作系统如rhel
,7
是操作系统版本号,如果你是centos6,就是替换成6
第二步,安装
运行命令yum install nginx
第三步,启动nginx并设置开机自启动
运行命令systemctl start nginx systemctl enable nginx
验证是否安装成功*
可以通过浏览器,访问http://服务器ip
访问也可以使用命令
net -tunlp | grep 80
看80端口是否在监听,终端有内容输出,证明正在监听
安装PHP
这里我们安装目前最新的稳定版php71
第一步 添加包含最新版PHP的yum源
这次不用我们手动添加.repo
文件了,运行命令自动添加 yum源文件rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
第二步 安装PHP以及相关扩展
运行命令yum install php71w-common php71w-fpm php71w-opcache php71w-gd php71w-mysqlnd php71w-mbstring php71w-pecl-redis php71w-pecl-memcached php71w-devel
查看是否安装成功
运行命令php -v
关联nginx和PHP
第一步 启动php-fpm 并设置开机自启动
安装php的时候我们已经顺带安装了php-fpm,我们启动就可以了systemctl start php-fpm systemctl enable php-fpm
第二步 检查php-fpm是否启动成功
php-fpm默认监听9000端口
运行命令netstat -tunlp | grep 9000
第三步 修改nginx配置文件
vim /ect/nginx/conf.d/default.conf
打开文件,把文件内容替换成下面内容server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /usr/share/nginx/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
第四步 验证是否成功
从上面的配置文件中,我们可以看到一个root
选项, 值是/usr/share/nginx/html
, 改目录就是nginx的web根目录,网站的代码就要放在这个目录中,当然你也可以修改成你指定的目录。注意location /
和location ~ \.php$
都有一个root
选项进入到nginx的根目录, 创建文件phpinfo.php, 写入内容
<?php phpinfo();
重启nginx,运行命令
systemctl restart nginx
浏览器内访问 http://ip地址/phpinfo.php
安装MySQL
这里,我们安装的是MySQL5.7
第一步,添加包含最新版mysql的yum源
运行命令wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm yum localinstall mysql57-community-release-el7-8.noarch.rpm
第二步,安装
运行命令yum install mysql-community-server
第三步,启动MySQL并设置开机自启动
运行命令systemctl start mysqld systemctl enable mysqld
第三部,登录MySQL并更改密码
mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码,然后登录mysql进行修改grep 'temporary password' /var/log/mysqld.log
登录mysql
mysql -u root -p
根据命令输入密码,登录mysql后
运行SQLset password for 'root'@'localhost'=password('新密码');
需要注意的是,mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
错误