1.一键安装所有编译工具 gcc
yum groupinstall "Development Tools" "Server Platform Development" -y
2.Yum添加 Epel源
wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -ivh epel-release-latest-7.noarch.rpm
yum repolist ##检查是否已添加至源列表
3.安装Nginx
yum -y install nginx 使用yum安装nginx
systemctl start nginx 启动nginx,浏览器输入ip就可以看到nginx的欢迎页
以上步骤都不能安装后,建议查看http://nginx.org/en/linux_packages.html#RHEL-CentOS
3.1修改Nginx配置文件以支持PHP解析
nginx配置文件默认放在/etc/nginx/nginx.conf
vi /etc/nginx/nginx.conf
在server区间里加入以下内容
注释掉本来的这两行
# location / {
# }
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
systemctl restart nginx #重启nginx
默认的nginx页面地址在
/usr/share/nginx/html
配置文件的目录
/etc/nginx
4.安装PHP
检查当前安装的PHP包
yum list installed | grep php
如果有安装的PHP包,先删除他们
(这条命令看情况执行看清楚你安装的包用yum remove删除)
yum remove php.x86_64 php-cli.x86_64 php-common.x86_64 php-gd.x86_64 php-ldap.x86_64 php-mbstring.x86_64 php-mcrypt.x86_643
4.1 添加PHP的yum源
Centos 5.X
rpm -Uvh http://mirror.webtatic.com/yum/el5/latest.rpm
CentOs 6.x
rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
CentOs 7.X
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
或者使用wget
wget https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -ivh epel-release.rpm
注意如果安装的时候失败,后面添加两个参数 --nodeps --force,如下所示
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm --nodeps --force
如果想删除上面安装的Yum源包,重新安装
rpm -qa | grep webstatic
rpm -e #上面搜索到的包即可
4.2自行选择要安装什么版本的PHP
php5.6
yum install php56w.x86_64 php56w-cli.x86_64 php56w-common.x86_64 php56w-gd.x86_64 php56w-ldap.x86_64 php56w-mbstring.x86_64 php56w-mcrypt.x86_64 php56w-mysql.x86_64 php56w-pdo.x86_64
php5.5
yum install php55w.x86_64 php55w-cli.x86_64 php55w-common.x86_64 php55w-gd.x86_64 php55w-ldap.x86_64 php55w-mbstring.x86_64 php55w-mcrypt.x86_64 php55w-mysql.x86_64 php55w-pdo.x86_64
php7
yum install php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-mysql.x86_64 php70w-pdo.x86_64
4.3安装PHP-FPM
5.5
yum install php55w-fpm
5.6
yum install php56w-fpm
7.0
yum install php70w-fpm
4.4启动php-fpm
systemctl start php-fpm
4.5配置php.ini
vi /etc/php.ini
按下esc进入命令模式,输入:/cgi.fix_pathinfo,按n
进行下一个查找,找到指定cgi.fix_pathinfo, 修改为=0;
5.mysql安装
6.如果有防火墙 要开放80和3306端口
centos7用的是firewall
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=3306/tcp --permanent
6.1重新加载防火墙
firewall-cmd --reload
7.检查php版本
php -v
8.重启apach或者nginx
Apache:
[root@localhost ~]# systemctl restart httpd.service
ngingx:
[root@localhost ~]# systemctl restart nginx.service
9.常用命令
启动nginx
systemctl start nginx
查看nginx运行状态
systemctl status nginx
查看nginx进程
ps aux | grep nginx
语法检测
nginx -t
重启
systemctl reload nginx
查看端口
netstat -anlpt | grep 80
10.防火墙,nginx启动后用ip去访问如果无法访问就是和防火墙有关系
10.1使用命令查看
systemctl status firewalld
10.2.开启防火墙
systemctl start firewalld.service
10.3.重启防火墙
systemctl restart firewalld.service
10.4.查看firewall防火墙的状态
firewall-cmd --state
10.5.查看防火墙开放端口规则
firewall-cmd --list-port
10.6.查看80端口是否开启
firewall-cmd --query-port=80/tcp
10.7.开放80端口
firewall-cmd --permanent --add-port=80/tcp #(–permanent永久生效,没有此参数重启后就失效)
10.8.加载生效开放的端口
每次修改防火墙,或者修改端口都需要用下面的命令重启防火墙
firewall-cmd --reload
10.9.查询指定端口80是否开放
firewall-cmd --query-port=80/tcp
11.测试php环境是否运行正常
进入到项目文件目录
cd /usr/share/nginx/html
创建index.php文件
touch index.php
编写index.php文件
vi index.php
输入代码
<?php
echo phpinfo();
?>
11.多项目的配置
nginx.conf(文件的地址/etc/nginx)的配置代码参考,这里只是在server中添加了代码(步骤:3.1修改Nginx配置文件以支持PHP解析有提到),所以不用覆盖掉你的配置文件
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
参考代码
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
#location / {
#}
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /404.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
然后在/etc/nginx/conf.d文件中创建你的配置文件,这个配置文件的所在目录,要看nginx.conf中的配置 include /etc/nginx/conf.d/*.conf;这里的地址指向那个路径就在那个路径下面创建配置文件,文件的名称自定义但是后缀必须是conf代码如下所示
server {
listen 80;#端口
server_name ceshi.yhjyx.top;#域名
root /www/ceshi;#项目地址
index index.php index.html;
access_log off;
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/ceshi$fastcgi_script_name;#项目地址
include fastcgi_params;
}
}
以上的配置的访问方式是:http://www.jsbke.cn/index.php?s=index/index/index
server {
listen 80;
server_name tpmodel1.yhjyx.top;
root "/www/tp/model1/model1/public";
location / {
index index.php index.html error/index.html;
autoindex off;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
以上的配置的访问方式是:http://www.jsbke.cn/index.php?s=index/index/index和http://www.jsbke.cn/index.php/index/index/index
12.为项目配置伪静态
这里实例以tp5项目为例(tp的伪静态一般放在public文件夹下,文件路径可以自定义,但是为了安全起见最好还是放到公共文件public下)
12.1在网站的配置文件中引入伪静态
server {
listen 80;
server_name www.jsbke.cn;
root "/www/tp/model1/model1/public";
location / {
index index.php index.html error/index.html;
include /www/tp/model1/model1/public/nginx.htaccess;#引入伪静态
autoindex off;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
12.2在public文件下创建nginx.htaccess,并且加入伪静态代码
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}
13.为项目配置ssl证书
13.1配置文件中配置
配置文件中添加配置文件
ssl_certificate /www/ssl/tpmodel1/5641393_tpmodel1.yhjyx.top.pem; # 指定证书的位置,绝对路径
ssl_certificate_key /www/ssl/tpmodel1/5641393_tpmodel1.yhjyx.top.key; # 绝对路径,同上
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
ssl_prefer_server_ciphers on;
设置端口和域名
listen 443 ssl;
server_name tpmodel1.yhjyx.cn;
全部配置
server {
listen 443 ssl;
server_name tpmodel1.yhjyx.cn;
ssl_certificate /www/ssl/tpmodel1/5641393_tpmodel1.yhjyx.top.pem; # 指定证书的位置,绝对路径
ssl_certificate_key /www/ssl/tpmodel1/5641393_tpmodel1.yhjyx.top.key; # 绝对路径,同上
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
ssl_prefer_server_ciphers on;
root "/www/tp/model1/model1/public";
location / {
index index.php index.html error/index.html;
include /www/tp/model1/model1/public/nginx.htaccess;
autoindex off;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
13.2把申请的ssl证书放到自定义的文件目录中 /www/ssl/tpmodel1/并配置
ssl_certificate /www/ssl/tpmodel1/5641393_tpmodel1.yhjyx.top.pem; # 指定证书的位置,绝对路径
ssl_certificate_key /www/ssl/tpmodel1/5641393_tpmodel1.yhjyx.top.key; # 绝对路径,同上
14.更多教程(阿里云开发者社区是个不错的开发者的社区)
https://developer.aliyun.com/article/626490?spm=a2c6h.14164896.0.0.356e52a1bI01Qb
https://developer.aliyun.com/article/690483?spm=a2c6h.14164896.0.0.356e52a1bI01Qb