# CentOS7中怎么搭建LNMP開發環境
## 一、LNMP環境簡介
LNMP是指由Linux、Nginx、MySQL/MariaDB和PHP組成的動態網站服務器架構,是當前最流行的Web開發環境之一。與傳統的LAMP(Apache)架構相比,Nginx具有更高的并發處理能力和更低的內存消耗。
### 主要組件說明:
- **Linux**:作為操作系統基礎
- **Nginx**:高性能Web服務器
- **MySQL/MariaDB**:關系型數據庫
- **PHP**:服務器端腳本語言
## 二、準備工作
### 1. 系統要求
- CentOS 7.x 64位系統
- 至少1GB內存(建議2GB以上)
- 10GB以上磁盤空間
- root用戶權限或sudo權限
### 2. 更新系統
```bash
yum update -y
yum install -y epel-release
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
yum install -y nginx
systemctl start nginx
systemctl enable nginx
瀏覽器訪問服務器IP地址,應看到Nginx歡迎頁面。
systemctl status nginx # 查看狀態
systemctl restart nginx # 重啟服務
nginx -t # 測試配置文件
yum install -y mariadb-server mariadb
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation
按照提示設置root密碼、移除匿名用戶、禁止root遠程登錄等。
mysql -u root -p
CREATE DATABASE testdb;
GRANT ALL PRIVILEGES ON testdb.* TO 'testuser'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum install -y yum-utils
yum-config-manager --enable remi-php74
yum install -y php php-fpm php-mysqlnd php-opcache php-gd php-xml php-mbstring php-json
編輯/etc/php-fpm.d/www.conf
:
user = nginx
group = nginx
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
systemctl start php-fpm
systemctl enable php-fpm
創建測試文件/usr/share/nginx/html/info.php
:
<?php phpinfo(); ?>
訪問http://服務器IP/info.php
應顯示PHP信息頁面。
編輯/etc/nginx/conf.d/default.conf
:
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /usr/share/nginx/html;
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;
}
}
nginx -t
systemctl restart nginx
yum install -y php-pecl-redis php-pecl-memcached php-zip php-curl php-bcmath
systemctl restart php-fpm
/usr/share/nginx/html/test.php
<?php
$conn = new mysqli('localhost', 'testuser', 'password', 'testdb');
if ($conn->connect_error) {
die("連接失敗: " . $conn->connect_error);
}
echo "MySQL連接成功!";
phpinfo();
?>
瀏覽器訪問http://服務器IP/test.php
,應顯示MySQL連接成功和PHP信息。
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
mv composer.phar /usr/local/bin/composer
yum install -y phpmyadmin
配置Nginx訪問(參考phpMyAdmin官方文檔)
/var/lib/php
目錄權限GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost'
編輯/etc/php.ini
:
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=4000
編輯/etc/my.cnf
:
[mysqld]
innodb_buffer_pool_size = 256M
query_cache_size = 32M
編輯/etc/nginx/nginx.conf
:
worker_processes auto;
worker_connections 1024;
keepalive_timeout 65;
gzip on;
通過以上步驟,我們已在CentOS7上成功搭建了LNMP開發環境。這個環境可以滿足大多數PHP項目的開發需求,后續可以根據具體項目需求安裝額外的擴展或組件。
建議定期更新各組件以獲取安全補?。?/p>
yum update -y
對于生產環境,還需要考慮: 1. 配置防火墻規則 2. 啟用HTTPS 3. 設置定期備份 4. 配置監控系統 “`
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。