Linux编译部署LNMP环境

介绍

  Lnmp架构
  Lnmp(web应用软件组合)
  Linux+Nginx+Mysql/MariaDB+Perl/PHP/Python一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的Web应用程序平台。

  LAMP的架构以及通信过程
  Lnmp是一个cs架构的平台(client/server架构, 即客户端/服务器架构),web客户端基于TCP/IP通过http协议发起传送这个请求可能是动态的也可能是静态的,所有的服务器通过发起请求的后缀来判断,如果是静态的就由web服务器自己处理,然后将资源发给客户端。如果请求是动态的这时web服务器会通过FSATCGI协议发给php,通过php模块去MySQL拿数据在通过Nginx交给客户端。

1. 安装nginx


#安装依赖
yum -y install gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetypefreetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers libxslt libxslt-devel  oniguruma oniguruma-devel sqlite-devel
#下载编译包
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar xvf nginx-1.18.0.tar.gz
#提前创建文件夹
mkdir -p /usr/local/nginx/tmp/nginx/client/
mkdir -p  /usr/local/nginx
#创建用户
useradd -s /sbin/nologin nginx -M
#进入目录编译
cd nginx-1.18.0/
./configure --prefix=/usr/local/nginx/nginx --sbin-path=/usr/local/nginx/sbin/nginx/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/log/error.log --http-log-path=/usr/local/nginx/log/access.log --pid-path=/usr/local/nginx/run/nginx/nginx.pid --lock-path=/usr/local/nginx/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/tmp/nginx/client/ --http-proxy-temp-path=/usr/local/nginx/tmp/nginx/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/usr/local/nginx/tmp/nginx/uwsgi --http-scgi-temp-path=/usr/local/nginx/tmp/nginx/scgi --with-pcre
make && make install
#配置环境
cd /usr/local/nginx/sbin/nginx
echo "PATH=$PWD:\$PATH" > /etc/profile.d/nginx.sh

  注意通过我的命令编译的目录结构如下,大家可以对比一下,然后酌情处理目录哈

2. 安装PHP

#下载编译包
wget http://mirrors.sohu.com/php/php-8.0.0.tar.gz
tar xvf php-8.0.0.tar.gz
cd php-8.0.0/
#编译
./configure --prefix=/usr/local/php8 --with-curl --with-mysql-sock=/var/tmp/mysql/mysql.sock --with-jpeg-dir --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libxml-dir --with-mysqli=mysqlnd --with-openssl --with-pcre-regex --with-pdo-mysql=mysqlnd --with-pdo-sqlite --with-pear --with-png-dir --with-xmlrpc --with-xsl --with-zlib --with-pdo-mysql --with-fpm-user=nginx --with-fpm-group=nginx --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip --enable-mysqlnd --enable-maintainer-zts
 make && make install
#配置
cp php.ini-production /usr/local/php8/etc/php.ini
cd /usr/local/php8
cd /etc
cp php-fpm.conf.default php-fpm.conf
cd php-fpm.d/
cp www.conf.default www.conf
#配置环境
cd /usr/local/php8/sbin
echo "PATH=$PWD:\$PATH" > /etc/profile.d/php.sh

3. 配置nginx

[root@study2 /usr/local/nginx/conf 10:31:16]#cat nginx.conf
#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        root   html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

4.创建探针

#在html下创建
[root@study2 /usr/local/nginx/nginx/html 10:33:52]#echo '<?php phpinfo();?>' > index.php
[root@study2 /usr/local/nginx/nginx/html 10:34:19]#cat index.php
<?php phpinfo();?>
#启动nginx,php-fpm
[root@study2 /usr/local/nginx/nginx/html 10:34:21]#nginx
[root@study2 /usr/local/nginx/nginx/html 10:34:26]#php-fpm

5. 安装mysql

  这里我才用的rpm包在线安装的方式。当然你也可采用二进制或者离线包安装。

#安装并启动
yum install -y mariadb-libs mariadb-server mariadb-devel
systemctl start mariadb
#默认是没有配置账号密码的我们需要开启一下
mysql_secure_installation
#输入以上命令后,输入你的命令
#然后现在通过客户端连接
mysql -uroot -ppasswd
#进入mysql后开启这个用户这个密码所有IP可以访问
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'passwd' WITH GRANT OPTION;
FLUSH   PRIVILEGES;

  通过以上数据库的一些操作已经可以远程连接了,还有一些高端的命令与设置我没说。我们的目的是让mysql可以与PHP连接哈,后续会详细说mysql的哈。

  然后我们用工具连接一下看一下:

  好了已经确定可以连接了,然后我们创建一个PHP文件,还是原来创建探针的那个目录下哈。

[root@study2 /usr/local/nginx/nginx/html 11:50:09]#ls
50x.html  index.html  index.php  mysql.php
[root@study2 /usr/local/nginx/nginx/html 11:56:42]#cat mysql.php
<!DOCTYPE html>
<html>
<body>
<?php
echo "Hello World!<br />";
$link = mysqli_connect('172.16.80.131', 'root', 'passwd', 'mysql');
if($link)
    echo "数据库连接成功<br />";
$close=mysqli_close($link);
if($close)
    echo "数据库关闭成功<br />";
?>
</body>
</html>
[root@study2 /usr/local/nginx/nginx/html 11:56:57]#