LNMP
LNMP就是:Linux系統下Nginx+MySQL+PHP這種網站服務器架構。
Linux是一類Unix計算機操作系統的統稱,是目前最流行的免費操作系統。代表版本有:debian、centos、ubuntu、fedora、gentoo等。
Nginx是一個高性能的HTTP和反向代理服務器,也是一個IMAP/POP3/SMTP代理服務器。
Mysql是一個小型關係型數據庫管理系統。
PHP是一種在服務器端執行的嵌入HTML文檔的腳本語言。
這四種軟件均爲免費開源軟件,組合到一起,成爲一個免費、高效、擴展性強的網站服務系統。
開始搭建LNMP環境
注:由於系統是最小化安裝的CentOS 7,所以在編譯安裝之前需要安裝gcc
[root@localhost ~]# yum install -y gcc*
1、編譯安裝Nginx
(1)、安裝軟件依賴包
[root@localhost ~]# yum -y groupinstall "Development Tools" "Server Platform Deveopment" [root@localhost ~]# yum -y install openssl-devel pcre-devel
(2)、下載Nginx安裝包並解壓
[root@localhost ~]# cd /usr/local/src/ [root@localhost src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz [root@localhost src]# useradd nginx ##創建Nginx用戶 [root@localhost src]# tar -xf nginx-1.12.0.tar.gz
(3)、編譯安裝Nginx
[root@localhost src]# cd nginx-1.12.0 [root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre [root@localhost nginx-1.12.0]# make && make install 注: --prefix:Nginx安裝目錄 --user:Nginx用戶 --group:Nginx用戶所屬組 --with-http_ssl_module:提供https支持 --with-http_flv_module:搭建flv視頻服務器使用的 --with-http_stub_status_module:開啓Stub Status模塊,該模塊會產生一個服務器狀態和信息頁 --with-http_gzip_static_module:開啓Gzip靜態模塊,該模塊用於發送預壓縮文件 --with-pcre:perl執行文件路徑
(4)、配置Nginx啓動腳本
[root@localhost ~]# cat /usr/lib/systemd/system/nginx.service [Unit] Description=The nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/bin/rm -f /run/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID KillMode=process KillSignal=SIGQUIT TimeoutStopSec=5 PrivateTmp=true [Install] WantedBy=multi-user.target [root@localhost ~]# chmod a+x /usr/lib/systemd/system/nginx.service ##給權限
(5)、啓動Nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t ##檢查Nginx的配置是否有問題 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 [root@localhost ~]# systemctl start nginx [root@localhost ~]# ps -ef | grep nginx root 10072 1 0 10:52 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 10073 10072 0 10:52 ? 00:00:00 nginx: worker process root 10075 963 0 10:52 pts/0 00:00:00 grep --color=auto nginx
(6)、訪問Nginx,如圖1所示
圖1
2、編譯安裝php-fpm
(1)、安裝依賴包
[root@localhost ~]# yum -y install libmcrypt-devel bzip2-devel gcc openssl-devel php-mcrypt libmcrypt libxml2-devel libjpeg-devel libpng-devel freetype-devel
(2)、下載php-fpm安裝包並解壓
[root@localhost ~]# cd /usr/local/src/ [root@localhost src]# wget http://cn2.php.net/distributions/php-5.5.38.tar.gz [root@localhost src]# tar -xf php-5.5.38.tar.gz
(3)、編譯安裝php-fpm
[root@localhost src]# cd php-5.5.38 [root@localhost php-5.5.38]# ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-mcrypt --with-bz2 --enable-fpm --with-gd --enable-bcmath [root@localhost php-5.5.38]# make && make install ##這一步時間有點久 ##模塊說明: --prefix:php安裝目錄 --with-mysql:mysql安裝目錄,對mysql的支持 --with-pdo-mysql:指定 MySQL 數據庫的安裝目錄位置 --with-mysqli:mysqli擴展技術不僅可以調用MySQL的存儲過程、處理MySQL事務,而且還可以使訪問數據庫工作變得更加穩定。 --with-openssl:openssl的支持,加密傳輸時用到的 --enable-mbstring:多字節,字符串的支持 --with-freetype-dir:打開對freetype字體庫的支持 --with-jpeg-dir:打開對jpeg圖片的支持 --with-png-dir:打開對png圖片的支持 --with-zlib:打開zlib庫的支持 --with-libxml-dir:打開libxml2庫的支持 --enable-sockets:打開 sockets 支持 --with-mcrypt:算法 --with-bz2:打開對bz2文件的支持 --enable-fpm:打上PHP-fpm 補丁後纔有這個參數,CGI方式安裝的啓動程序 --with-gd:打開gd庫的支持 --enable-bcmath:打開圖片大小調整,用到zabbix監控的時候用到了這個模塊
(4)、修改一些配置
[root@localhost php-5.5.38]# cp /usr/local/src/php-5.5.38/php.ini-production /usr/local/php/lib/php.ini [root@localhost php-5.5.38]# mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf [root@localhost php-5.5.38]# useradd -M -s /sbin/nologin php ##創建用戶 [root@localhost php-5.5.38]# sed -i -e 's\;pid = run/php-fpm.pid\pid = run/php-fpm.pid\g' -e 's\nobody\php\g' -e 's\listen = 127.0.0.1:9000\listen = 0.0.0.0:9000\g' /usr/local/php/etc/php-fpm.conf [root@localhost php-5.5.38]# sed -i 's\;daemonize = yes\daemonize = no\g' /usr/local/php/etc/php-fpm.conf
(5)、配置啓動腳本
[root@localhost ~]# cd /usr/local/src/php-5.5.38/sapi/fpm ##php-fpm自帶的啓動腳本 [root@localhost fpm]# cp init.d.php-fpm /etc/init.d/php-fpm [root@localhost fpm]# cd /etc/init.d/ [root@localhost init.d]# chmod +x php-fpm
(6)、啓動php-fpm
[root@localhost ~]# /usr/local/php/sbin/php-fpm -t ##檢查php-fpm配置是否正常 [22-Dec-2017 11:13:16] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful [root@localhost ~]# service php-fpm start Unit php-fpm.service could not be found. Starting php-fpm done [root@localhost ~]# ps -ef | grep php-fpm root 4677 1 0 11:12 ? 00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) php 4678 4677 0 11:12 ? 00:00:00 php-fpm: pool www php 4679 4677 0 11:12 ? 00:00:00 php-fpm: pool www root 4681 963 0 11:12 pts/0 00:00:00 grep --color=auto php-fpm
(7)、訪問PHP
首先先修改Nginx,使其支持PHP
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf location / { root html; index index.html index.htm index.php; } location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } ##添加PHP代碼 [root@localhost html]# cat /usr/local/nginx/html/index.php <?php phpinfo(); ?>
訪問網頁,如圖2所示
圖2
3、編譯安裝MySQL
(1)、安裝依賴包
[root@localhost ~]# yum install -y cmake gcc-c++ ncurses-devel perl-Data-Dumper boost boost-doc boost-devel
(2)、下載MySQL安裝包並解壓
[root@localhost ~]# cd /usr/local/src/ [root@localhost src]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.20.tar.gz [root@localhost src]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20.tar.gz [root@localhost src]# tar -xf mysql-boost-5.7.20.tar.gz [root@localhost src]# tar -xf mysql-5.7.20.tar.gz
(3)、創建用戶並授權
[root@localhost src]# useradd -M -s /sbin/nologin mysql [root@localhost src]# mkdir -p /usr/local/mysql/mydata [root@localhost src]# mkdir -p /usr/local/mysql/conf [root@localhost src]# chown -R mysql:mysql /usr/local/mysql [root@localhost src]# mv /etc/my.cnf /etc/my.cnf.bak ##將該文件名修改爲.bak作爲備份,因爲啓動MySQL時默認會讀取該文件裏的內容
(4)、如果主機的內存不足2G的話,在cmake時會出錯,解決辦法是創建虛擬內存(或者可以把內存調整到2G以上也是可以的)
dd if=/dev/zero of=/swapfile bs=1k count=2048000 ##獲取要增加的2G的SWAP文件塊 mkswap /swapfile ##創建SWAP文件 swapon /swapfile ##激活SWAP文件 swapon -s ##查看SWAP信息是否正確
注:
然後就可以繼續下一步了,如果在編譯安裝完成後,不想要交換分區的話,可以刪除,刪除的命令如下
swapoff /swapfile rm -fr /swapfile
(5)、編譯安裝
[root@localhost src]# cd mysql-5.7.20 [root@localhost mysql-5.7.20]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/mydata -DSYSCONFDIR=/usr/local/mysql/conf -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0 -DMYSQL_MAINTAINER_MODE=0 -DWITH_SSL:STRING=bundled -DWITH_ZLIB:STRING=bundled -DDOWNLOAD_BOOST=1 -DWITH_BOOST=./boost [root@localhost mysql-5.7.20]# make && make install ##耗時比較長 注: -DCMAKE_INSTALL_PREFIX=/usr/local/mysql 設置安裝目錄 -DMYSQL_DATADIR=/usr/local/mysql/mydata 設置數據庫存放目錄 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock 設置UNIX socket 目錄 -DMYSQL_USER=mysql 設置運行用戶 -DDEFAULT_CHARSET=utf8 設置默認字符集,默認latin1 -DEFAULT_COLLATION=utf8_general_ci 設置默認校對規則,默認latin1_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 添加InnoDB引擎支持 -DENABLE_DOWNLOADS=1 自動下載可選文件,比如自動下載谷歌的測試包 -DMYSQL_TCP_PORT=3306 設置服務器監聽端口,默認3306 -DSYSCONFDIR=/usr/local/mysql/conf 設置my.cnf所在目錄,默認爲安裝目錄 更多參數執行 # cmake . -LH 或者查看官方說明
(6)、修改配置
##設置添加到系統服務並設置開機啓動
[root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld ##配置啓動腳本(6版本的) [root@localhost ~]# chmod +x /etc/init.d/mysqld [root@localhost ~]# cp /usr/local/mysql/bin/mysql /usr/bin/mysql ##將MySQL添加到bash裏面
##初始化MySQL,如圖3(初始密碼爲圖3中劃線部分)
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/mydata
圖3
##配置my.cnf
[root@localhost ~]# cp /etc/my.cnf.bak /usr/local/mysql/conf/my.cnf [root@localhost ~]# vim /usr/local/mysql/conf/my.cnf [mysqld] datadir=/usr/local/mysql/mydata ##數據庫存放目錄路徑 socket=/usr/local/mysql/mysql.sock ##UNIX socket 目錄路徑 # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd [mysqld_safe] log-error=/usr/local/mysql/mydata/mysql.err ##錯誤日誌路徑(名稱可以定義爲其他,一般爲主機名,不過這裏我定義爲mysql.err) pid-file=/usr/local/mysql/mydata/mysql.pid ##pid路徑(名稱可以定義爲其他,一般爲主機名,不過這裏我定義爲mysql.pid) # # include all files from the config directory # !includedir /etc/my.cnf.d
##啓動MySQL服務
[root@localhost ~]# service mysqld start
##修改密碼(需要啓動MySQL服務後才能進入數據庫修改密碼,密碼爲初始化後的密碼,即圖3劃線部分),如圖4所示
SET PASSWORD = PASSWORD('00000000');
圖4
(7)、配置啓動腳本(這是配置7版本的啓動腳本,上面第6步時已經配置了一個6版本的啓動腳本了)
[root@localhost ~]# cat /usr/lib/systemd/system/mysql.service [Unit] Description=MariaDB server and services After=syslog.target After=network.target [Service] Type=simple User=mysql Group=mysql ExecStart=/usr/local/mysql/bin/mysqld_safe --basedir=/usr/local/mysql TimeoutSec=300 PrivateTmp=false [Install] WantedBy=multi-user.target [root@localhost ~]# chmod a+x /usr/lib/systemd/system/mysql.service ##授權 [root@localhost ~]# systemctl start mysql [root@localhost ~]# ps -ef | grep mysql mysql 23300 1 0 12:22 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --basedir=/usr/local/mysql mysql 23378 23300 3 12:22 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/mydata --plugin-dir=/usr/local/mysql/lib/plugin --log-error=localhost.localdomain.err --pid-file=localhost.localdomain.pid root 23408 963 0 12:22 pts/0 00:00:00 grep --color=auto mysql
至此,LNMP環境就已經搭建完成了