Centos 7.3 搭建 LNMP 环境 yum安装
安装nginx
环境准备
服务器系统以及软件升级命令//可忽略
yum -y update
CentOS 系列的服务器系统有一个毛病,就是官方自带的源的软件比较古老,并且很多的软件都没有。因为他们的首要任务是保证服务器的稳定,而不是追求最新。但是太过于保守了,一般来说,我们会给服务器添加一个 epel-release 这个源。这个源里包含了例如 nginx 之类的我们需要的软件,使用起来比较方便。
安装 epel-release
yum install epel-release -y
通过上面的命令进行安装。确认是否安装成功,可以用下面的命令检测一下
yum search nginx
如果搜索的结果包含下面的这行内容,就表示安装成功了,然后我们就能愉快的安装我们需要的软件了。
nginx.x86_64 : A high performance web server and reverse proxy server
开始安装:
安装 nginx
yum install nginx -y
修改Nginx配置文件
vim /etc/nginx/conf.d/test.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
root /var/www/html/;
index index.html index.htm index.php;
#charset koi8-r;
access_log /var/log/nginx/host.access.log main;
location / {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
查看本机SELinux的开启状态,如果SELinux status参数为enabled即为开启状态
/usr/sbin/sestatus -v
vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
启动 nginx
systemctl start nginx
注意:修改配置文件需要重启系统 reboot
将 nginx 设置为开机启动
systemctl enable nginx
CentOS7防火墙没有开启端口80,开启的方法为
[root@localhost sysconfig]# firewall-cmd --permanent --add-port=80/tcp
success
[root@localhost sysconfig]# firewall-cmd --reload
success
好,通过上面三条命令执行之后,应该可以在浏览器中直接用服务器IP可以访问到 nginx 默认的首页了。
安装PHP7.3
安装php7.3.20,是需要配置额外的yum源地址的,否则会报错不能找到相关软件包。
php高版本的yum源地址,有两部分,其中一部分是epel-release,另外一部分来自webtatic。如果跳过epel-release的话,安装webtatic的时候,会有错误爆出
yum install epel-release -y
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum -y remove php*
执行下面的命令,安装 PHP 已经它的常用的库
yum --enablerepo=remi-php73 install php -y
yum install -y php73-php-fpm php73-php-cli php73-php-bcmath php73-php-gd php73-php-json php73-php-mbstring php73-php-mcrypt php73-php-mysqlnd php73-php-opcache php73-php-pdo php73-php-pecl-crypto php73-php-pecl-mcrypt php73-php-pecl-geoip php73-php-recode php73-php-snmp php73-php-soap php73-php-xmll
php73-php-devel
修改 /etc/opt/remi/php73/php-fpm.d/www.conf 文件
/24 user = nginx
/26 group = nginx
/48 listen.owner = nginx
/49 listen.group = nginx
启动PHP并将它设置为开机启动
systemctl enable php73-php-fpm
systemctl start php73-php-fpm
配置nginx使其支持php
首先,我们打开 nginx 的配置文件
vim /etc/nginx/nginx.conf
然后在 server 这一段的花括号中,添加如下内容:
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
另外,还需要配置默认的首页文件,我们找到 index index.html index.htm; 这段配置,在中间添加 index.php 。如下所示:
index index.php index.html index.htm;
好,经过这样的简单配置,我们的任务就已经完成了。
重启 nginx 服务
systemctl restart nginx
至此已经可以解析PHP代码了
安装 MySQL(MariaDB)
php 的最佳拍档 mysql 我们还没有安装。这里,我们需要注意的是,自从 mysql 被收购之后,我们就不使用了,而是使用一个叫 mariadb 的从 mysql 发展而来的数据库,完全兼容。
除了名字不一样,哪哪就兼容。
# 安装 mariadb
yum install mariadb-server mariadb -y
启动 mariadb
systemctl start mariadb
将 mariadb 设置为开机启动
systemctl enable mariadb
默认情况下,数据库的密码为空,我们需要设置一下,运行下面的命令:
mysql_secure_installation
Enter current password for root (enter for none): # 输入数据库超级管理员root的密码(注意不是系统root的密码),第一次进入还没有设置密码则直接回车
Set root password? [Y/n] # 设置密码,y
New password: # 新密码
Re-enter new password: # 再次输入密码
Remove anonymous users? [Y/n] # 移除匿名用户, y
Disallow root login remotely? [Y/n] # 拒绝root远程登录,n,不管y/n,都会拒绝root远程登录
Remove test database and access to it? [Y/n] # 删除test数据库,y:删除。n:不删除,数据库中会有一个test数据库,一般不需要
Reload privilege tables now? [Y/n] # 重新加载权限表,y。或者重启服务也许
运行这个命令之后,根据提示进行相应的设置。一般情况下,就是不断的回车,以及输入你的密码,确认密码,然后一路回车即可。
登陆一下mysql
mysql -u root -p
安装redis
查找文件
whereis (查找文件名)
安装nginx
加载插件
yum install epel-release -y
安装redis
yum install redis -y
启动redis
systemctl start redis
设置为开机启动
systemctl enable redis
在命令行中输入 redis-cli, 然后执行ping命令,如果返回的结果是PONG,则说明你的redis安装成功了。
安装phpredis扩展
1.确保php已经成功安装
#检查php版本#
php -v
2.安装编译工具
#安装编译工具#
yum install wget make gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel kernel keyutils patch perl -y
3.安装phpredis扩展
复制代码
#下载扩展包#
yum -y install git
cd /usr/local/src
git clone https://github.com/nicolasff/phpredis
#解压#
cd phpredis/
#用phpize生成configure配置文件 如果phpize报错,安装php-devel# phpize报错执行 yum install php-devel
/usr/bin/phpize
./configure --with-php-config=/usr/bin/php-config
make && make install
安装完成之后,会出现nstalling shared extensions: /usr/lib64/php/modules/
cd /etc/php.d
vim redis.in
添加extension=redis.so
重启apache或者nginx php-fpm
systemctl restart php-fpm
4.测试是否安装成功
访问phpinfo.php文件
若info拓展里面有Rdeis,无法连接关闭selinux
在生产环境下,selinux有时太严格了,权限要求方面,因此经常要关闭,
关闭方法:
1)、修改/etc/selinux/config文件中的SELINUX="" 为 disabled ,即SELINUX=“disabled”;
2)、执行命令:setenforce 0。
编译安装Rdeis
http://www.linuxidc.com/Linux/2017-04/143210.htm 安装PHP拓展时与上面相同