操作系统:CentOS 7.3 64位
安装 Nginx
PHP-FPM 依赖于 Nginx 进行请求的派发与响应,并且一些静态文件我们也可以直接通过 Nginx 代理,提高性能。其中第一步就是安装 Nginx。
通过 SSH 连接上云服务器,直接使用包管理工具 yum
安装 Nginx 即可:
yum -y install nginx
安装完成之后会显示 Complete!
,可以通过如下命令检查 Nginx 是否安装成功:
nginx -v
这个命令会显示 Nginx 的版本号,如果显示如下信息,则安装成功:
安装 PHP
安装PHP 5.6 为例,yum
本身不提供 PHP 的源,所以首先我们得添加 remi 源:
wget 'https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi.repo' -O /etc/yum.repos.d/remi.repo
接着就可以直接通过 yum
安装了:
yum install --enablerepo=remi --enablerepo=remi-php56 php php-mbstring php-mcrypt php-mysql php-curl php-fpm
同理,我们可以通过如下命令验证 PHP 是否安装成功:
php -v
该命令会返回当前 PHP 的版本号,如果你看到了版本号大于 5.6,则 PHP 安装成功:
开启 SFTP
SFTP 是一种安全的文件传输协议,我们可以通过 SFTP 把本地的文件上传到服务器上,通过以下命令检查 sftp 状态:
service sshd status
看到输出的信息中有 active (running)
则表示 sshd
进程已经开启,可以通过 sftp 连接:
接下来可以通过 FileZilla、Transmit 等 FTP 工具连接上服务器。
配置 Nginx
user ftp;
worker_processes 2;
#error_log /dev/null;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid conf/nginx.pid;
worker_rlimit_nofile 1024;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
include proxy.conf;
log_format yundns_log '$server_name $remote_addr [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
#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 logs/access.log ;
server_tokens off;
sendfile on;
tcp_nopush on;
server_names_hash_bucket_size 256;
client_header_buffer_size 256k;
#large_client_header_buffers 4 32k;
large_client_header_buffers 4 256k;
client_body_buffer_size 256k;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
client_max_body_size 50m;
keepalive_timeout 120;
fastcgi_intercept_errors on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
#fastcgi_buffer_size 64k;
#fastcgi_buffers 8 64k;
#fastcgi_busy_buffers_size 128k;
#fastcgi_temp_file_write_size 128k;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
error_log logs/error.log;
access_log logs/host.access.log ;
location / {
root 项目根目录;
index index.html index.htm index.php;
}
#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 html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {x
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root 你的项目根目录;
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;
#}
}
server {
listen 8081 default_server;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
index index.php index.html;
root /usr/local/nginx/html/default;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:10000;
include fastcgi.conf;
}
location ~ \.key$ {
return 404;
}
#location / {
# root 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 html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# 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;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include vhost.conf;
}
nginx -t
如果显示如下信息,则配置成功:
配置成功之后,输入 nginx
回车,即可启动 Nginx。
此时通过配置的域名访问服务器,会显示 404 Not Found
,则表示配置成功:
启动 PHP
回到 SSH 界面,输入:
service php-fpm start