NGINX
Nginx (engine x) 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 服务器。。 本例演示 CentOS 7 下安装和配置 Nginx 的基本步骤。
添加源 --> 查看
$ sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
$ sudo yum repolist
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
repo id repo name status
base/7/x86_64 CentOS-7 - Base 9,911
extras/7/x86_64 CentOS-7 - Extras 368
nginx/x86_64 nginx repo 108
updates/7/x86_64 CentOS-7 - Updates 1,041
repolist: 11,428
安装
$ sudo yum install nginx
配置
设置开机启动
$ sudo systemctl enable nginx
启动服务
$ sudo systemctl start nginx
停止服务
$ sudo systemctl restart nginx
重新加载
一般重新配置之后,不希望重启服务,这时可以使用重新加载。
$ sudo systemctl reload nginx
防火墙
默认 CentOS7 使用的防火墙 firewalld 是关闭 http 服务的(打开 80 端口)。
$ sudo firewall-cmd --zone=public --permanent --add-service=http
success
$ sudo firewall-cmd --reload
success
打开之后,可以查看一下防火墙打开的所有的服务
$ sudo firewall-cmd --list-service
ssh dhcpv6-client http
可以看到,系统已经打开了 http 服务。
效果图
可能出现的问题
报 [emerg] bind() to 0.0.0.0:XXXX failed (13: Permission denied)错误的处理方式
端口小于1024
原因是1024以下端口启动时需要root权限,所以sudo nginx即可。
端口大于1024
-
查看http允许访问的端口
$ semanage port -l | grep http_port_t
-
将要启动的端口加入到如上端口列表中
$ semanage port -a -t http_port_t -p tcp 8090
403 Forbidden
一般是四种原因:
- SELinux没有关闭
- Nginx启动用户和工作用户不一致
- 网页所在的目录权限不对
- 缺少默认的首页
SELinux没有关闭
1.1 临时关闭SELinux,但是重启操作系统还会开启
setenforce=0
1.2 永久关闭SELinux
vim /etc/selinux/config
将SELINUX=enforcing 修改为 SELINUX=disabled 状态
Nginx启动用户和工作用户不一致
查看启动nginx 用户
ps aux | grep "nginx: worker process" | awk '{print $1}'
nobody
root
修改nginx 配置文件
vim /etc/nginx/nginx.conf
将 user nobody; 修改为 user root; 重启Nginx
注意:Nginx的启动用户和工作用户可以不一致,但是要配好网页目录的权限,让工作用户有访问网页目录的权限
网页所在的目录权限不对
3.1 精细控制:网页根目录要用x权限(也就是可以cd进去),网页所在的父级目录要有r(可读权限)
3.2 简单粗暴(不推荐,不安全,但是效果明显):
chmod -R 777 /web
chmod -R 777 /web/www
缺少默认的首页
权限配完了,访问首页还显示403 Forbidden?
网页根目录提供一个默认的首页: index.html
访问报错:No input file specified
根本原因,是nginx配置不正确,导致CGI获取参数错误。简单来说,是因为配置文件中
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
的$document_root
这个变量未定义。
分析:
Nginx的配置文件中,有三级关系,http > server > location。
如果要在location中使用document_root,需要在上层server块或http块中定义了root指令,这样才能通过继承关系,
在location中用document_root拿到root的值。而定义在别的location中的root指令值,是一个局部变量,它的值无法在匹配php的这个location中被获取。
解决:
把“/” location中的 root 往上提到 server 块中。
或者,
在php的location块中重新定义 root。
recv() failed (104: Connection reset by peer) while reading response header from upstream
- 修改配置文件 /etc/php-fpm.d/www.conf
- listen = 127.0.0.1:9000 改成 listen = 9000
- listen.allowed_clients = 127.0.0.1 注释掉或删掉
MYSQL
yum源的安装
rpm -ivh mysql57-community-release-el7-9.noarch.rpm
安装完成后,就可以使用yum命令安装mysql了:
yum -y install mysql-server
# 启动mysql:
systemctl start mysqld
# 查看mysql状态:
systemctl status mysqld
# 获取mysql的临时密码:
grep 'temporary password' /var/log/mysqld.logx
# 登录mysql:(密码为上一步骤获取的临时密码)
mysql -u root -p(此处不用输入密码,按下回车后会专门要你再输入密码的)
# 登录成功后,做任何操作都会被要求先修改密码
show databases;
# 因为5.7及以上版本的数据库对密码做了强度要求,
# 默认密码的要求必须是大小写字母数字特殊字母的组合且至少要8位长度
ALTER USER 'root'@'localhost' IDENTIFIED BY '新的密码';
扩展
修改密码的强度:
查看密码的规则:
SHOW VARIABLES LIKE 'validate_password%';
关注两个信息即可:
- validate_password_length=8,表示长度最少为8位
- validate_password_policy=MEDIUM,表示密码的安全等级为中
设置密码强度
- set global validate_password_policy=0;–表示将密码安全等级设置为low
- set global validate_password_length=6;–表示将密码长度设置为最小6位
PHP7.3
安装源
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
安装PHP 以及 扩展
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-pecl-swoole php73-php-recode php73-php-snmp php73-php-soap php73-php-xml php73-php-zip
php73 -v #查看版本
systemctl enable php73-php-fpm #开启开机自启
systemctl restart php73-php-fpm #重启
systemctl start php73-php-fpm #启动
systemctl stop php73-php-fpm #关闭
systemctl status php73-php-fpm #检查状态
设置
# 将php73链接到系统环境变量中,就可以使用 php -v
ln -s /opt/remi/php73/root/usr/bin/php /usr/bin/php
# 将 cgi.fix_pathinfo 设置为 0
sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/opt/remi/php73/php.ini
关于 cgi.fix_pathinfo 选项请查看(https://taobig.org/?p=650)
潜在的漏洞风险,关于安全隐患的问题可查看(http://www.laruence.com/2010/05/20/1495.html)
安装更多扩展:
目前只安装了部分 PHP 拓展,更多扩展可见:
$ yum search php73
$ 更新 PHP:
yum update #更新可更新的所有软件,包括PHP
Composer
curl -sS https://getcomposer.org/installer -o composer-setup.php
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
# 更新 composer
sudo composer self-update