centos 7.5,lnmp搭建+xcache

一、nginx安裝

1.一、環境準備

yum install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel ntpdate make wget curl -y

1.二、下載nginx安裝包

wget http://nginx.org/download/nginx-1.14.0.tar.gz

1.三、解壓nginx安裝包

tar xf nginx-1.14.0.tar.gz
cd nginx-1.14.0

1.四、新建nginx用戶以及nginx組

groupadd -r -g 1010 nginx
useradd -r -g nginx -u 1010 nginx -s /sbin/nologin

1.五、安裝nginx

./configure \
--user=nginx \
--group=nginx \ 
--prefix=/usr/local/nginx-1.14.0 \
--with-file-aio \
--with-http_ssl_module \ 
--with-pcre \
-–with-http_stub_status_module \
--with-cc-opt="-O2" \
--with-threads

--user=nginx   #指定程序運行時的非特權用戶
--group=nginx  #指定程序運行時的非特權用戶組
--prefix=/usr/local/nginx-1.14.0 #指向安裝目錄
--with-file-aio #啓用異步io模型
--with-http_ssl_module #加載http ssl模塊
--with-pcre #啓用pcre庫
-–with-http_stub_status_module #啓用ngx_http_stub_status_module支持(獲取nginx自上次啓動以來的工做狀態)
--with-cc-opt="-O2" #這是編譯器優化,目前最經常使用的是-02 而不是3,--with-cpu-opt=opteron,後面接處理器得型號
--with-threads #啓用線程池

make && make install 
ln -s /usr/local/nginx-1.14.0 /usr/local/nginx

二、安裝mariadb

2.一、下載mariadb安裝包

wget https://downloads.mariadb.org/interstitial/mariadb-10.3.10/bintar-linux-glibc_214-x86_64/mariadb-10.3.10-linux-glibc_214-x86_64.tar.gz
tar xf mariadb-10.3.10-linux-glibc_214-x86_64.tar.gz -C /usr/local/mariadb-10.3.10
ln -s /usr/local/mariadb-10.3.10 /usr/local/mysql

2.二、建立用戶和用戶組

groupadd -r -g 1011 mysql
useradd -r -g mysql -u 1011 mysql -s /sbin/nologin

2.三、PATH環境變量、man、頭文件、庫文件

echo 'PATH=$PATH:/usr/local/mysql/bin' >/etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
echo "MANDATORY_MANPATH /usr/local/mysql/man" >>/etc/man_db.conf
ln -s /usr/local/mysql/include/mysql /usr/include/mariadb
echo "/usr/local/mariadb/lib" >/etc/ld.so.conf.d/mariadb-x86_64.conf 
ldconfig -v

2.四、建立msql數據目錄,以及賦予權限

mkdir /data/mydata -vp
chown mysql:mysql /data/ -R
chown mysql:mysql /usr/local/mariadb-10.3.10 -R

2.五、初始化mariadb

須要epel源 依賴多線程內存管理
yum -y install jemalloc-devel jemalloc
/usr/local/mysql/scripts/mysql_install_db --user=mysql --group=mysql --basedir=/usr/local/mysql --datadir=/data/mydata

2.六、init腳本和配置文件

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
sed -ri 's@^(basedir=).*@\1/usr/local/mysql@' /etc/init.d/mysqld
sed -ri 's@^(datadir=).*@\1/data/mydata@' /etc/init.d/mysqld

cat<<EOF>/etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8mb4

[mysql]
#prompt="MySQL [\d]> "
no-auto-rehash

[mysqld]
skip-ssl
port = 3306
user = mysql
server-id = 1
bind-address = 0.0.0.0
#log_timestamps = SYSTEM
socket = /tmp/mysql.sock

basedir = /usr/local/mysql
datadir = /data/mydata
character-set-server = utf8mb4
pid-file = /data/mydata/mysql.pid
init_connect = 'SET collation_connection = utf8_general_ci'
init_connect = 'SET NAMES utf8'
character_set_server = utf8
collation_server = utf8_general_ci


back_log = 300
#skip-networking
skip-name-resolve

max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M

read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M

thread_cache_size = 8

query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M

ft_min_word_len = 4

log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 7

slow_query_log = 1
long_query_time = 1
log_error = /data/mydata/mysql-error.log
slow_query_log_file = /data/mydata/mysql-slow.log

performance_schema = 0
explicit_defaults_for_timestamp

#lower_case_table_names = 1

skip-external-locking

default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120

bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1

interactive_timeout = 28800
wait_timeout = 28800

[mysqldump]
quick
max_allowed_packet = 500M

