部署LNMP动态网站

部署LNMP动态网站,实现如下目标:php

  1. 安装LNMP平台相关软件
  2. 配置Nginx实现动静分离
  3. 配置数据库,建立帐户与密码
  4. 上线Wordpress代码
  5. 使用Wordpress后台管理界面,调整Wordpress版式

    实现此案例须要按照以下步骤进行。html

    步骤一:安装部署LNMP软件mysql

    备注:mariadb(数据库客户端软件)、mariadb-server(数据库服务器软件)、mariadb-devel(其余客户端软件的依赖包)、php(解释器)、php-fpm(进程管理器服务)、php-mysql(PHP的数据库扩展包)。nginx

    1)安装软件包web

    1. [root@centos7 ~]# yum -y install gcc openssl-devel pcre-devel
    2. [root@centos7 ~]# useradd -s /sbin/nologin nginx
    3. [root@centos7 ~]# tar -xvf nginx-1.12.2.tar.gz
    4. [root@centos7 ~]# cd nginx-1.12.2
    5. [root@centos7 nginx-1.12.2]# ./configure \
    6. --user=nginx --group=nginx \
    7. --with-http_ssl_module \
    8. --with-http_stub_status_module
    9. [root@centos7 nginx-1.12.2]# make && make install
    10. [root@centos7 ~]# yum -y install mariadb mariadb-server mariadb-devel
    11. [root@centos7 ~]# yum -y install php php-mysql php-fpm

    2)启动服务(nginx、mariadb、php-fpm)sql

    1. [root@centos7 ~]# /usr/local/nginx/sbin/nginx                 #启动Nginx服务
    2. [root@centos7 ~]# echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
    3. [root@centos7 ~]# chmod +x /etc/rc.local
    4. [root@centos7 ~]# ss -utnlp | grep :80                        #查看端口信息
    5. [root@centos7 ~]# systemctl start mariadb         #启动mariadb服务器
    6. [root@centos7 ~]# systemctl enable mariadb     
    7.     
    8. [root@centos7 ~]# systemctl start php-fpm         #启动php-fpm服务
    9. [root@centos7 ~]# systemctl enable php-fpm

    附加知识:systemd!!!数据库

    源码安装的软件默认没法使用systemd管理,若是须要使用systemd管理源码安装的软件须要手动编写服务的service文件(编写是能够参考其余服务的模板文件)。如下是nginx服务最终编辑好的模板。apache

    Service文件存储路径为/usr/lib/system/system/目录。vim

    1. [root@centos7 ~]# vim /usr/lib/systemd/system/nginx.service
    2. [Unit]
    3. Description=The Nginx HTTP Server
    4. #描述信息
    5. After=network.target remote-fs.target nss-lookup.target
    6. #指定启动nginx以前须要其余的其余服务,如network.target等
    7. [Service]
    8. Type=forking
    9. #Type为服务的类型,仅启动一个主进程的服务为simple,须要启动若干子进程的服务为forking
    10. ExecStart=/usr/local/nginx/sbin/nginx
    11. #设置执行systemctl start nginx后须要启动的具体命令.
    12. ExecReload=/usr/local/nginx/sbin/nginx -s reload
    13. #设置执行systemctl reload nginx后须要执行的具体命令.
    14. ExecStop=/bin/kill -s QUIT ${MAINPID}
    15. #设置执行systemctl stop nginx后须要执行的具体命令.
    16. [Install]
    17. WantedBy=multi-user.target

    3)修改Nginx配置文件,实现动静分离centos

    修改配置文件,经过两个location实现动静分离,一个location匹配动态页面,一个loation匹配其余全部页面。

    注意修改默认首页为index.php!

    1. [root@centos7 ~]# vim /usr/local/nginx/conf/nginx.conf
    2. ...省略部分配置文件内容...
    3. location / {
    4. root html;
    5. index index.php index.html index.htm;
    6. }
    7. ...省略部分配置文件内容...
    8. location ~ \.php$ {
    9. root html;
    10. fastcgi_pass 127.0.0.1:9000;
    11. fastcgi_index index.php;
    12. include fastcgi.conf;
    13. }
    14. ...省略部分配置文件内容...
    15. [root@centos7 ~]# /usr/local/nginx/sbin/nginx -s reload            #从新加载配置

    4)配置数据库帐户与权限

    为网站提早建立一个数据库、添加帐户并设置该帐户有数据库访问权限。

    1. [root@centos7 ~]# mysql
    2. MariaDB [(none)]> create database wordpress character set utf8mb4;
    3. MariaDB [(none)]> grant all on wordpress.* to wordpress@'localhost' identified by 'wordpress';
    4. MariaDB [(none)]> grant all on wordpress.* to wordpress@'192.168.2.11' identified by 'wordpress';
    5. MariaDB [(none)]> flush privileges;
    6. MariaDB [(none)]> exit

    提示:在mysql和mariadb中%表明匹配全部,这里是受权wordpress用户能够从任意主机链接数据库服务器,生产环境建议仅容许特定的若干主机访问数据库服务器。

    步骤二:上线wordpress代码

    1)上线PHP动态网站代码

    1. [root@centos7 ~]# unzip wordpress.zip
    2. [root@centos7 ~]# cd wordpress
    3. [root@centos7 wordpress]# tar -xf wordpress-5.0.3-zh_CN.tar.gz
    4. [root@centos7 wordpress]# cp -r wordpress/* /usr/local/nginx/html/
    5. [root@centos7 wordpress]# chown -R apache.apache /usr/local/nginx/html/

    提示:动态网站运行过程当中,php脚本须要对网站目录有读写权限,而php-fpm默认启动用户为apache。

    2)初始化网站配置(使用客户端访问web服务器IP)

    1. [root@client ~]# firefox http://192.168.2.11/
相关文章
相关标签/搜索