文章目录
前言
已经在CentOS中安装好了Nginx和PHP。进行Nginx的基本配置和phpMyAdmin的安装。
1.Nginx的配置
1.1Nginx的默认配置
# 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 4096;
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;
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 {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# 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;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
关键的几个配置:
listen
监听端口server_name
服务名_
,默认是对所有的服务名称进行响应。可以改为本机ip;如果本服务作为代理,修改为服务所有计算机的ip。root
服务所在的根目录
1.2为Nginx添加其它服务
现在想要添加一个phpMyAdmin服务
在/etc/nginx/conf.d
目录下添加文件phpmyadmin.conf
server {
listen 8080;
listen [::]:80;
server_name 127.0.0.1;
root /usr/share/nginx/phpmyadmin;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location ~ ^/phpmyadmin/(.+\.php)$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 请根据你的PHP版本调整此路径
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/nginx/phpmyadmin;
}
location ~ /\.ht {
deny all;
}
}
如果使用的是
sudo apt-get install phpmyadmin
安装的phpMyAdmin,则root
路径在Nginx的web根目录下。- 使用TCP/IP套接字时,
fastcgi_pass
修改为:127.0.0.1:9000
- 使用unix套接字时,
fastcgi_pass
修改为php-fpm的路径,目的是让nginx和php-fpm都能访问:/var/run/php/php7.4-fpm.sock;
- 在php-fpm也进行相应的套接字设置,使用unix套接字的作用是不经过网卡,在高并发的时候性能更佳。
- 不允许对
root
目录下的隐藏文件进行访问:
location ~ /\.ht {
deny all;
}
- 记得重新加载nginx服务
nginx -t
systemctl reload nginx
systemctl reatart nginx
2.phpMyAdmin的安装
2.1下载文件
wget https://files.phpmyadmin.net/phpMyAdmin/5.2.1/phpMyAdmin-5.2.1-all-languages.zip
2.2复制到Nginx的web目录下
#解压
unzip phpMyAdmin-5.2.1-all-languages.zip
#重命名
mv phpMyAdmin-5.2.1-all-languages phpmyadmin
#复制 根据添加的服务所指定的位置。可以先复制,然后在Nginx中添加服务
cp -r ./phpmyadmin /usr/share/nginx/html
3.phpMyAdming的报错以及解决办法
- 在phpmyadmin中登陆mysql。在php测试通过,phpmyadmin也能访问,但是仍然报错:
mysqli::real_connect(): (HY000/2002): No such file or directory
将phpMyAdmin的配置文件 config.sample.inc.php 改为 config.inc.php,并将host配置由localhost改为127.0.0.1
这样在本机访问时不会报错,如果想要在其它地方访问phpmyadmin并登陆mysql,把host
配置为本机真实ip。 - 后续添加
总结
在nginx安装完之后,不着急进行服务添加,先测试一下默认服务是否可以正常访问。
在mysql安装完之后,不着急进行下一步,测试一下mysql可不可以在别的机器进行访问。
在php安装完之后,不着急进行下一步配置,测试一下php有没有安装成功。
这样安装其它web服务时才不会报一些莫名奇妙的错误。