[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

[mysqld_safe]
#init_connect = 'SET collation_connection = utf8_general_ci'
#init_connect = 'SET NAMES utf8'
#character_set_server = utf8
#collation_server = utf8_general_ci

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

EOF

systemctl enable mysqld

2.7 數據庫配置優化

cp /etc/my.cnf{,.bak}
Mem=`free -m | awk '/Mem:/{print $2}'`
sed -i "s@max_connections.*@max_connections = $((${Mem}/3))@" /etc/my.cnf
if [ ${Mem} -gt 1500 -a ${Mem} -le 2500 ]; then
    #  1500MB < 實際內存 <= 2500MB
    sed -i 's@^thread_cache_size.*@thread_cache_size = 16@' /etc/my.cnf
    sed -i 's@^query_cache_size.*@query_cache_size = 16M@' /etc/my.cnf
    sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 16M@' /etc/my.cnf
    sed -i 's@^key_buffer_size.*@key_buffer_size = 16M@' /etc/my.cnf
    sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 128M@' /etc/my.cnf
    sed -i 's@^tmp_table_size.*@tmp_table_size = 32M@' /etc/my.cnf
    sed -i 's@^table_open_cache.*@table_open_cache = 256@' /etc/my.cnf
elif [ ${Mem} -gt 2500 -a ${Mem} -le 3500 ]; then
    #  2500MB < 實際內存 <= 3500MB
    sed -i 's@^thread_cache_size.*@thread_cache_size = 32@' /etc/my.cnf
    sed -i 's@^query_cache_size.*@query_cache_size = 32M@' /etc/my.cnf
    sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 32M@' /etc/my.cnf
    sed -i 's@^key_buffer_size.*@key_buffer_size = 64M@' /etc/my.cnf
    sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 512M@' /etc/my.cnf
    sed -i 's@^tmp_table_size.*@tmp_table_size = 64M@' /etc/my.cnf
    sed -i 's@^table_open_cache.*@table_open_cache = 512@' /etc/my.cnf
elif [ ${Mem} -gt 3500 ]; then
    #  3500MB < 實際內存
    sed -i 's@^thread_cache_size.*@thread_cache_size = 64@' /etc/my.cnf
    sed -i 's@^query_cache_size.*@query_cache_size = 64M@' /etc/my.cnf
    sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 64M@' /etc/my.cnf
    sed -i 's@^key_buffer_size.*@key_buffer_size = 256M@' /etc/my.cnf
    sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 1024M@' /etc/my.cnf
    sed -i 's@^tmp_table_size.*@tmp_table_size = 128M@' /etc/my.cnf
    sed -i 's@^table_open_cache.*@table_open_cache = 1024@' /etc/my.cnf
fi

2.八、安全設置

sed -ri 's@^(basedir=).*@\1/usr/local/mysql@' /usr/local/mysql/bin/mysql_secure_installation
mysql_secure_installation 

print: /usr/local/mysql/bin/my_print_defaults


NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB

      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


In order to log into MariaDB to secure it, we'll need the current

password for the root user.  If you've just installed MariaDB, and

you haven't set the root password yet, the password will be blank,

so you should just press enter here.


Enter current password for root (enter for none): 

OK, successfully used password, moving on...


Setting the root password ensures that nobody can log into the MariaDB

root user without the proper authorisation.


Set root password? [Y/n] 

New password: 

Re-enter new password: 

Password updated successfully!

Reloading privilege tables..

 ... Success!



By default, a MariaDB installation has an anonymous user, allowing anyone

to log into MariaDB without having to have a user account created for

them.  This is intended only for testing, and to make the installation

go a bit smoother.  You should remove them before moving into a

production environment.


Remove anonymous users? [Y/n] 

 ... Success!


Normally, root should only be allowed to connect from 'localhost'.  This

ensures that someone cannot guess at the root password from the network.


Disallow root login remotely? [Y/n] 

 ... Success!


By default, MariaDB comes with a database named 'test' that anyone can

access.  This is also intended only for testing, and should be removed

before moving into a production environment.


Remove test database and access to it? [Y/n] 

 - Dropping test database...

 ... Success!

 - Removing privileges on test database...

 ... Success!


Reloading the privilege tables will ensure that all changes made so far

will take effect immediately.


Reload privilege tables now? [Y/n] 

 ... Success!


Cleaning up...


All done!  If you've completed all of the above steps, your MariaDB

installation should now be secure.


 

Thanks for using MariaDB!

提示:如沒特殊要求,除設置root密碼外,一路回車便可

三、php安裝

3.1 php-5.6安裝包下載

wget http://101.110.118.21/cn.php.net/distributions/php-5.6.38.tar.gz
tar xf php-5.6.38.tar.gz
cd php-5.6.38

3.二、相關依賴下載

yum -y install libxml2 libxml2-devel openssl openssl-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel pcre pcre-devel libxslt libxslt-devel bzip2 bzip2-devel

3.三、php編譯安裝

./configure --prefix=/usr/local/php-5.6.38 --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --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-sysvshm --enable-xml --enable-zip
make && make install
ln -s /usr/local/php-5.6.38 /usr/local/php

3.四、複製php.ini文件和php-fpm.conf

cp php.ini-production /usr/local/php-5.6.38/lib/php.ini 
cp /usr/local/php-5.6.38/lib/php.ini{,.bak}
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

3.五、配置nginx,讓php請求被傳送到後端得php-fpm模塊

location / { 
       root   html;
        index  index.php index.html index.htm;
    }  

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;
     }

