nginx配合apache部署PHP项目,安装reids扩展
1. 安装nginx与apache与PHP
yum install nginx
yum install httpd
yum install php php-cli php-fpm php-mysql php-json php-opcache php-mbstring php-xml php-gd php-curl
php -v 查看PHP版本
2.apache与nginx设置开机自启
systemctl enable nginx
systemctl is-enabled nginx
如果输出显示enabled,则表示nginx服务已成功设置为开机自启
systemctl enable httpd.service
如果输出显示enabled,则表示httpd服务已成功设置为开机自启
systemctl is-enabled httpd.service
3.配置apache部署项目
vim /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
#ServerAdmin 111@qq.com //邮箱
DocumentRoot /var/www/html //项目地址
ServerName api.cn //域名
ErrorLog "/var/log/httpd/api_error_%Y%m%d.log"
CustomLog "/var/log/httpd/api_access_%Y%m%d.log" combined
<Directory "/var/www/html/">//项目地址
Options ExecCGI FollowSymLinks
Order allow,deny
Allow from all
AllowOverride all
#Options -MultiViews
#Require all granted
</Directory>
</VirtualHost>
查看配置文件是否正确
httpd -S
返回以下内容表示正常
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 1.1.1.1. Set the 'ServerName' directive globally to suppress this message
VirtualHost configuration:
*:80 api.cn (/etc/httpd/conf/httpd.conf:355)
ServerRoot: "/etc/httpd"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/etc/httpd/logs/error_log"
Mutex proxy: using_defaults
Mutex authn-socache: using_defaults
Mutex default: dir="/run/httpd/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex authdigest-opaque: using_defaults
Mutex proxy-balancer-shm: using_defaults
Mutex rewrite-map: using_defaults
Mutex authdigest-client: using_defaults
PidFile: "/run/httpd/httpd.pid"
Define: _RH_HAS_HTTPPROTOCOLOPTIONS
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="apache" id=48
Group: name="apache" id=48
4.配置nginx映射
vim /etc/nginx/nginx.conf
将此代码注释防止apache与nginx端口冲突
# server {
# listen 80;
# listen [::]:80;
# server_name _;
# root /usr/share/nginx/html;
# Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
# error_page 404 /404.html;
# location = /404.html {
# }
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
}
增加子配置文件
vim /etc/nginx/conf.d/api.cn.conf
server {
listen 443 ssl;
server_name api.cn ;
ssl_certificate /etc/ssl/certs/api.cn.pem;
ssl_certificate_key /etc/ssl/certs/api.cn.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://api.cn/;
proxy_set_header Host "api.cn";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_connect_timeout 300;
#开启跨域
add_header Access-Control-Allow-Origin $http_origin always;
add_header Access-Control-Allow-Methods POST,GET,OPTIONS,DELETE,PUT,HEAD,PATCH;
add_header Access-Control-Allow-Headers $http_access_control_request_headers;
add_header Access-Control-Expose-Headers $http_access_control_request_headers;
add_header Access-Control-Allow-Credentials true always;
client_max_body_size 100M;
if ($request_method = 'OPTIONS') {
return 204;
}
}
#charset koi8-r;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
查看nginx配置文件是否正确
/usr/sbin/nginx -t
正确返回
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
5.重启apache与nginx
systemctl restart httpd
查看nginx位置
whereis nginx
重启nginx
/usr/sbin/nginx -s reload
nginx重启返回错误解决方案
nginx: [error] invalid PID number “” in “/run/nginx.pid”
重新加载配置文件 nginx.conf,然后再进行重启
nginx -c /etc/nginx/nginx.conf
/usr/sbin/nginx -s reload
如存在端口占用情况直接杀死进程
killall nginx
killall httpd
至此整个部署结束即可根据域名访问项目
6.安装PHPredis 4.2版本扩展
wget http://pecl.php.net/get/redis-4.2.0.tgz && tar zxvf redis-4.2.0.tgz; cd redis-4.2.0;phpize;./configure -with-php-config=/usr/bin/php-config;make; make install;cd && echo "extension = redis.so" >> /etc/php.ini
查看redis扩展是否安装成功
php -m | grep redis
//安装扩展后在php.ini写入配置,需要重启apache
systemctl restart httpd