日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LNMP架构之环境搭建

發(fā)布時(shí)間:2025/3/21 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LNMP架构之环境搭建 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

本文索引:

  • LAMP架構(gòu)介紹
  • Mysql的安裝
  • PHP的安裝
  • Nginx介紹
  • Nginx的安裝

LNMP架構(gòu)介紹

LNMP = Linux + Ningx + Mysql + PHP

由Nginx取代apache,提供web服務(wù);

PHP作為一個(gè)獨(dú)立服務(wù)存在而非apache的一個(gè)模塊,這個(gè)服務(wù)為php-fpm;

Nginx直接處理靜態(tài)請(qǐng)求,動(dòng)態(tài)請(qǐng)求會(huì)轉(zhuǎn)發(fā)給php-fpm。

Nginx在處理靜態(tài)文件的速率較Apache要快的多,這時(shí)兩者的底層設(shè)計(jì)所決定的。同時(shí)Nginx可以處理的并發(fā)訪問量也較Apache要大的多,畢竟Apache創(chuàng)建之初并沒有考慮到當(dāng)今的高并發(fā)訪問量的規(guī)模會(huì)如此之大。Apache采用的是多進(jìn)程的方式,對(duì)內(nèi)存的要求會(huì)較高,而且有限制;Nginx采用多線程的方式,在內(nèi)存消耗方面要比Apache好的多。


Mysql的安裝

Mysql安裝的方法與LAMP架構(gòu)中一致

可能缺少的軟件包:

[root@localhost src]# yum install -y perl-Data-Dumper libaio-devel

具體操作如下:

  • 解壓軟件包,重命名并移動(dòng)至/usr/local
[root@localhost src]# tar zxf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz [root@localhost src]# mv mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql
  • 創(chuàng)建相關(guān)目錄和用戶
[root@localhost src]# cd /usr/local/mysql/ [root@localhost mysql]# useradd mysql [root@localhost mysql]# mkdir /data
  • 初始化
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
  • 配置my.cnf
[root@localhost mysql]# cp support-files/my-default.cnf /etc/my.cnf [root@localhost mysql]# vi /etc/my.cnf "修改[mysqld]內(nèi)的2行即可basedir = /usr/local/mysqldatadir = /data/mysql 保存退出"
  • 配置自啟動(dòng)腳本
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@localhost mysql]# vi /etc/init.d/mysqld "同樣要修改一下參數(shù) basedir=/usr/local/mysql datadir=/data/mysql"
  • 配置開啟啟動(dòng)并啟動(dòng)服務(wù)
[root@localhost mysql]# chmod 755 /etc/init.d/mysqld [root@localhost mysql]# chkconfig --add mysqld [root@localhost mysql]# service mysqld start

本人安裝過程中報(bào)的一個(gè)錯(cuò)(讀安裝過程報(bào)錯(cuò)十分重要)

... ./bin/mysqld: Can't create/write to file '/tmp/iboFQUc3' (Errcode: 30 - Read-only file system) ...

發(fā)現(xiàn)是我之前在tmp下掛載了磁盤鏡像,mysql_install_db腳本對(duì)/tmp目錄又有寫權(quán)限導(dǎo)致的錯(cuò)誤。執(zhí)行umount /tmp后,腳本運(yùn)行正常。


PHP的安裝

