搭建LNMP环境(CentOS 7.4)
1. 安装Nginx
1.1 更新系统
sudo yum update
1.2 安装Nginx
sudo yum install epelrelease sudo yum install nginx
1.3 启动Nginx
sudo systemctl start nginx sudo systemctl enable nginx
2. 安装MySQL
2.1 添加MySQL官方仓库
sudo wget https://dev.mysql.com/get/mysql80communityreleaseel73.noarch.rpm sudo yum localinstall mysql80communityreleaseel73.noarch.rpm
2.2 安装MySQL
sudo yum install mysqlcommunityserver
2.3 启动MySQL
sudo systemctl start mysqld sudo systemctl enable mysqld
2.4 设置MySQL root密码
sudo grep 'temporary password' /var/log/mysqld.log mysql uroot p ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
3. 安装PHP
3.1 安装EPEL和Remi仓库
sudo yum install epelrelease sudo yum install http://rpms.remirepo.net/enterprise/remirelease7.rpm
3.2 安装PHP 7.4
sudo yumconfigmanager enable remiphp74 sudo yum install php phpfpm phpmysqlnd phpopcache phpgd phpxml phpmbstring phpjson
3.3 配置PHPFPM
编辑/etc/phpfpm.d/www.conf
文件,修改以下内容:
user = nginx group = nginx listen = /run/phpfpm/phpfpm.sock
3.4 启动PHPFPM
sudo systemctl start phpfpm sudo systemctl enable phpfpm
4. 配置Nginx支持PHP
编辑/etc/nginx/conf.d/default.conf
文件,修改以下内容:
location ~ .php$ { fastcgi_pass unix:/run/phpfpm/phpfpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
4.1 重启Nginx服务
sudo systemctl restart nginx
至此,LNMP环境已经搭建完成,你可以创建一个简单的PHP文件来测试环境是否正常运行。
下面是一个介绍,展示了在CentOS 7.4和Ubuntu 20.04上搭建LNMP(Linux, Nginx, MySQL/MariaDB, PHP)环境的过程,特别强调PHP 7的手工安装步骤。
步骤 | CentOS 7.4 | Ubuntu 20.04 |
1. 更新系统软件包 | sudo yum update | sudo apt update && sudo apt upgrade |
2. 安装Nginx | sudo yum install epelrelease sudo yum install nginx | sudo apt install nginx |
3. 启动Nginx并设置开机自启 | sudo systemctl start nginx sudo systemctl enable nginx | sudo systemctl start nginx sudo systemctl enable nginx |
4. 安装MySQL/MariaDB | sudo yum install mariadbserver | sudo apt install mysqlserver |
5. 启动MySQL并设置开机自启 | sudo systemctl start mariadb sudo systemctl enable mariadb | sudo systemctl start mysql sudo systemctl enable mysql |
6. 安装PHP | 安装PHP及相关模块sudo yum install php phpfpm phpmysql | 安装PHP及相关模块sudo apt install php7.4fpm php7.4mysql |
7. 配置PHP | 编辑/etc/php.ini ,根据需求调整配置,例如date.timezone = Asia/Shanghai | 编辑/etc/php/7.4/fpm/php.ini ,调整配置 |
8. 启动PHPFPM并设置开机自启 | sudo systemctl start phpfpm sudo systemctl enable phpfpm | sudo systemctl start php7.4fpm sudo systemctl enable php7.4fpm |
9. 配置Nginx与PHPFPM | 创建或编辑站点配置文件,如/etc/nginx/conf.d/default.conf 使用以下配置作为参考: | 创建或编辑站点配置文件,如/etc/nginx/sitesavailable/default 使用以下配置作为参考: |
server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.php index.html index.htm; location / { try_files $uri $uri/ =404; } location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } | server { listen 80; server_name localhost; root /var/www/html; index index.php index.html index.htm; location / { try_files $uri $uri/ =404; } location ~ .php$ { include snippets/fastcgiphp.conf; fastcgi_pass unix:/var/run/php/php7.4fpm.sock; } } | |
10. 重启Nginx | sudo systemctl restart nginx | sudo systemctl restart nginx |
11. 安全设置MySQL | sudo mysql_secure_installation | sudo mysql_secure_installation |
12. 创建数据库和用户 | sudo mysql CREATE DATABASE db_name; CREATE USER 'user_name'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON db_name.* TO 'user_name'@'localhost'; FLUSH PRIVILEGES; | 同上,在MySQL/MariaDB命令行中执行 |
请注意,这些步骤可能需要根据您的具体需求进行调整,比如选择不同的PHP版本或安装额外的PHP扩展,在生产环境中,还需要进行安全配置和性能调优。
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/714135.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复