1. 程式人生 > >LNMP環境部署

LNMP環境部署

php linux mysql nginx

1 安裝nginx

1.1 安裝nginx相關包

yum install -y http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install -y nginx

1.2 創建項目目錄

mkdir -p /var/www/www.cmdschool.org

1.3 配置http和https服務

http config:

cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/www.cmdschool.org_80.conf
vim /etc/nginx/conf.d/www.cmdschool.org.conf

保留以下內容:

server {
    listen       80;
    server_name  www.cmdschool.org;

    location / {
        root   /var/www/www.cmdschool.org;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

https config:

cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/www.cmdschool.org_443.conf
vim /etc/nginx/conf.d/ssl_default.conf

確認存在如下內容:

server {
    listen       443;
    server_name  www.cmdschool.org;
    ssl on;
    ssl_certificate 1_www.cmdschool.org_bundle.crt;
    ssl_certificate_key 2_www.cmdschool.org.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;

    location / {
        root   /var/www/www.cmdschool.org;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

添加靜態測試頁面:

echo www.cmdschool.org > /var/www/www.cmdschool.org/index.html

1.4 配置nginx服務

systemctl enable nginx
systemctl restart nginx

1.5 配置防火墻

firewall-cmd --permanent --add-service http
firewall-cmd --permanent --add-service https
firewall-cmd --reload
firewall-cmd --list-all

1.6 關閉selinux

sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/g‘ /etc/selinux/config
setenforce 0

2 集成PHP fastCGI

2.1 安裝基礎軟件包

yum install -y php-fpm php

2.2 啟動並配置服務自啟動

systemctl enable php-fpm.service
systemctl start php-fpm.service

2.3 配置集成php fastCGI

vim /etc/nginx/conf.d/ssl_default.conf

增加如下代碼

    location ~ \.php$ {
        root           /var/www/www.cmdschool.org;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        /etc/nginx/fastcgi_params;
    }

重啟nginx服務

systemctl restart nginx

2.4 測試php fastCGI

添加調試代碼

echo ‘<?php phpinfo(); ?>‘ > /var/www/www.cmdschool.org/index.php

訪問以下鏈接測試fastCGI

http://www.cmdschool.org/index.php
https://www.cmdschool.org/index.php

2.5 如下日誌可以協助你排錯

tail -f /var/log/nginx/error.log
tail -f /var/log/php-fpm/error.log

2.6 安裝輔助工具包

yum install -y unzip

3 配置MySQL數據庫

3.1 配置MySQL源

vim /etc/yum.repos.d/mysql-community.repo

輸入如下配置:

[mysql56-community-source]
name=MySQL 5.6 Community Server - Source
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/SRPMS
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

3.2 安裝MySQL相關包

yum install -y mysql-community-server mysql-community-devel mysql-community-client

3.3 啟動MySQL服務並配置自啟動

systemctl enable mysqld
systemctl restart mysqld

3.4 初始化MySQL

mysql_secure_installation

向導如下:

[...]
Set root password? [Y/n] y
New password:
Re-enter new password:
[...]
Remove anonymous users? [Y/n] y
[...]
Disallow root login remotely? [Y/n] y
[...]
Remove test database and access to it? [Y/n] y
[...]
Reload privilege tables now? [Y/n] y
[...]

====================

參閱文章

---------

https://secure.php.net/manual/en/install.unix.nginx.php



本文出自 “老譚linux集群博客” 博客,謝絕轉載!

LNMP環境部署