PHP的安裝的部分參數(shù)較LAMP不同

  • 解壓壓縮包
  • [root@localhost src]# tar zxvf php-5.6.30.tar.gz [root@localhost src]# cd php-5.6.30
  • 創(chuàng)建用戶php-fpm
  • [root@localhost src]# useradd -M -s /sbin/nologin php-fpm
  • 安裝依賴包
  • [root@localhost php-5.6.30]# yum install -y epel-release [root@localhost php-5.6.30]# yum install -y gcc gcc-c++ libxml2-devel openssl-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel
  • 編譯安裝
  • [root@localhost php-5.6.30]# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl --with-openssl [root@localhost php-5.6.30]# make && make install

    php7版本已經(jīng)取消--with-mysql參數(shù),編譯安裝時(shí)該參數(shù)不寫,寫了會(huì)報(bào)錯(cuò)

  • 修改配置文件
  • [root@localhost php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini [root@localhost php-5.6.30]# cd /usr/local/php-fpm/etc [root@localhost etc]# vi php-fpm.conf [root@localhost etc]# cat php-fpm.conf [global] # 定義全局參數(shù) pid = /usr/local/php-fpm/var/run/php-fpm.pid error_log = /usr/local/php-fpm/var/log/php-fpm.log [www] # 模塊名 listen = /tmp/php-fcgi.sock # 監(jiān)聽socket #listen = 127.0.0.1:9000 listen.mode = 666 # 監(jiān)聽是socket才會(huì)生效 user = php-fpm # 定義用戶 group = php-fpm # 定義組 pm = dynamic pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35 pm.max_requests = 500 rlimit_files = 1024
  • 設(shè)置啟動(dòng)腳本,并設(shè)置開機(jī)啟動(dòng)
  • # 啟動(dòng)腳本模板文件在/usr/local/src/php-5.6.30/sapi/fpm/init.d.php-fpm [root@localhost etc]# cd /usr/local/src/php-5.6.30 [root@localhost php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [root@localhost php-5.6.30]# chmod 755 /etc/init.d/php-fpm [root@localhost php-5.6.30]# chkconfig --add php-fpm [root@localhost php-5.6.30]# chkconfig --level 345 php-fpm on
  • 啟動(dòng)php-fpm服務(wù)
  • [root@localhost php-5.6.30]# service php-fpm start Starting php-fpm done

    php-fpm腳本用法

    相對(duì)于LAMP中php安裝目錄,LNMP安裝的php多了一些文件、目錄

    [root@localhost php-5.6.30]# ls /usr/local/php-fpm/ bin etc include lib php sbin var [root@localhost php-5.6.30]# ls /usr/local/php-fpm/var/ log run# sbin目錄下是php-fpm啟動(dòng)腳本 [root@localhost php-5.6.30]# ls /usr/local/php-fpm/sbin/ php-fpm
    • 查看編譯參數(shù)
    [root@localhost php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -i | grep -i configure [19-Dec-2017 22:10:30] NOTICE: PHP message: PHP Warning: Unknown: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in Unknown on line 0 Configure Command => './configure' '--prefix=/usr/local/php-fpm' '--with-config-file-path=/usr/local/php-fpm/etc' '--enable-fpm' '--with-fpm-user=php-fpm' '--with-fpm-group=php-fpm' '--with-mysql=/usr/local/mysql' '--with-mysqli=/usr/local/mysql/bin/mysql_config' '--with-pdo-mysql=/usr/local/mysql/' '--with-mysql-sock=/tmp/mysql.sock' '--with-libxml-dir' '--with-gd' '--with-jpeg-dir' '--with-png-dir' '--with-freetype-dir' '--with-iconv-dir' '--with-zlib-dir' '--with-mcrypt' '--enable-soap' '--enable-gd-native-ttf' '--enable-ftp' '--enable-mbstring' '--enable-exif' '--with-pear' '--with-curl' '--with-openssl'
    • 查看php的模塊
    [root@localhost ~]# /usr/local/php-fpm/sbin/php-fpm -m [PHP Modules] cgi-fcgi Core ctype curl date dom ereg exif fileinfo filter ...
    • 檢查php-fpm配置文件語法
    [root@localhost ~]# /usr/local/php-fpm/sbin/php-fpm -t [02-Jan-2018 21:43:03] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

    安裝過程報(bào)錯(cuò)提示及解決方法

    • "error: xml2-config not found."
    yum install -y libxml2-devel
    • "error: Cannot find OpenSSL's <evp.h>"
    yum install -y openssl-devel
    • "error: Please reinstall the libcurl distribution"
    yum install -y libcurl-devel
    • "error: jpeglib.h not found"
    yum install -y libjpeg-devel
    • "error: png.h not found"
    yum install -y libpng-devel
    • "error: freetype-config not found"
    yum install -y freetype-devel
    • "error: mcrypt.h not found"
    yum install -y epel-release yum install -y libmcrypt-devel

    Nginx介紹

    官網(wǎng) nginx.org,由俄羅斯程序員開發(fā)的一種web服務(wù)器,小巧而強(qiáng)大。Ningx在處理靜態(tài)文件方便較apache更好,被更多國內(nèi)外網(wǎng)站也采用。Ningx可以通過外接模塊來擴(kuò)展功能,這點(diǎn)與apache一樣。

    Ningx不僅可以應(yīng)用于web服務(wù),同樣可以用于反向代理服務(wù)以及負(fù)載均衡。

    Nginx是一個(gè)開源的軟件,淘寶基于其開發(fā)的Tengine,在使用上與其一致,其內(nèi)的服務(wù)名、配置文件等都一致,不同的是它添加了一些定制化的模塊,在安全限速方面的表現(xiàn)很好,另外還支持對(duì)js、css合并功能。

    openresty:一個(gè)由Nginx核心+lua相關(guān)組件和模塊組成的支持lua的高性能web容器。

    Nginx安裝

    當(dāng)前最新穩(wěn)定版:http://nginx.org/download/nginx-1.12.2.tar.gz

    • 下載并解壓
    [root@localhost ~]# cd /usr/local/src [root@localhost src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz --2017-12-24 22:22:26-- http://nginx.org/download/nginx-1.12.2.tar.gz 正在解析主機(jī) nginx.org (nginx.org)... 206.251.255.63, 95.211.80.227, 2606:7100:1:69::3f, ... 正在連接 nginx.org (nginx.org)|206.251.255.63|:80... 已連接。 已發(fā)出 HTTP 請(qǐng)求,正在等待回應(yīng)... 200 OK 長度:981687 (959K) [application/octet-stream] 正在保存至: “nginx-1.12.2.tar.gz”100%[============================>] 981,687 74.1KB/s 用時(shí) 9.3s 2017-12-24 22:22:36 (103 KB/s) - 已保存 “nginx-1.12.2.tar.gz” [981687/981687])[root@localhost src]# tar zxf nginx-1.12.2.tar.gz
    • 編譯、安裝
    [root@localhost src]# cd nginx-1.12.2 [root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx [root@localhost src]# make && make install
    • 創(chuàng)建啟動(dòng)腳本
    [root@localhost nginx-1.12.2]# vim /etc/init.d/nginx #!/bin/bash # chkconfig: - 30 21 # description: http service. # Source Function Library . /etc/init.d/functions # Nginx Settings NGINX_SBIN="/usr/local/nginx/sbin/nginx" NGINX_CONF="/usr/local/nginx/conf/nginx.conf" NGINX_PID="/usr/local/nginx/logs/nginx.pid" RETVAL=0 prog="Nginx" start() {echo -n $"Starting $prog: "mkdir -p /dev/shm/nginx_tempdaemon $NGINX_SBIN -c $NGINX_CONFRETVAL=$?echoreturn $RETVAL } stop() {echo -n $"Stopping $prog: "killproc -p $NGINX_PID $NGINX_SBIN -TERMrm -rf /dev/shm/nginx_tempRETVAL=$?echoreturn $RETVAL } reload() {echo -n $"Reloading $prog: "killproc -p $NGINX_PID $NGINX_SBIN -HUPRETVAL=$?echoreturn $RETVAL } restart() {stopstart } configtest() {$NGINX_SBIN -c $NGINX_CONF -treturn 0 } case "$1" instart)start;;stop)stop;;reload)reload;;restart)restart;;configtest)configtest;;*)echo $"Usage: $0 {start|stop|reload|restart|configtest}"RETVAL=1 esac exit $RETVAL
    • 更改配置文件權(quán)限并開機(jī)啟動(dòng)
    [root@localhost nginx-1.12.2]# chmod 755 /etc/init.d/nginx [root@localhost nginx-1.12.2]# chkconfig --add nginx [root@localhost nginx-1.12.2]# chkconfig nginx on# 如果無法直接使用nginx命令,則表示/etc/init.d不在PATH變量內(nèi),可以通過修改/etc/profile來添加
    • 修改配置文件
    [root@localhost nginx-1.12.2]# mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf user nobody nobody; worker_processes 2; error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 51200; events {use epoll;worker_connections 6000; } http {include mime.types;default_type application/octet-stream;server_names_hash_bucket_size 3526;server_names_hash_max_size 4096;log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'' $host "$request_uri" $status'' "$http_referer" "$http_user_agent"';sendfile on;tcp_nopush on;keepalive_timeout 30;client_header_timeout 3m;client_body_timeout 3m;send_timeout 3m;connection_pool_size 256;client_header_buffer_size 1k;large_client_header_buffers 8 4k;request_pool_size 4k;output_buffers 4 32k;postpone_output 1460;client_max_body_size 10m;client_body_buffer_size 256k;client_body_temp_path /usr/local/nginx/client_body_temp;proxy_temp_path /usr/local/nginx/proxy_temp;fastcgi_temp_path /usr/local/nginx/fastcgi_temp;fastcgi_intercept_errors on;tcp_nodelay on;gzip on;gzip_min_length 1k;gzip_buffers 4 8k;gzip_comp_level 5;gzip_http_version 1.1;gzip_types text/plain application/x-javascript text/css text/htm application/xml;server{listen 80;server_name localhost;index index.html index.htm index.php;root /usr/local/nginx/html;location ~ \.php$ {include fastcgi_params;fastcgi_pass unix:/tmp/php-fcgi.sock;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;} } }
    • nginx配置文件檢測
    [root@localhost nginx-1.12.2]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    • 啟動(dòng)服務(wù)
    [root@localhost nginx-1.12.2]# /etc/init.d/nginx start
    • 查看是否啟動(dòng)成功
    [root@localhost ~]# /etc/init.d/nginx start Starting nginx (via systemctl): [ 確定 ][root@localhost ~]# ps aux | grep nginx root 1302 0.0 0.0 20500 628 ? Ss 21:40 0:00nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 1307 0.0 0.2 22944 3464 ? S 21:40 0:00nginx: worker process nobody 1308 0.0 0.2 22944 3212 ? S 21:40 0:00nginx: worker process root 2386 0.0 0.0 112680 976 pts/0 R+ 21:42 0:00 grep --color=auto nginx

    可能的錯(cuò)誤

    在配置自定義啟動(dòng)腳本之前如果使用PREFIX/sbin/nginx啟動(dòng)了腳本,那么可能出現(xiàn)如下錯(cuò)誤:

    [root@localhost nginx-1.12.2]# nginx start Starting nginx (via systemctl): Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.[失敗]

    解決方法是使用PREFIX/sbin/nginx -s stop先關(guān)閉nginx進(jìn)程,然后使用自定義腳本啟動(dòng)即可。


    轉(zhuǎn)載于:https://my.oschina.net/LuCastiel/blog/1600842

    總結(jié)

    以上是生活随笔為你收集整理的LNMP架构之环境搭建的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。