centos安装lnmp
安装 LNMP(Linux + Nginx + MySQL + PHP)环境在 Centos 7 上,您可以按照以下步骤进行操作:
更新系统和软件包:
sudo yum update
安装必要的软件包:
sudo yum install epel-release sudo yum install wget vim unzip
安装 Nginx:
sudo yum install nginx sudo systemctl start nginx sudo systemctl enable nginx
安装 MySQL:
sudo yum install mariadb-server mariadb sudo systemctl start mariadb sudo systemctl enable mariadb sudo mysql_secure_installation
这将引导您设置 MySQL 的 root 用户密码以及其他安全选项。按照提示进行操作。
安装 PHP:
sudo yum install php php-fpm php-mysql sudo systemctl start php-fpm sudo systemctl enable php-fpm
配置 Nginx 配置文件:
sudo vim /etc/nginx/conf.d/default.conf
编辑配置文件并添加以下内容:
server { listen 80; server_name your_domain; root /var/www/html; index index.php index.html; location / { try_files $uri $uri/ =404; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
替换
your_domain
为您实际的域名。重启 Nginx:
sudo systemctl restart nginx
现在,您的 LNMP 环境应该已经安装完成了。您可以在 /var/www/html
目录下创建您的网站文件,并通过浏览器访问您的域名来测试是否正常运行。
请注意,上述步骤仅为基本安装配置,您可能需要根据您的具体需求进行进一步的配置和优化。
一定要注意nginx配置文件中的fastcgi_pass 要和php-fpm配置文件中的listen保持一致
如果您的 PHP-FPM 配置文件指示 PHP-FPM 监听 127.0.0.1:9000 地址和端口。
在这种情况下,您需要在 Nginx 配置文件中的 fastcgi_pass 行中使用 TCP 套接字指定 PHP-FPM 的地址和端口。例如:
fastcgi_pass 127.0.0.1:9000;
请确保将此行精确添加到您的 Nginx 配置文件中,并确保没有出现任何错误的注释或其他字符。
完成后,通过重新启动 Nginx 和 PHP-FPM 服务以使更改生效:
sudo systemctl restart nginx
sudo systemctl restart php-fpm
然后,再次打开您的网站并查看是否仍然出现错误页面。如果问题仍然存在,请查看错误日志以获取更多信息进行故障排除。
如果嫌安装的php版本太低
要在 CentOS 系统上升级 PHP 版本到 7.4,您可以按照以下步骤进行操作:
添加 Remi 存储库:
sudo yum install epel-release sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
启用 Remi 存储库:
sudo yum-config-manager --enable remi-php74
安装 PHP 7.4 及其相关扩展:
sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysqlnd
验证 PHP 版本:
运行以下命令确认新安装的 PHP 版本:
php -v
应该显示 PHP 7.4 版本的信息。
完成这些步骤后,您的 CentOS 系统应该已经成功升级到 PHP 7.4。确保测试您的网站以确保一切正常运行,并根据您的需要进行必要的配置更改。
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: