在Ubuntu上搭建Linux Nginx MySQL PHP网站运行环境
LNMP,即Linux Nginx MySQL PHP,是搭建动态网站的一套常用环境。还有一种叫法是LEMP。
The E in LEMP comes from the pronunciation of Nginx: Engine-X (en-juhn-ecks).The importance is the sound of the first letter rather than its written representation.Besides, LEMP is actually pronounceable and doesn’t sound like you’re just reciting letters of the alphabet.即Nginx的原意是Engine-X,取LEMP能够正确表达Nginx的读音,而且LEMP本身也可以作为一个单词发音,而LNMP则不能。
此外还有用Apache来处理HTTP请求的LAMP环境。关于Nginx和Apache的区别和优缺点在另一篇文章进行了讨论

这里把注意力集中在搭建操作本身,不再过多叙述。
Linux
Linux我选用了Ubuntu Server 18.04 LTS,也可以选用centOS。关于Ubuntu Server和CentOS的讨论参见这里。

Nginx
得益于Ubuntu出色的软件包管理软件apt-get,在Ubuntu上安装nginx非常简单。
sudo apt-get update
sudo apt-get install nginx
安装完成之后Nginx会自动启动。通过浏览器访问自己的ip,看到以下画面说明配置成功。

此时服务器已经可以响应http通信了。
注意要通过IP进行访问,如果使用域名的话在局域网内通过域名访问可能会出现问题,我在另一篇文章中解释了原因和解决方法。
只缘身在此山中?内网域名解析问题

Nginx配置文件
Nginx的配置文件模板保存在/etc/nginx/sites-available/default,按照nginx的设计,配置并启动一个web服务分两步:
- 在sites-available中给每个web服务创建一个单独的配置文件
- 在/etc/nginx/sites-enabled中为想要启动的web服务创建链接。
假设想要启动服务的网站域名是example.com,首先通过default模板在/sites-avialable下创建文件。
sudo cp /etc/nginx/sites-available/deault /etc/nginx/sites-available/example.com
创建完成后按照以下步骤编辑文件中的server{}代码块。
- 因为wordpress是基于php的,所以在默认起始页面中添加index.php。
- 将server_name指定为自己的域名
- 把php部分的代码注释取消掉,启用php
- 忽略.htaccess文件
修改完之后应该是这样:
server{
listen 80 default_server
listen [::]:80 default_server
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name example.com
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
注意factcgi_pass后的php版本要修改成自己的环境中实际安装的版本。我在ubuntu18.04下配置的时候nginx的default文件中给出的是php7.0-fpm.sock,而实际上通过apt安装的php是7.2。安装wordpress时出现了形如connect() to unix:/run/php/php7.0-fpm.sock failed (11: Resource temporarily unavailable) while connecting to upstream的错误信息。
最后在sites-available中创建链接启用配置,重启nginx。
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
sudo service nginx restart
MySQL
sudo apt-get install mysql-server
安装完成之后为wordpress创建一个数据库和一个用户。如果要在不同目录下安装多个wordpress也是同理,为每一个wordpress创建一个独立的数据库和用户即可。假设要创建的数据库信息如下
数据库名wordpressDB用户名wpuser密码password
sudo mysql -u root -p
create database wordpressDB;
create user 'wpuser'@'localhost' identified by 'password';
grant all on wordpressDB.* to 'wpadmin'@'localhost'
之后安装wordpress的时候会要求输入这些信息。
PHP
sudo apt-get install php-fpm php-mysql
这样动态网站所需要的环境就搭建完成了。下一节:安装WordPress。
相关推荐
-
第18问:MySQL CPU 高了,怎么办?2025-02-24 10:27:18
-
mysql索引类型 normal, unique, full text
mysql索引类型 normal, unique, full text2025-02-24 10:05:05 -
uwsgi+django+nginx 搭建部分总结2025-02-24 10:03:33
-
使用Docker配置Nginx环境部署Nextcloud2025-02-24 10:02:03
-
Nginx安装和怎么使用2025-02-24 10:00:45