在CentOS 8.1下搭建LEMP(Linux Nginx MySQL PHP)环境,我们需要分别安装和配置Nginx、MySQL和PHP,以下是详细的步骤:
安装Nginx
1、我们需要更新系统软件包:
sudo dnf update -y
2、安装EPEL仓库,以便我们能够下载Nginx的RPM包:
sudo dnf install epel-release -y
3、安装Nginx:
sudo dnf install nginx -y
4、启动Nginx服务,并设置为开机自启动:
sudo systemctl start nginx sudo systemctl enable nginx
5、验证Nginx是否安装成功:
curl http://localhost/
安装MySQL
1、安装MySQL官方仓库:
sudo dnf config-manager --add-repo=https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm
2、安装MySQL:
sudo dnf install mysql-community-server -y
3、启动MySQL服务,并设置为开机自启动:
sudo systemctl start mysqld sudo systemctl enable mysqld
4、获取临时密码:
sudo grep 'temporary password' /var/log/mysqld.log
5、登录MySQL,修改临时密码:
mysql -u root -p ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
安装PHP及其扩展
1、安装Remi仓库,以便我们能够下载PHP的RPM包:
sudo yum install https://rpms.remirepo.net/enterprise/remi-release-8.rpm -y
2、启用PHP 8.1 Remi仓库:
sudo yum-config-manager --enable remi-php81
3、安装PHP及其扩展:
sudo yum install php php-fpm php-mysqlnd php-opcache -y
4、修改PHP配置文件,以支持Nginx:
sudo nano /etc/php-fpm.d/www.conf listen = /run/php-fpm/php-fpm.sock; 修改为实际的PHP-FPM套接字路径,listen = /var/run/php-fpm/php-fpm.sock;
5、重启PHP-FPM服务,并设置为开机自启动:
sudo systemctl restart php-fpm sudo systemctl enable php-fpm
配置Nginx以支持PHP应用
1、创建一个新的网站配置文件:
``bashnano /etc/nginx/sites-available/myapp.confhttp{server {listen 80;server_name myapp.local;root /var/www/myapp;index index.php index.html index.htm;location ~ \.php$ {include fastcgi_params;fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}}}2. 创建一个符号链接,将新的网站配置文件添加到
sites-enabled`目录:
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/325677.html