CentOS 7上Nginx、PHP、MySQL的安装与配置
安装Nginx
1、通过yum指令安装Nginx
安装依赖包:
```bash
yum install gcc gccc++ y
yum install pcre pcredevel zlib zlibdevel y
```
下载并安装Nginx:
```bash
wget http://nginx.org/download/nginx1.13.6.tar.gz
tar zxvf nginx1.13.6.tar.gz
cd nginx1.13.6
./configure
prefix=/usr/local/nginx
sbinpath=/usr/local/nginx/sbin/nginx
confpath=/usr/local/nginx/nginx.conf
pidpath=/usr/local/nginx/logs/nginx.pid
user=nginx
group=nginx
withhttp_ssl_module
withhttp_flv_module
withhttp_mp4_module
withhttp_stub_status_module
withhttp_gzip_static_module
httpclientbodytemppath=/var/tmp/nginx/client
httpproxytemppath=/var/tmp/nginx/proxy
httpfastcgitemppath=/var/tmp/nginx/fcgi
httpuwsgitemppath=/var/tmp/nginx/uwsgi
httpscgitemppath=/var/tmp/nginx/scgi
make && make install
```
启动Nginx:
```bash
/usr/local/nginx/sbin/nginx
```
2、配置Nginx
编辑配置文件:
```bash
vim /usr/local/nginx/nginx.conf
```
修改worker_processes和keepalive_timeout(可选):
```bash
worker_processes 4;
keepalive_timeout 2;
```
定义虚拟主机:
```bash
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = /40x.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/phpfpm/phpfpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
```
3、开放80端口:
```bash
firewallcmd permanent zone=public addservice=http
firewallcmd reload
```
安装MySQL
1、通过社区仓库安装MySQL
安装MySQL社区仓库:
```bash
wget https://dev.mysql.com/get/mysqlcommunityreleaseel75.noarch.rpm
rpm ivh mysqlcommunityreleaseel75.noarch.rpm
yum update
yum install mysqlserver
```
启动MySQL:
```bash
systemctl start mysqld
systemctl enable mysqld
```
设置开机启动:
```bash
systemctl enable mysqld
```
安全设置:
```bash
mysql_secure_installation
```
修改密码:
```bash
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
```
安装PHP
1、通过yum指令安装PHP及其扩展
安装PHP及相关扩展:
```bash
yum install php phpfpm phpmysql phpbcmath phpgd phpmbstring phpmcrypt phpxml y
```
配置PHPFPM:
```bash
vim /etc/phpfpm.d/www.conf
```
修改以下内容:
```bash
listen = /var/run/phpfpm/phpfpm.sock
listen.owner = nobody
listen.group = nobody
user = nginx
group = nginx
```
启动PHPFPM:
```bash
systemctl start phpfpm
systemctl enable phpfpm
```
测试环境
1、创建测试文件:
创建PHP文件:
```bash
vim /usr/share/nginx/html/info.php
```
```php
<?php phpinfo(); ?>
```
重启Nginx和PHPFPM:
```bash
systemctl restart nginx
systemctl restart phpfpm
```
访问测试页面:
```bash
curl http://your_server_ip/info.php
```
如果能够看到PHP的信息页,说明LNMP环境搭建成功。
相关问答FAQs
1、问题1:如何查看Nginx和PHP版本?
答案1:可以通过以下命令查看Nginx版本:
```bash
nginx v
```
查看PHP版本:
```bash
PHP v
```
2、问题2:如何重启Nginx和PHPFPM服务?
答案2:可以使用以下命令分别重启Nginx和PHPFPM服务:
```bash
systemctl restart nginx
systemctl restart phpfpm
```