CentOS8.1下搭建LEMP环境教程
1. 安装nginx
-
安装epel-release和nginx
sudo dnf install epel-release
sudo dnf install nginx -
启动nginx
sudo systemctl enable nginx.service
sudo systemctl start nginx.service -
防火墙配置
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload -
查看nginx版本号
nginx -v
2. 安装MySQL
-
配置MySQL源
sudo dnf install https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm
-
安装MySQL
sudo dnf install mysql-community-server
-
启动并设置MySQL开机启动
sudo systemctl enable mysqld
sudo systemctl start mysqld -
查看MySQL版本号
mysql -V
3. 安装PHP
-
安装PHP
sudo dnf install php php-fpm php-common php-xmlrpc php-soap php-gd php-mbstring php-mysqlnd php-pdo php-json
-
配置php.ini
sudo vi /etc/php.ini
将date.timezone设置成"Asia/Shanghai"、cgi.fix_pathinfo修改为0、upload_max_filesize、post_max_size修改为20M -
启动PHP-FPM
sudo systemctl enable php-fpm.service
sudo systemctl start php-fpm.service -
查看php版本号
php -v
4. 配置Nginx与PHP-FPM
-
修改Nginx默认配置
sudo vi /etc/nginx/nginx.conf
在http段加入以下内容:
```
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;location / { index index.php index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } 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; }
}
``` -
重启Ngnix
sudo systemctl restart nginx.service
5. 测试
- 在
/usr/share/nginx/html
目录下新建phpinfo.php并输入以下内容
```
```
- 访问http://localhost/phpinfo.php,看是否能够成功显示PHP信息
示例
示例一
假设我们需要部署一个博客网站,我们需要配置nginx虚拟主机,以及安装wordpress,具体步骤如下:
- 创建
/var/www/blog
目录,将wordpress文件解压到该目录下 - 配置nginx虚拟主机
sudo vi /etc/nginx/conf.d/blog.conf
输入以下内容
```
server {
listen 80;
server_name blog;
access_log /var/log/nginx/blog.access.log;
error_log /var/log/nginx/blog.error.log;
root /var/www/blog;
location / {
index index.php index.html index.htm;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~* \.(jpg|jpeg|gif|png|css|js|ico)$ {
expires max;
log_not_found off;
}
}
```
-
启动blog虚拟主机
sudo systemctl reload nginx.service
-
创建wordpress配置文件
sudo cp /var/www/blog/wp-config-sample.php /var/www/blog/wp-config.php
-
配置wordpress
sudo vi /var/www/blog/wp-config.php
修改以下字段:
define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpressuser');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
示例二
假设我们需要部署一个在线商城,我们需要安装Magento2,具体步骤如下:
- 创建
/var/www/magento
目录,将Magento2文件解压到该目录下 - 配置nginx虚拟主机
sudo vi /etc/nginx/conf.d/magento.conf
输入以下内容
```
server {
listen 80;
server_name magento;
access_log /var/log/nginx/magento.access.log;
error_log /var/log/nginx/magento.error.log;
root /var/www/magento/pub;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /pub/ {
location ~ ^/pub/media/(downloadable|customer|import|theme_customization/.*\.xml) {
deny all;
}
alias $MAGE_ROOT/pub/;
add_header X-Frame-Options "SAMEORIGIN";
}
location /static/ {
# Uncomment the following line in production mode
# expires max;
# Remove signature of the static files that is used to overcome the browser cache
location ~ ^/static/version {
rewrite ^/static/(version[^/]+/)?(.*)$ /static/$2 last;
}
location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
add_header Cache-Control "public";
add_header X-Content-Type-Options "nosniff";
add_header X-Frame-Options "SAMEORIGIN";
expires +1y;
if (!-f $request_filename) {
rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
}
}
location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
add_header Cache-Control "no-store";
add_header X-Content-Type-Options "nosniff";
add_header X-Frame-Options "SAMEORIGIN";
if (!-f $request_filename) {
rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
}
}
if (!-f $request_filename) {
rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
}
add_header X-Frame-Options "SAMEORIGIN";
}
location /media/ {
try_files $uri $uri/.
/get.php?$args;
location ~ ^/media/theme_customization/.*\.xml {
deny all;
}
add_header X-Frame-Options "SAMEORIGIN";
}
location /media/customer/ {
deny all;
}
location /media/downloadable/ {
deny all;
}
location /media/import/ {
deny all;
}
location /setup/ {
index index.php;
#add_header X-Frame-Options "SAMEORIGIN";
#add_header X-XSS-Protection "1; mode=block";
# Deny everything but index.php
location ~ ^/setup/index.php$ {
# Allow internal IPs
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
# Deny everything but index.php
location ~ ^/setup/(?!pub/). {
deny all;
}
# Deny everything but index.php
location ~ ^/setup/pub/ {
# Allow internal IPs
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
}
}
location /update/ {
index index.php;
try_files $uri $uri/ /update/index.php?$args;
}
location /deploy/ {
deny all;
return 404;
}
location /var/ {
deny all;
return 404;
}
location /downloader/ {
allow 127.0.0.1;
deny all;
location ~* ^/downloader/(.*)$ {
try_files $uri $uri/ /downloader/index.php?$args;
fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_NAME /downloader/index.php;
fastcgi_param SCRIPT_FILENAME /var/www/magento/downloader/index.php;
include fastcgi_params;
}
location /downloader/cache/ {
allow all;
}
location /downloader/pearlib/cache/ {
allow all;
}
}
}
```
-
启动magento虚拟主机
sudo systemctl reload nginx.service
-
安装Magento2
- 使用浏览器访问http://yourdomain/magento2/setup/
- 安装向导指导你安装Magento2
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:CentOS 8.1下搭建LEMP(Linux+Nginx+MySQL+PHP)环境(教程详解) - Python技术站