四、使用xcache加速php運行

XCache 是一個開源的 opcode 緩存器/優化器, 這意味着他可以提升您服務器上的 PHP 性能. 他經過把編譯 PHP 後的數據緩衝到共享內存從而避免重複的編譯過程, 可以直接使用緩衝區已編譯的代碼從而提升速度. 一般可以提升您的頁面生成速率 2 到5 倍, 下降服務器負載.php

  目前用於Web的緩存系統不少,包括squid、varnish、Nginx自帶的proxy_cache、FastCGI中的fastcgi_cache、APC、Xcache等。
  像squid、varnish、Nginx自帶的proxy_cache這類系統,屬於重量級產品,配置維護比較麻煩,不適合小型網站,並且通常用這類系統緩存靜態內容,好比圖片、css、JavaScript等;像FastCGI中的fastcgi_cache,它主要用於緩存動態內容,因此在訪問使用fastcgi_cache的網站時速度極快,可是筆者使用時發現其維護比較麻煩,特別是每次網站有數據要更新後,若是不等到緩衝期過時後得須要手動清除緩存才能看到網站更新的內容;至於APC我的感受性能就通常了,拿它和Xcache比較時發現訪問使用Xcache網站的速度明顯高於使用APC網站的速度(筆者沒有具體測試),因此最終選擇了使用Xcache。
  咱們都知道PHP是一種動態語言,它在執行時是以解釋的方式執行,因此PHP代碼每次執行時都會被解析和轉換成操做碼(opcode)。而Xcache是一個開源的操做碼緩存器/優化器,它經過把解析/轉換PHP後的操做碼緩存到文件(直到原始代碼被修改)從而避免重複的解析過程,提升了代碼的執行速度,一般可以提升頁面生成速率2-5倍,下降了服務器負載,提升了用戶訪問網站的速度。css

4.一、安裝Xcache

wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz
tar xf xcache-3.2.0.tar.gz
cd xcache-3.2.0
/usr/local/php/bin/phpize
./configure --enable-xcache--enable-xcache-coverager --enable-xcache-optimizer --with-php-config=/usr/local/php/bin/php-config
make && make install
  注:--enable-xcache表示啓用Xcache支持;--enable-xcache-coverager表示包含用於測量加速器功效的附加特性;--enable-xcache-optimizer表示啓用操做碼優化
  安裝完畢後系統會提示xcache.so模塊生成路徑,本次生成路徑爲/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/,而後把xcache.so移動到/usr/local/php/include/php/ext目錄下。

4.二、修改php配置文件

  配置時咱們能夠參考xcache的配置模板xcache.ini,此文件位於Xcache安裝程序中
# vi /usr/local/php/lib/php.ini
  而後添加以下內容
extension_dir=/usr/local/php/include/php/ext
[xcache-common]
extension = xcache.so
[xcache.admin]
xcache.admin.enable_auth = On
xcache.admin.user = "xcache"
xcache.admin.pass = ""
[xcache]
xcache.shm_scheme ="mmap"
xcache.size=60M
xcache.count =1
xcache.slots =8K
xcache.ttl=0
xcache.gc_interval =0
xcache.var_size=4M
xcache.var_count =1
xcache.var_slots =8K
xcache.var_ttl=0
xcache.var_maxttl=0
xcache.var_gc_interval =300
xcache.test =Off
xcache.readonly_protection = On
xcache.mmap_path ="/tmp/xcache"
xcache.coredump_directory =""
xcache.cacher =On
xcache.stat=On
xcache.optimizer =Off
[xcache.coverager]
xcache.coverager =On
xcache.coveragedump_directory =""

4.3 生成Xcache緩存文件

touch /tmp/xcache
chmod 777 /tmp/xcache

4.4 生成Xcache管理員的祕密(MD5密文)

echo -n "123456" | md5sum
e10adc3949ba59abbe56e057f20f883e
而後將上述生成的MD5密文粘貼到php.ini文件中xcache.admin.pass = ""選項
xcache.admin.pass= "e10adc3949ba59abbe56e057f20f883e"

4.5 拷貝Xcache管理程序到網站根目錄下

cp -a htdocs /usr/local/nginx/html/xcache-admin

5 啓動nginx、mysql、php-fpm

/usr/local/nginx/sbin/nginx
systemctl start mysqld
/usr/local/php/sbin/php-fpm

6 登錄服務器ip/xcache-admin

輸入帳號 xcache 密碼 123456
html

相關文章
相關標籤/搜索